baptiste / gridExtra

Miscellaneous Functions for "Grid" Graphics
http://cran.r-project.org/web/packages/gridExtra/index.html
15 stars 4 forks source link

arrangeGrob sometimes does and sometimes does not plot #38

Closed vnijs closed 7 years ago

vnijs commented 7 years ago

In the R-console I get the following:

> library(ggplot2)
> library(gridExtra)
> plots <- list()
> plots[["p1"]] <- ggplot(mpg, aes(displ, hwy)) + geom_point()
> plots[["p2"]] <- ggplot(mpg, aes(displ, hwy)) + geom_line()
> print(arrangeGrob(grobs = plots))
TableGrob (2 x 1) "arrange": 2 grobs
   z     cells    name           grob
p1 1 (1-1,1-1) arrange gtable[layout]
p2 2 (2-2,1-1) arrange gtable[layout]

In Rstudio trying to knit the following also shows the TableGrob text but when run from the command line in Rstudio I do see the plots in the plots tab. Any idea what might be going on?

---
title: "R Notebook"
output: html_document
---

```{r}
library(ggplot2)
library(gridExtra)
plots <- list()
plots[["p1"]] <- ggplot(mpg, aes(displ, hwy)) + geom_point()
plots[["p2"]] <- ggplot(mpg, aes(displ, hwy)) + geom_line()
print(arrangeGrob(grobs = plots))
```
baptiste commented 7 years ago

print() is not the right function to draw a gtable. Use grid.draw() instead.

vnijs commented 7 years ago

Forgot about that. Thanks @baptiste!

Related question ... why does arrangeGrob not show any output in the R-notebook below but grid.arrange does show a plot?

---
title: "R Notebook"
output: html_notebook
---

```{r}
library(ggplot2)
library(gridExtra)
plots <- list()
plots[["p1"]] <- ggplot(mpg, aes(displ, hwy)) + geom_point()
plots[["p2"]] <- ggplot(mpg, aes(displ, hwy)) + geom_line()
arrangeGrob(grobs = plots)
```

```{r}
grid.arrange(grobs = plots)
```