【问题标题】:R ggpubr: move legend title above the legend keysR ggpubr:将图例标题移动到图例键上方
【发布时间】:2019-08-01 02:36:28
【问题描述】:

我正在为 ggpubr 中的传奇位置而苦苦挣扎。我知道我可以修改图例位置 p.e.通过ggpar(legend = "bottom")。但是,如何在图例键上方放置图例标题?

ggplot2 中,似乎guide_legend(title.position = "top") 应该这样做,但是如何使它与ggpubr 一起工作? (感谢@c06n 和此处的链接:https://ggplot2.tidyverse.org/reference/guide_legend.html)。我想将ggplot2ggpubr 结合起来,但我不知道具体原因。

虚拟示例:

df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))

# Plot "len" by "dose" and
# Change line types and point shapes by a second groups: "supp"
p<- ggline(df2, "dose", "len",
       linetype = "supp", shape = "supp",
       color = "supp", palette = c("#00AFBB", "#E7B800"))

# Seems not working
ggpar(p,
  linetype = guide_legend(title = "My title", title.position = "top"))

【问题讨论】:

  • 有可能。但对我来说有点不同。有一个技巧,例如,guides(colour = guide_legend(title.position = "top")) 有效,但 guides(shape = guide_legend(title.position = "top")) 无效。

标签: r ggplot2 ggpubr


【解决方案1】:

我不知道ggpubr,它似乎是ggplot2 的包装。如果这是真的,并且/或者它已经实现了与ggplot2 相同的功能,那么您应该能够通过legend guide 实现它,请参阅here。我认为你需要调整的是title.position

如果您无法使用ggpubr 实现这一点,我建议您改用ggplot2,或许可以根据您的需要调整主题。

编辑。 @markus 有答案,所以把它放在一个地方:

p <- ggline(df2, "dose", "len",
           linetype = "supp", 
           shape = "supp",
           color = "supp", 
           palette = c("#00AFBB", "#E7B800")) +
  guides(color = guide_legend(title.position = "top", 
                              title.hjust = 0.5))

@Bruno Pinheiro 提出了一个有趣的问题,为什么只有color(和fill)可以工作,而shapelinetype 不行。如果情节需要是单色的,这将是相关的。这三个都应该作为分组因素同样有效。

有人有想法吗?

【讨论】:

  • 谢谢@c06n,但您的方法不起作用。我已经更新了我的问题,我开始在 ggplot2 中重新制作东西
  • 你的建议是正确的@c06n,代码是p + guides(color = guide_legend(title.position = "top", title.hjust = 0.5))
  • 真棒@markus,谢谢你,你的建议有效!请问,你能把它作为答案发布吗?
  • @maycca 很高兴它成功了。我认为@c06n 应该在他的答案中包含指向正确方向的代码。顺便说一句,它之所以有效,是因为 ggpubr 返回一个 ggplot 对象,您可以按常规方式修改该对象。
  • 对不起,伙计们。我开始在这里进行一些测试,同时制作了这个新的 cmets,我在发布答案后看到了。我会一直保留到@c06n 编辑他的答案然后删除它。
【解决方案2】:

这些选项在这里起作用。

ggpubr:

df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
                  dose=rep(c("D0.5", "D1", "D2"),2),
                  len=c(6.8, 15, 33, 4.2, 10, 29.5))

library(ggpubr)
p <- ggline(df2, "dose", "len",
       linetype = "supp", shape = "supp",
       color = "supp", palette = c("#00AFBB", "#E7B800"))

p + guides(colour = guide_legend(title.position = "top"))

我不知道为什么,但是如果我将指南设置为另一种美学,则图例标题位置不会改变:

p + guides(shape = guide_legend(title.position = "top"))
p + guides(linetype = guide_legend(title.position = "top"))

这是向ggplot2 专家提出的好问题。

ggplot2 的工作原理相同:

library(ggplot2)
ggplot(df2, aes(x = dose, y = len, colour = supp)) +
  geom_line(aes(group = supp, linetype = supp)) +
  geom_point(aes(shape = supp)) +
  scale_colour_manual(values = c("#00AFBB", "#E7B800")) +
  theme_classic() +
  theme(legend.position = "top") +
  guides(colour = guide_legend(title.position = "top"))

【讨论】:

    猜你喜欢
    • 2022-12-01
    • 2018-01-17
    • 1970-01-01
    • 2013-01-18
    • 2021-11-17
    • 2015-12-15
    • 1970-01-01
    • 2018-06-08
    • 2018-02-09
    相关资源
    最近更新 更多