0

我在 R 方面没有太多经验,我想知道他们是否可以在这种情况下帮助我。

我有以下矩阵:

mat <- matrix(c(0,0.5,0.2,0.23,0.6,0,0,0.4,
         0.56,0.37,0,0.32,0.4,0.99,0.54,0.6,0,0.39), ncol=6, nrow=3)

dimnames(mat) = list( 
                  c("y1","y2","y3"),
                  c("day1","day2","day3","day4","day5","day6")
                    )

> mat
   day1 day2 day3 day4 day5 day6
y1  0.0 0.23 0.00 0.37 0.40 0.60
y2  0.5 0.60 0.40 0.00 0.99 0.00
y3  0.2 0.00 0.56 0.32 0.54 0.39
> 

我想知道如何获得基于矩阵标记点的图表。

值在区间[0,1]内是任意的。可以将生成点的颜色更改为一组约束吗?

例子:

  • (0,0.2] - 红色
  • (0.2,0.4] - 绿色
  • (0.4,0.6] - 黄色
  • (0.6,0.9] - 蓝色
  • (0.9,1] - 黑色

如果我没有很好地解释自己,我很抱歉。

谢谢!

4

4 回答 4

1

假设您的黄色范围应该是 [0.4,0.6] (否则您没有为 (0.4,0.5) 指定颜色 - 即使您的数据不需要它,您也需要)

image(mat,col=c("red","green","yellow","blue","black"),breaks=c(0,0.2,0.4,0.6,0.9,1))

我忽略了间隔端点问题。

于 2013-08-27T23:36:47.973 回答
0

要将颜色分配给不同的间隔,您可以使用 将值分成组cut。就像其他人所说的,有点不清楚如何处理边界上的点,所以我设置include.lowerTRUE

library(reshape2)
df = melt(mat)
colnames(df)[1:2] = c('year', 'day')
df$value_groups = cut(df$value, breaks=c(0,0.2,0.4,0.6,0.9,1), include.lower=TRUE)

library(ggplot2)
ggplot(df, aes(x=day, y=value, colour=value_groups, shape=year)) +
  geom_point(size=3)

结果:

在此处输入图像描述

于 2013-08-27T23:40:29.197 回答
0

以下是我使用 lattice 的方法:

library(reshape2)
library(lattice)

mmat <- melt(mat) # reshaping the data

# note that zero isn't included in the interval
  mmat$colors <- cut(mmat$value, breaks=seq(0, 1, 0.2), include.lower=TRUE) # stealing from Marius


xyplot(value ~ Var2 | Var1, mmat, groups = colors,
  par.settings = list(superpose.symbol = 
    list(col = c('red', 'green', 'yellow', 'blue', 'black'))))
于 2013-08-27T23:46:39.320 回答
0

如果您只想要彩色点,可以这样做:

palette(c("red","green","yellow","blue","black"))
plot.default(
  as.data.frame.table(t(mat))[1:2],
  col=findInterval(t(mat),c(0,0.2,0.4,0.6,0.9)),
  pch=19,
  axes=FALSE,ann=FALSE,
  panel.first=grid()
)
axis(2,at=1:length(rownames(mat)),labels=rownames(mat),lwd=0,lwd.ticks=1,las=1)
axis(1,at=1:length(colnames(mat)),labels=colnames(mat),lwd=0,lwd.ticks=1)
box()
palette("default")

结果:

在此处输入图像描述

于 2013-08-28T00:24:14.073 回答