1

I want that the two plots on top of each other have the same size. The problem is that the length of tick labels are different and therefore the plots are not aligned. Just making the font smaller is not a solution because it's not reader friendly. There must be a way to say how big a plot should be just within ggplot. I just want to control the size of the plot manually regardless whether I have to bar plots or a bar plot and boxplot etc. How do I accomplish this?

 library(ggplot2)
library(grid)
library(gridExtra)

df1 <- data.frame(a = as.factor(1:20), b = runif(20, 500000, 900000))
df2 <- data.frame(a = as.factor(1:20), c = rnorm(2000))

plot1 <- ggplot(df1, aes(x = a, y =b)) + geom_bar(stat = "identity")
plot2 <- ggplot(df2, aes(x = a, y =c)) + geom_boxplot()

grid.arrange(plot1,
             plot2,
             ncol = 1)

Here is my example: enter image description here

JAQuent
  • 1,137
  • 11
  • 25

1 Answers1

0

As one of the comments mentioned, package cowplot has the functionality you need.

library(ggplot2)
library(cowplot)

df1 <- data.frame(a = as.factor(1:20), b = runif(20, 5, 9))
df2 <- data.frame(a = as.factor(1:20), b = runif(20, 50000, 90000))

plot1 <- ggplot(df1, aes(x = a, y =b)) + geom_bar(stat = "identity")
plot2 <- ggplot(df2, aes(x = a, y =b)) + geom_bar(stat = "identity")

#see ?plot_grid for more details
plot_grid(plot1,plot2,ncol = 1, align = "v", rel_heights = c(.7, .3))

Created on 2019-01-12 by the reprex package (v0.2.1)

Chase
  • 67,710
  • 18
  • 144
  • 161