R ggpubr:将图例标题移动到图例键上方
我正在与ggpubr中的传奇地位作斗争。我知道我可以通过R ggpubr:将图例标题移动到图例键上方,r,ggplot2,ggpubr,R,Ggplot2,Ggpubr,我正在与ggpubr中的传奇地位作斗争。我知道我可以通过ggpar(legend=“bottom”)修改图例位置p.e。但是,如何将图例标题置于图例键上方? 在ggplot2中,似乎guide\u legend(title.position=“top”)应该这样做,但是如何使它与ggpubr一起工作呢?(感谢@c06n和此处的链接:)。我想把ggplot2和ggpubr结合起来,但我不知道确切的原因 虚拟示例: df2 <- data.frame(supp=rep(c("VC", "OJ
ggpar(legend=“bottom”)
修改图例位置p.e。但是,如何将图例标题置于图例键上方?
在ggplot2
中,似乎guide\u legend(title.position=“top”)
应该这样做,但是如何使它与ggpubr
一起工作呢?(感谢@c06n和此处的链接:)。我想把ggplot2
和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))
# 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"))
df2我不知道ggpubr
,它看起来像是ggplot2
的包装器。如果这是真的,并且/或者它实现了与ggplot2
相同的功能,您应该能够通过图例指南
实现这一点,请参阅。我认为这是标题。位置需要调整
如果使用ggpubr
无法实现这一点,我建议改用ggplot2
,也许可以根据需要调整主题
编辑@马库斯有答案,所以把它放在一个地方:
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))
p这些选项在这里起作用
使用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"))
这是一个需要向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"))
谢谢@c06n,但您的方法不起作用。我已经更新了我的问题,并开始在GGPLOT2中重新制作内容。您的建议是正确的@c06n,代码是p+guides(color=guide\u legend(title.position=“top”,title.hjust=0.5))
Awesome@markus,谢谢,您的建议有效!拜托,你能把它贴出来作为答案吗?@maycca很高兴它奏效了。我认为@c06n应该在他的答案中包含指向正确方向的代码。顺便说一句,它之所以有效,是因为ggpubr
返回一个ggplot
对象,您可以用通常的方式修改它。对不起,伙计们。我开始在这里做一些测试,当我发表新的评论时,我看到了一个答案。我会一直保存到@c06n编辑他的答案,然后删除。可能是重复的。但对我来说有点不同。这是一个技巧,因为指南(color=guide\u legend(title.position=“top”)
可以工作,但不能指南(shape=guide\u legend(title.position=“top”)
。
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"))