3

I am trying to display multiple plots side by side using par in an R Notebook. I have the code for all of the plots in a single code chunk as follows:

code

However, when I render the document in html, the code chunk get split into multiple pieces:

output

I know that R Notebooks only allow one output per chunk, and that if you include multiple lines with output in a single chunk, then it will split the chunk. However, this chunk only has one output (albeit consisting of four plots).

Does anyone know how to fix this? Or at least, does anyone know of a work-around?

By the way, I will confess to being aware that this question was asked before, but it was 2.5 years ago, and there was no answer. I am really hoping that someone will know the answer this time. This has long been a source of frustration for me.

Thank you.

Beane
  • 306
  • 1
  • 4

2 Answers2

0

Create new objects for each histogram. Right now you're making four outputs, but if you assign each histogram to an object, R evaluates only one output - the par output. This code works for me:

sample1 <- rnorm(40,10,3)
sample2 <- rnorm(40,10,3)
sample3 <- rnorm(40,10,3)
sample4 <- rnorm(40,10,3)

par(mfrow=c(2,2))
hist1 <- hist(sample1)
hist2 <- hist(sample2)
hist3 <- hist(sample3)
hist4 <- hist(sample4)
James Martherus
  • 1,033
  • 1
  • 9
  • 20
  • Unfortunately, that did not work for me. I am still having the same issue: http://rpubs.com/beane/521165 . So, did the code I posted cause the chunk to split for you? That is strange if they both split the chunk for me, but only one of them split the chunk for you. – Beane Aug 21 '19 at 03:10
0

I had a similar issue with the chunk being split, and found a simple solution:

Just add echo=FALSE to the chunk properties (top part):

```{r chunk name, echo=FALSE}
<your code>
```

Or select "Show output only" from the chunk settings menu in RStudio:

enter image description here

This way the code is hidden and the outputs are displayed one after another.

Mahm00d
  • 3,881
  • 8
  • 44
  • 83