简体   繁体   English

R ggpubr:将图例标题移至图例键上方

[英]R ggpubr: move legend title above the legend keys

I am struggling with the legend position within ggpubr. 我正在ggpubr中的图例位置挣扎。 I know that I can modify the legend position pe by ggpar(legend = "bottom") . 我知道我可以通过ggpar(legend = "bottom")修改图例位置pe。 But, how to place legend title above the legend keys? 但是, 如何将图例标题放置在图例键上方?

In ggplot2 , it seems that guide_legend(title.position = "top") should do this, but how to make it work with ggpubr ? ggplot2 ,似乎guide_legend(title.position = "top")应该这样做,但是如何使其与ggpubr一起ggpubr (thanks to @c06n and link here: https://ggplot2.tidyverse.org/reference/guide_legend.html ). (感谢@ c06n并在此处链接: https ://ggplot2.tidyverse.org/reference/guide_legend.html)。 I would like to combine the ggplot2 and ggpubr but I don't know exactly why. 我想将ggplot2ggpubr结合起来,但我不知道为什么。

在此处输入图片说明

Dummy example: 虚拟示例:

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"))

I do not know ggpubr , it seems like a wrapper for ggplot2 . 我不知道ggpubr ,它似乎是ggplot2的包装。 If that is true, and/or it has implemented the same functionality as ggplot2 , you should be able to achieve that via legend guide , see here . 如果是这样,并且/或者实现了与ggplot2相同的功能,那么您应该可以通过legend guide来实现,请参见此处 I think it is title.position you need to adjust. 我认为这是title.position您需要调整。

If you cannot achieve that with ggpubr , I suggest using ggplot2 instead, perhaps adjusting the theme to your needs. 如果您不能使用ggplot2实现此ggpubr ,建议您改用ggplot2 ,也许可以根据需要调整主题。

Edit. 编辑。 @markus has the answer, so to put it all in one place: @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))

Interesting question though from @Bruno Pinheiro as to why only color (and fill ) will work, but not shape or linetype . 来自@Bruno Pinheiro的有趣问题是,为什么只有color (和fill )有效,而shapelinetype却无效。 This would be relevant if the plot needs to be monochrome. 如果该图需要是单色的,则这将是相关的。 All three should work equally well as grouping factors. 这三个因素在分组因素方面均应表现良好。

Does anyone have an idea? 有人有主意吗?

These options are working here. 这些选项在这里起作用。

With ggpubr : 使用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"))

I don't know why, but if i set the guide for another aesthetic the legend title position do not change: 我不知道为什么,但是如果我为另一种美学设定了指南,则图例标题的位置不会改变:

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

It's a good question to be made for ggplot2 experts. 对于ggplot2专家来说,这是一个很好的问题。

And here is the same working in 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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM