绘制数据点,其中颜色由一系列值给出

时间:2014-03-23 22:33:13

标签: r plot

我有以下矩阵

> mdat <- matrix(c(0.25,0.45,0.3, 0.75,0.15,0.62,0.40,0.90,1, 0.45,0.15,0), nrow = 4, ncol = +3, byrow = TRUE)
> mdat
     [,1] [,2] [,3]
[1,] 0.25 0.45 0.30
[2,] 0.75 0.15 0.62
[3,] 0.40 0.90 1.00
[4,] 0.45 0.15 0.00

我需要用pch = 22(正方形)绘制它,但是正方形应根据以下颜色着色:
红色,如果数据指向(0.0,0.25) 如果数据指向橙色(0.26,0.50) 数据指向蓝色(0.51,0.75) 绿色,如果数据指向(0.76,1.00)

1 个答案:

答案 0 :(得分:4)

使用findInterval生成一个索引,用于从颜色值矢量中选择颜色:

plot(rep(1:4, 3), mdat, pch=22, 
                   col=c("red","orange","blue")[findInterval(mdat, c(0,.25,.5,.75))])