【发布时间】:2014-04-01 17:06:18
【问题描述】:
我很难理解因子标签如何与图例交互。如何设置级别的顺序,以便 ggplot2 系统创建我设想的图例?
在下面的代码中,该图将红色与区间的负侧相关联,将蓝色与正相关联。
require(ggplot2)
require(RColorBrewer)
set.seed(1492) #discovery!
sample = data.frame(x = c(1:20), y = 1, obs = runif(20, -150, 150))
the.breaks = seq(-100, 100, by = 20)
sample$interval = factor(findInterval(sample$obs, vec = the.breaks, all.inside = TRUE),
labels = the.breaks, levels = c(1:length(the.breaks)))
pal = rev(brewer.pal(11, "RdBu"))
p = ggplot(sample, aes(x, y, colour = interval))
p = p + geom_point(size = 10)
p = p + scale_colour_manual(values = pal, limits = the.breaks, labels = the.breaks)
p = p + guides(colour = guide_legend(override.aes = list(size = 3, shape = 19)))
p
这很好用,但我真的不喜欢
pal = rev(brewer.pal(11, "RdBu"))
声明 - 看起来不优雅。我希望能够用类似的东西替换 scale_colour_manual 的使用
p = p + scale_colour_brewer(palette="RdBu", type="div", limits = the.breaks, labels = the.breaks)
但当我这样做时,红军会与积极的一面联系在一起。
【问题讨论】: