使用ggarrange定位ggplot标签时出现的问题

46scxncf  于 2023-02-06  发布在  其他
关注(0)|答案(3)|浏览(181)

我正在尝试用ggarrange创建一个绘图面板,我使用“labels”参数为每个绘图创建一个标题,但是根据标题的长度,每个绘图的位置都不一样。看起来标题越长,标签打印得越靠右。
如何使所有标签左对齐?我尝试过使用hjustlabel.x,但这并没有改变什么。
这是我的代码和情节:

ggarrange(plot1,plot2,plot3,
          common.legend=TRUE,
          labels = c("asdf", "asdfasdf", "asdfasdfasdfsadfasdf"),
          hjust=-0.8,
          ncol = 2, nrow = 2)

7fhtutme

7fhtutme1#

我不知道这是否是你问题的捷径,但试试这个可能会有帮助:

plot1 <- plot1 + theme(legend.position="left")+
            labs(title="asdf")
    plot2<- plot2 + theme(legend.position="left")+
            labs(title="asdfasf")
    plot3 <- plot3 + theme(legend.position="left")+
            labs(title="asdfasdfasfdsas")
    ggarrange(plot1,plot2,plot3,
      common.legend=TRUE,
      hjust=-0.8,
      ncol = 2, nrow = 2)

您可以检查:https://www.royfrancis.com/customising-ggplot2/

zf9nrax1

zf9nrax12#

在我看来,您可以先使用geom_text()为每个图设置标签,然后使用ggarrange()将所有子图放在一起。

u2nhd7ah

u2nhd7ah3#

hjust适用于水平移位,vjust适用于垂直移位。在我的例子中,我保持hjust=-7以保持标签在中间,它起作用了。你必须根据你的数据尝试一下。

相关问题