daattali / ggExtra

📊 Add marginal histograms to ggplot2, and more ggplot2 enhancements
http://daattali.com/shiny/ggExtra-ggMarginal-demo/
Other
383 stars 45 forks source link

Rendering inline in rstudio #89

Closed TarjinderSingh closed 6 years ago

TarjinderSingh commented 6 years ago

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!

daattali commented 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?
TarjinderSingh commented 6 years ago

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!

daattali commented 6 years ago

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

kevinushey commented 6 years ago

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.

daattali commented 6 years ago

@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

faustovrz commented 6 years ago

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)
```
daattali commented 6 years ago

@faustovrz Thanks for the info! @TarjinderSingh you can try what he suggested

daattali commented 6 years ago

This does indeed work and I added it to the README

TarjinderSingh commented 6 years ago

@daattali It works! Thanks!