gpt4 book ai didi

R ggpubr : move legend title above the legend keys

转载 作者:行者123 更新时间:2023-12-05 03:02:14 26 4
gpt4 key购买 nike

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

ggplot2 中,似乎 guide_legend(title.position = "top") 应该这样做,但是如何让它与 ggpubr ? (感谢@c06n 和链接在这里:https://ggplot2.tidyverse.org/reference/guide_legend.html)。我想结合使用 ggplot2ggpubr 但我不知道为什么。

enter image description here

虚拟示例:

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

最佳答案

我不知道 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)有效,而 shape 无效线型。如果情节需要是单色的,这将是相关的。这三个因素作为分组因素应该同样有效。

有没有人有想法?

关于R ggpubr : move legend title above the legend keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55090702/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com