英文:
Increase spacing between legend groups with patchwork settings
问题
以下是您要翻译的内容:
更新:
这里建议的代码
p1 + p2 + p3 + plot_layout(ncol = 1) +
plot_annotation(tag_levels = 'a') &
theme(legend.justification = "left")
返回了这个结果
我需要将图例组与各自的图表对齐,我正在使用这个示例:<https://gotellilab.github.io/GotelliLabMeetingHacks/NickGotelli/ggplotPatchwork.html>
期望的输出应该在d4和d2组之间有更大的间距,以使d4与面板a对齐,d2与面板b对齐。
library(devtools)
install_github("thomasp85/patchwork")
library(ggplot2)
library(patchwork)
d1 <- runif(500)
d2 <- rep(c("Treatment","Control"),each=250)
d3 <- rbeta(500,shape1=100,shape2=3)
d4 <- d3 + rnorm(500,mean=0,sd=0.1)
plotData <- data.frame(d1,d2,d3,d4)
str(plotData)
p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4, color = d4))# + theme(legend.position = "right")
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))# + theme(legend.position = "right")
p3 <- ggplot(data=plotData) +
geom_histogram(aes(x=d1, color=I("black"),fill=I("orchid"))) #+ theme(legend.position = "right")
p1 + p2 + p3 + plot_layout(ncol = 1, guides = "collect") +
plot_annotation(tag_levels = 'a') & theme(legend.position = "right")
我能够做到的最接近的是使用& theme(legend.position = "right", legend.key.height = unit(2, "cm"))
来操纵legend.key.height
,但这并不理想,因为图例仍然居中对齐,而且调整unit()
命令以使图例高度与相应的面板相同将非常耗时。
谢谢您的时间。
英文:
UPDATE:
The suggested code here
p1 + p2 + p3 + plot_layout(ncol = 1) +
plot_annotation(tag_levels = 'a') &
theme(legend.justification = "left")
returned this
I need to have the legend groups aligned with the respective plots and I'm using this example from: <https://gotellilab.github.io/GotelliLabMeetingHacks/NickGotelli/ggplotPatchwork.html>
The desired output would have a larger space between the d4 and d2 groups, such that d4 is aligned with panel a and d2 panel b.
library(devtools)
install_github("thomasp85/patchwork")
library(ggplot2)
library(patchwork)
d1 <- runif(500)
d2 <- rep(c("Treatment","Control"),each=250)
d3 <- rbeta(500,shape1=100,shape2=3)
d4 <- d3 + rnorm(500,mean=0,sd=0.1)
plotData <- data.frame(d1,d2,d3,d4)
str(plotData)
p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4, color = d4))# + theme(legend.position = "right")
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))# + theme(legend.position = "right")
p3 <- ggplot(data=plotData) +
geom_histogram(aes(x=d1, color=I("black"),fill=I("orchid"))) #+ theme(legend.position = "right")
p1 + p2 + p3 + plot_layout(ncol = 1, guides = "collect") +
plot_annotation(tag_levels = 'a') & theme(legend.position = "right")
The closest I could get is manipulating legend.key.height
with & theme(legend.position = "right", legend.key.height = unit(2, "cm"))
, but this isn't ideal because the legends are still center-justified and it would be very time consuming to nudge the unit()
command so the legend height is the same as the respective panel's.
Thanks for your time.
答案1
得分: 2
你可以在你的theme
中使用legend.justification
而不是guides = "collect"
,以使你的图例与其图表对齐,就像这样:
library(ggplot2)
library(patchwork)
p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4, color = d4))# + theme(legend.position = "right")
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))# + theme(legend.position = "right")
p3 <- ggplot(data=plotData) +
geom_histogram(aes(x=d1, color=I("black"),fill=I("orchid"))) #+ theme(legend.position = "right")
p1 + p2 + p3 + plot_layout(ncol = 1) +
plot_annotation(tag_levels = 'a') &
theme(legend.justification = "left")
Created on 2023-05-17 with reprex v2.0.2
英文:
You could use legend.justification
in your theme
instead of the guides = "collect"
to have your legends aligned with its plot like this:
library(ggplot2)
library(patchwork)
p1 <- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4, color = d4))# + theme(legend.position = "right")
p2 <- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))# + theme(legend.position = "right")
p3 <- ggplot(data=plotData) +
geom_histogram(aes(x=d1, color=I("black"),fill=I("orchid"))) #+ theme(legend.position = "right")
p1 + p2 + p3 + plot_layout(ncol = 1) +
plot_annotation(tag_levels = 'a') &
theme(legend.justification = "left")
<!-- -->
<sup>Created on 2023-05-17 with reprex v2.0.2</sup>
答案2
得分: 2
或许ggarrange
是您的选择:
library(ggpubr)
ggarrange(p1, p2, p3,
labels = c("A", "B", "C"),
ncol = 1, nrow = 3)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论