如何避免在ggarrange中裁剪标签?

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

How to avoid cropping of labels in ggarrange?

问题

我使用ggplotggarrange()函数合并了4个图形。我的问题是,合并后的图片中,一些柱状图标签被裁剪,而在单独的图片中则没有被裁剪,比如A图中的25%。

如何解决这个问题?
我尝试了coord_cartesian(clip = "off"),但它只对单独的图片起作用。

英文:

I have 4 plots combined by ggarrange() in ggplot. My problem is that some bar labels are cropped in combined picture, while they are not cropped at all on a single pictures. Like 25% on the A plot.

如何避免在ggarrange中裁剪标签?

ggarrange(country + theme(axis.text.y = element_text(size = 10), axis.text.x = element_blank()), 
      position + theme(axis.text.y = element_text(size = 10), axis.text.x = element_blank()), 
      fieldplot + theme(axis.text.y = element_text(size = 10), axis.text.x = element_blank()), 
      methods + scale_y_discrete(labels=c("OPMs", "iEEG", "MEG", "Scalp EEG")) + 
        theme(axis.text.y = element_text(size = 10), axis.text.x = element_blank()), 
      labels = c("A", "B", "C", "D"),
      ncol = 2, nrow = 2, align = 'h')  

How can I solve this problem?
I tried coord_cartesian(clip = "off"), but it works only on individual pictures.

答案1

得分: 1

问题在于轴上剩余的空间量(默认情况下)是刻度的5%。 当图表很大时,这5%足以容纳文本,但在图表很小时则不够。

我们可以将该空间增加到15%,并且还可以移除x轴开头的5%,因为对于条形图来说,它并不是非常有用。 我们通过定义x刻度的轴扩展来实现这一点。

将以下内容添加到所有的图表中:

+ scale_x_continuous(expand = expansion(mult = c(0, 0.15))

您可以根据需要更改0.15以获得更多或更少的空间。

英文:

The problem is that the amount of space left on the axis is (by default) 5% of the scale. That 5% is enough space to fit the text when the plot is large, but not when the plot is small.

We can increase that space to say 15%, and also remove the 5% at the start of the x-axis, since it is not very useful for bar graphs. We do this by defining the axis extension of the x-scale.

Add this to all your plots:

+ scale_x_continuous(expand = expansion(mult = c(0, 0.15))

You can change the 0.15 as needed to make more or less space.

huangapple
  • 本文由 发表于 2023年7月4日 21:51:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613337.html
匿名

发表评论

匿名网友

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

确定