ggplot2:合并图时固定轴的高度/宽度 [英] ggplot2: Fix axis height/width when combining plots

查看:235
本文介绍了ggplot2:合并图时固定轴的高度/宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

library(ggplot2)
library(gridExtra)

df1 <- data.frame(x=c("A1","A2","A3","A4"),something=c(10,18,24,32))
df2 <- data.frame(x=c("C1","C2","C3","C4"),somethingelse=c(10543,182334,242334,32255))


p1 <- ggplot(df1,aes(x,something))+
  geom_bar(stat="identity")
p2 <- ggplot(df2,aes(x,somethingelse))+
  geom_bar(stat="identity")


png("test.png",height=8,width=6,res=120,units="cm")
gridExtra::grid.arrange(p1,p2,heights=grid::unit(c(4,4),"cm"))
dev.off()

当我手动组合两个或多个如上所述的图时,如何将y轴宽度固定为相同,以使所有图(A1-C1,A2-C2,..)上的条对齐?有什么方法可以计算最大y标签宽度并将该宽度应用于所有图的y轴吗?不,在这种特殊情况下,刻面不是我想要的.

When I manually combine two or more plots like above, how can I fix the y-axis widths to be the same, so that my bars across all plots (A1-C1,A2-C2,..) align? Is there some way to calculate max y label width and apply that width to the y-axis of all plots? And no, facets are not something I want in this particular case.

推荐答案

您可以使用ggarrange "rel =" nofollow noreferrer> ggpubr ,参数为align = "v"

You can use ggarrange from ggpubr with argument align = "v"

# Plotting figures provided by OP
ggpubr::ggarrange(p1, p2, heights = c(4, 4), nrow = 2, align = "v")

这篇关于ggplot2:合并图时固定轴的高度/宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆