Error adding main header with index to gridExtra

I am trying to organize several graphs 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 get the main header, this action works fine.

 grid.arrange(main="CO2Exp", p1, p2, p3, p4, ncol=2) 

But when I try to get an index with expression ,

 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 it?

I am using R _3.1.3, gridExtra _0.9.1 and ggplot2 _1.0.1

+6
source share
1 answer

Create textGrob explicitly:

 grid.arrange(main = textGrob(label = expression(paste(CO[2], "Exp"))), p1, p2, p3, p4, ncol=2) 

Edit (07/16/2015): with gridExtra > = 2.0.0, the main parameter was renamed top . See ?arrangeGrob .

+7
source

Source: https://habr.com/ru/post/985179/


All Articles