增加图例组之间的间距,使用patchwork设置。

huangapple go评论67阅读模式
英文:

Increase spacing between legend groups with patchwork settings

问题

以下是您要翻译的内容:

更新:
这里建议的代码

p1 + p2 + p3 + plot_layout(ncol = 1) + 
  plot_annotation(tag_levels = 'a') &
  theme(legend.justification = "left")

返回了这个结果

增加图例组之间的间距,使用patchwork设置。

我需要将图例组与各自的图表对齐,我正在使用这个示例:<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")

增加图例组之间的间距,使用patchwork设置。

我能够做到的最接近的是使用& 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 = &#39;a&#39;) &amp;
  theme(legend.justification = &quot;left&quot;)

returned this

增加图例组之间的间距,使用patchwork设置。

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(&quot;thomasp85/patchwork&quot;)
library(ggplot2)
library(patchwork)

d1 &lt;- runif(500)
d2 &lt;- rep(c(&quot;Treatment&quot;,&quot;Control&quot;),each=250)
d3 &lt;- rbeta(500,shape1=100,shape2=3)
d4 &lt;- d3 + rnorm(500,mean=0,sd=0.1)
plotData &lt;- data.frame(d1,d2,d3,d4)
str(plotData)

p1 &lt;- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4, color = d4))# + theme(legend.position = &quot;right&quot;)

p2 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))# + theme(legend.position = &quot;right&quot;)

p3 &lt;- ggplot(data=plotData) +
  geom_histogram(aes(x=d1, color=I(&quot;black&quot;),fill=I(&quot;orchid&quot;))) #+ theme(legend.position = &quot;right&quot;)

p1 + p2 + p3 + plot_layout(ncol = 1, guides = &quot;collect&quot;) + 
  plot_annotation(tag_levels = &#39;a&#39;) &amp; theme(legend.position = &quot;right&quot;)

增加图例组之间的间距,使用patchwork设置。

The closest I could get is manipulating legend.key.height with &amp; theme(legend.position = &quot;right&quot;, legend.key.height = unit(2, &quot;cm&quot;)), 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")

增加图例组之间的间距,使用patchwork设置。

Created on 2023-05-17 with reprex v2.0.2

英文:

You could use legend.justification in your theme instead of the guides = &quot;collect&quot; to have your legends aligned with its plot like this:

library(ggplot2)
library(patchwork)

p1 &lt;- ggplot(data=plotData) + geom_point(aes(x=d3, y=d4, color = d4))# + theme(legend.position = &quot;right&quot;)

p2 &lt;- ggplot(data=plotData) + geom_boxplot(aes(x=d2,y=d1,fill=d2))# + theme(legend.position = &quot;right&quot;)

p3 &lt;- ggplot(data=plotData) +
  geom_histogram(aes(x=d1, color=I(&quot;black&quot;),fill=I(&quot;orchid&quot;))) #+ theme(legend.position = &quot;right&quot;)

p1 + p2 + p3 + plot_layout(ncol = 1) + 
  plot_annotation(tag_levels = &#39;a&#39;) &amp;
  theme(legend.justification = &quot;left&quot;)

增加图例组之间的间距,使用patchwork设置。<!-- -->

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

增加图例组之间的间距,使用patchwork设置。

英文:

Maybe ggarrange is an option for you:

library(ggpubr)

ggarrange(p1, p2, p3, 
          labels = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
          ncol = 1, nrow = 3)

增加图例组之间的间距,使用patchwork设置。

huangapple
  • 本文由 发表于 2023年5月18日 00:52:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定