I am trying to arrange a few plots generated by ggplot2
using the gridExtra
package.
library(ggplot2)
library(gridExtra)
p1 <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + geom_point()
p2 <- ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point()
p3 <- ggplot(iris, aes(Species, Sepal.Width)) + geom_point()
p4 <- ggplot(iris, aes(Sepal.Width, Petal.Width)) + geom_point()
grid.arrange(main="CO2Exp", p1, p2, p3, p4, ncol=2)
When I try to have a main title, the follwing works fine.
grid.arrange(main="CO2Exp", p1, p2, p3, p4, ncol=2)
But when I try with expression
to get a subscript,
grid.arrange(main=expression(paste(CO[2], "Exp")), p1, p2, p3, p4, ncol=2)
I get the following error
Error in valid.data(rep(units, length.out = length(x)), data) :
no 'grob' supplied for 'grobwidth/height' unit
How to fix this?
I am using R
_3.1.3, gridExtra
_0.9.1 and ggplot2
_1.0.1
Create a textGrob
explicitly:
grid.arrange(main = textGrob(label = expression(paste(CO[2], "Exp"))),
p1, p2, p3, p4, ncol=2)
Edit (16/07/2015): with gridExtra
>= 2.0.0, the main
parameter has been renamed top
. See ?arrangeGrob
for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With