【发布时间】:2014-08-19 22:14:14
【问题描述】:
假设我有这个例子data.frame:
df = data.frame(x = c(1,1,1,2,2,2,3,3,3), y = runif(9,0,0.9))
我为其分配了如下颜色(请原谅我对这部分的非复杂代码):
df$color = rep(NA, nrow(df))
color.breaks = seq(0,0.9,0.3)
colors = c("lightgray","gray","darkgray")
df$color[which(df$x == 1)] = colors[findInterval(df$y[which(df$x == 1)], color.breaks)]
colors = c("lightblue","blue","darkblue")
df$color[which(df$x == 2)] = colors[findInterval(df$y[which(df$x == 2)], color.breaks)]
colors = c("lightgreen","green","darkgreen")
df$color[which(df$x == 3)] = colors[findInterval(df$y[which(df$x == 3)], color.breaks)]
然后我绘制:
plot(x = df$x, y = df$y, col = as.character(df$col), pch = 16)
现在我想添加一个三行四列的图例,其中第一列是color.breaks,接下来的三列是各自的颜色。
类似这样的:
知道我是如何使用legend 函数完成这项工作的吗?
【问题讨论】: