【问题标题】:Factor / level order with ggplot2 legendggplot2图例的因子/水平顺序
【发布时间】: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)

但当我这样做时,红军会与积极的一面联系在一起。

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    您可以以任何您喜欢的方式订购您的因子水平,这应该有助于您获得正确的颜色。尝试this explanation 或任何其他的订购因素水平。请注意,博文中的 opts 命令已过时。

    还可以查看this question

    还有this one

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 2018-12-07
      • 2017-07-14
      • 1970-01-01
      • 2014-09-29
      • 1970-01-01
      • 2017-01-02
      • 2021-12-08
      相关资源
      最近更新 更多