[R] Arranging ggplot2 objects with ggplotGrob()

Ulrik Stervbo U|r|k@Stervbo @end|ng |rom ruhr-un|-bochum@de
Wed Jul 29 13:34:30 CEST 2020


Then this should work:

```
library(ggplot2)
library(cowplot)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) +  
geom_point()
p2 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width * 1000)) + 
geom_point()

plot_grid(p1, p2, ncol = 1, align = "hv", rel_heights = c(2, 1), axis = 
"t")

p1 <- p1 + theme(
   axis.text.x = element_blank(),
   axis.title.x = element_blank(),
   axis.ticks.x = element_blank()
)

plot_grid(p1, p2, ncol = 1, align = "hv", rel_heights = c(2, 1), axis = 
"t")

# You can play around with ggplot2 plot.margin to further reduce the 
space
p1 <- p1 + theme(
   plot.margin = margin(b = -6)
)

p2 <- p2 + theme(
   plot.margin = margin(t = -6)
)

plot_grid(p1, p2, ncol = 1, align = "hv", rel_heights = c(2, 1), axis = 
"t")
```

Best,
Ulrik



More information about the R-help mailing list