M. Siwik
M. Siwik

Reputation: 497

Cannot disable R markdown output from gridExtra (additional comment)

In my r markdown report i use 2 chunks.

First:

```{r, include = FALSE, echo=FALSE, results='asis'}```

Where i load all packages and do computation.

And second

{r, fig.margin = TRUE, fig.width=10,
fig.height=4.5, echo=FALSE, results='asis', 
message=FALSE, warning = FALSE} 

Where i have for loop where I filter data I producent in first chunk, and make ggplots for each i.

My problem is that, when i produce 2 plots in second chunk i combine them in one grid by grid.arrange, but there is additional comment I don't want to show.

TableGrob (2 x 2) "arrange": 2 grobs
  z     cells    name           grob
1 1 (1-2,1-1) arrange gtable[layout]
2 2 (1-2,2-2) arrange gtable[layout]

title: "Test"
author: "Maybe It's You"
date: "2016.09.01"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(grid)
library(gridExtra)
library(ggplot2)

lay <- rbind(c(1,2),
             c(1,2))
gs <- list()

```


```{r cars}
summary(cars)
```

## Including Plots

You can also embed plots, for example:

```{r pressure, echo=FALSE, results='asis', message=FALSE, warning = FALSE}
p1 <- qplot(1:10)
p2 <- qplot(1:10)
gs[[1]] <- p1
gs[[2]] <- p2          


wykres <- grid.arrange(grobs = gs, layout_matrix = lay)
print(wykres)

```

Fragment of result with gridExtra comment (TablGrob...)

Pic of pdf.output

Upvotes: 9

Views: 4336

Answers (3)

habeeba
habeeba

Reputation: 11

Use the cowplot package. If plot1 and plot2 are ggplot objects, then we can arrange them side by side with relative widths like so:

library(cowplot)
finalplot <- plot_grid(plot1, plot2, ncol = 2, align = "h", axis = "tb", rel_widths = c(4, 1))

Upvotes: 0

Takeshi Gitano
Takeshi Gitano

Reputation: 849

The solution worked for me, but after noticing that the R code chunk should directly return the grid.arrange(), without previously storing the result. For example:

This will still display gridExtra info (TableGrob comments):

```{r pressure, echo=FALSE, results='asis', message=FALSE, warning = FALSE}
p1 <- qplot(1:10)
p2 <- qplot(1:10)
gs[[1]] <- p1
gs[[2]] <- p2          


wykres <- grid.arrange(grobs = gs, layout_matrix = lay)
return(wykres)
```

And this won't display gridExtra info:

```{r pressure, echo=FALSE, results='asis', message=FALSE, warning = FALSE}
p1 <- qplot(1:10)
p2 <- qplot(1:10)
gs[[1]] <- p1
gs[[2]] <- p2          


return(grid.arrange(grobs = gs, layout_matrix = lay))
```

Upvotes: 4

baptiste
baptiste

Reputation: 77116

don't print(), grid.arrange() draws by itself. If you want to store the result and then draw it, use arrangeGrob()+grid.draw()

Upvotes: 11

Related Questions