Cannot disable R markdown output from gridExtra (additional comment)
Asked Answered
S

3

9

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

Sibilant answered 1/9, 2016 at 9:44 Comment(0)
P
11

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

Pronto answered 1/9, 2016 at 9:48 Comment(0)
L
4

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))
```
Longing answered 18/7, 2018 at 13:23 Comment(0)
S
0

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))
Sverdlovsk answered 28/3, 2023 at 16:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.