0

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.

Worice
  • 3,847
  • 3
  • 28
  • 49
  • Probably related: [combine lattice plot and standard R plot](http://r.789695.n4.nabble.com/combine-lattice-plot-and-standard-R-plot-td3496409.html#a3497717). However, for this specific question two independent figures, placed side by side, could be a workaround. What is the desired output format? – CL. Mar 03 '16 at 12:54
  • According to the material you kindly suggested, lattice and the base system are not compatible. There are limitations in compatibility between base and grid graphics (lattice is built on the latter). Hence, I think I cannot but try a different approach. Thank you very much for your help. – Worice Mar 03 '16 at 13:41
  • Well, according to the [article](http://cran.r-project.org/doc/Rnews/Rnews_2003-2.pdf) Mark Difford links to, combining lattice and base graphs is not impossible. But in your case, the most straightforward solution will be to generate two separate figures. – CL. Mar 03 '16 at 13:51
  • @CL. I agree. The effort is no worth the result, probably. – Worice Mar 03 '16 at 14:00

1 Answers1

1

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.

Community
  • 1
  • 1
CL.
  • 14,577
  • 5
  • 46
  • 73