I hope the question is correctly stated. I would like to create, for didactic purposes, a knitr document where the outputs of the R base graphics system and the outputs of the Lattice package are compared in a panel. Consider a numeric vector:
```{r dataload}
e2 <- c(72, 79, 81, 80, 63, 62, 89, 90, 50, 78, 87, 97, 55, 69, 97, 87,
88, 99, 76, 78, 65, 77, 88, 90, 81)
```
I tried to produce a panel with the following code:
```{r plots}
par(mfrow = c(1,2)) #Set 1 row 2 columns panel
hist(e2) #Base graphic histogram on the left
histogram(~e2) #Lattice histogram on the right
```
But the Lattice histogram closes the panel, deleting the Base graphics histogram.
Expected result:
The panel should report the Base graph on a side and the Lattice graph on the other, similarly to the product of this really beautiful post. Unfortunately, the blog does not explain how the result is produced, and I did not find further information on the problem. I could easily produce two different images, but I think the panel representation produces a more beautifully comparable result
Thank you very much for your patience.
As discussed here on the R mailing list, the compatibility of lattice
and base R graphics is limited. The article that is referenced in this entry demonstrates that it can be done, but with some complications. However, this simple case is not worth the effort because a much simpler solution is to generate two independent plots and place them side by side.
This leads to the new question: How to place two figures side by side in Rmarkdown? There are some answers that explain how to place figures side by side with knitr
, but most of them refer to LaTeX/RNW documents. A possible solution for Rmarkdown is presented in this answer, but I'd like to suggest another, simple approach that doesn't involve adding custom CSS:
```{r mychunk, fig.show='hide'}
library(knitr)
library(lattice)
hist(iris$Sepal.Length)
histogram(iris$Sepal.Length)
```
base|lattice
-------------------------------|-------------------------------:
`r include_graphics(paste0(opts_chunk$get("fig.path"), "mychunk-1.png"))`|`r include_graphics(paste0(opts_chunk$get("fig.path"), "mychunk-2.png"))`
The chunk option fig.show = 'hide'
suppresses automatic printing of the figures. The figures are produced anyways and can then be inserted using include_graphics
in combination with the path to the figures directory obtained using opts_chunk$get("fig.path")
and the fact that knitr
names "images as fig.path-label-i
where i
is incremental from 1" (source: previous link).
The side by side layout is achieved using a Pandoc pipe_table
. This is convenient because the width of the images can be controlled by simply adding or removing dashes from the second row.
Note: In HTML documents, unless the YAML option self_contained
is false
, images are included using data URIs, not files. The solution above works nonetheless because at the point where knitr
processes the RMD file to MD, the images or not yet converted to data URIs; this occurs in the next step where Pandoc generates HTML from the MD file.