Closed TarjinderSingh closed 6 years ago
That's strange. I don't have any problems with ggMarginal in rmarkdown. Here's an example simple document:
---
output: html_document
---
```{r setup}
library(ggplot2)
library(ggExtra)
Sample ggMarginal plot:
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()
ggMarginal(p)
Can you show an example when it doesn't work?
Ah, when I run the example you have, it actually shows in the knited html document. However, when I am running the exact same script, it doesn't show inline as a rmarkdown notebook document. This is the figure that usually appears directly about the code chunk. If I add a plot(ggMarginal(p))
, then it shows but the theme is not quite right. Does this work for you too?
Thanks!
Oh I see what you mean. I'm not sure how RStudio renders plots there. The plot renders fine in the Viewer pane so I'm not sure why the notebook feature doesn't know how to render it. Maybe it works differently and requires some special properties in order to make a plot. Perhaps someone from RStudio (@kevinushey maybe?) could tell us what's the requirement to make a plot work in the rmarkdown notebook feature
To be honest, I'm not sure why this doesn't work in R Notebooks. R Notebooks work by generating a graphics device, letting any code write to that graphics device during chunk execution, and then figuring out what plots were generated at the end of execution.
My best guess is that the plots generated by ggExtra
need to explicitly create a new graphics device (with grid.newpage()
) before writing the plot.
@kevinushey yep that's a correct guess https://github.com/daattali/ggExtra/blob/4468f2113ecdd4d4fa2f8d380f843d2ac917b4db/R/ggMarginal.R#L191-L194
If you happen to know how to fix that please let us know. I'm fully aware that this week is super busy (as is any other week) so no worries if you can't get to it
From Henrik Seidel comment in:
https://support.rstudio.com/hc/en-us/community/posts/239529128-Notebooks-grid-newpage
First store the ggmarginal plot as a variable without actual plotting, and then use grid.newpage() and grid.draw() in a new chunk:
```{r}
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()
p <- ggMarginal(p)
```
```{r}
grid.newpage()
grid.draw(p)
```
@faustovrz Thanks for the info! @TarjinderSingh you can try what he suggested
This does indeed work and I added it to the README
@daattali It works! Thanks!
Hello!
When I use ggMarginal with knitr in rmarkdown, it appears that the plot does not show inline or in the knited html document. However, when I run the ggMarginal code in the Console, it appears. Are there any settings I need to tweak to make it appear with rmarkdown?
Thanks!