【问题标题】:Trouble with fine tuning legend key in ggplot2ggplot2中微调图例键的问题
【发布时间】:2017-01-20 13:53:16
【问题描述】:

我正在尝试使用 theme()guides() 函数使图例键与绘图完全匹配。

从这个情节开始:

library(ggplot2)
data(mpg)

mpg$year <- as.numeric(mpg$year)
means <- tapply(mpg$displ, list(mpg$year), FUN = mean)
means <- as.data.frame(means)
means$year <- as.numeric(row.names(means))
names(means)[1] <- "displ"

p1 <- 
  ggplot(mpg, aes(x = year, y = displ)) +
  geom_jitter(aes(color = "points")) +
  geom_errorbar(data = means, aes(ymin = displ, ymax = displ, 
    color = "mean")) +
  scale_x_continuous(breaks = c(1998, 2008))

p1

我得到一个情节,我可以使用以下方法修改图例键:

p1 + theme(legend.key = element_rect(fill = NA), 
legend.title = element_blank()) + 
guides(color = guide_legend(override.aes = 
list(shape=c(NA, 16), linetype=c(1, 0), fill = c(NA, NA))))

得到想要的结果:

但是,当情节还包含geom_ribbon() 时,同样的方法会失败:

p2 <- 
  ggplot(mpg, aes(x = year, y = displ)) +
  geom_jitter(aes(color = "points")) +
  geom_ribbon(aes(ymin = 3, ymax = 4, color = "ribbon"), 
    fill = "green", alpha = 0.5) +
  geom_errorbar(data = means, aes(ymin=displ, ymax=displ, 
    color="mean")) +
  scale_x_continuous(breaks = c(1998, 2008))

p2 + theme(legend.key = element_rect(fill = NA), 
  legend.title = element_blank()) + 
  guides(color = guide_legend(override.aes = 
    list(shape=c(NA, 16, NA), linetype=c(1, 0, 0), 
    fill = c(NA, NA, "green"))))

要明确:我希望代表平均值的图例键显示为一条直的红线,就像在第一个图中一样,没有边框,没有对角线。

(我意识到我可以使用 stat_summary() 而不是创建单独的数据框,但这不适用于我的实际项目)。

感谢您的建议,

拉斯


【问题讨论】:

  • 需要设置aes中的功能区颜色吗?
  • 我想是的。我需要一个图例来说明功能区显示的内容,据我所知,唯一的方法是在 aes 中创建一个常量 color
  • 您可以使用填充代替,这样可以避免图例中的线条。
  • 您在aes() 中用geom_ribbon() 填充而不是颜色?对我来说,这会产生错误吗?
  • 更准确地说:使用填充而不是颜色完全失去了功能区图例

标签: r ggplot2 legend


【解决方案1】:

这可能有点小技巧,但我从 geom_ribbon 中删除了图例,并在图例中添加了色带颜色作为点类型 (pch = 15)。 颜色还不完美,但我敢打赌你可以做到。

p2 <- 
  ggplot(mpg, aes(x = year, y = displ)) +
  geom_jitter(aes(color = "points")) +
  geom_ribbon(aes(ymin = 3, ymax = 4, color = "ribbon"), 
              fill = "green", alpha = 0.5, show.legend = FALSE) +
  geom_errorbar(data = means, aes(ymin=displ, ymax=displ, 
                                  color="mean")) +
  scale_x_continuous(breaks = c(1998, 2008))

p2 + theme(legend.key = element_rect(fill = NA), 
           legend.title = element_blank()) + 
  guides(color = guide_legend(override.aes = 
                                list(shape=c(NA, 16, 15), linetype=c(1, 0, 0), 
                                     color = c("red", "green", "green"),
                                     alpha = c(1,1,.5),
                                     size = c(1,2,6))))

【讨论】:

  • 谢谢,但是当我运行你的代码时,我没有得到你的情节?我得到所有三个带有绿色填充的图例键?
  • 是的。我只是复制粘贴了您的代码,包括show.legend = FALSE。我正在运行:R 版本 3.2.2 (2015-08-14) 平台:x86_64-w64-mingw32/x64 (64-bit) 运行条件:Windows 7 x64 (build 7601) Service Pack 1 ggplot2_1.0.1 谢谢你努力,@Wie​​tze314
  • 我已经在 R 版本 3.3.1 (2016-06-21) 和在 Rstudio 服务器上运行的 R 上测试了 ggplot2 v2.1.0。
  • 在 R-fiddle (3.1.2) 上,我可以看到您遇到的问题。我不知道为什么版本之间存在差异......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-20
相关资源
最近更新 更多