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

enter image description here

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.

pogibas
  • 27,303
  • 19
  • 84
  • 117
mindlessgreen
  • 11,059
  • 16
  • 68
  • 113
  • 1
    Here are two options: `egg::ggarrange(p1, p2)` or `cowplot::plot_grid(p1, p2, align="v", ncol=1)`. – eipi10 Dec 28 '17 at 08:53
  • 1
    some discussion in this vignette: https://cran.r-project.org/web/packages/egg/vignettes/Ecosystem.html – baptiste Dec 28 '17 at 09:11

1 Answers1

10

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

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117