wilkelab / cowplot

cowplot: Streamlined Plot Theme and Plot Annotations for ggplot2
https://wilkelab.org/cowplot/
704 stars 84 forks source link

Blank plot when using cowplot with RNotebook #82

Closed DanielReedOcean closed 6 years ago

DanielReedOcean commented 6 years ago

When using cowplot with RNotebook an additional blank plot is created before the requested plot. For example,

---
title: "R Notebook"
output: html_notebook
---

```{r}
library(cowplot)
library(ggplot2)
g <- ggplot(mtcars) + geom_point(aes(mpg, hp))
plot_grid(g,g)

produces the following:

![capture](https://user-images.githubusercontent.com/11167566/33444987-0f9da026-d5d2-11e7-8221-098adb02f0dd.PNG)
 Note that this **doesn't** happen if the file is knitted as a `html_document` rather than `html_notebook`.
clauswilke commented 6 years ago

Could you try the following, please? Replace this line:

plot_grid(g,g)

with these three lines:

grDevices::pdf(NULL)
plot_grid(g,g)
grDevices::dev.off()

and see if this prevents the problem. If it does, we may be able to put this workaround into the plot_grid() function.

DanielReedOcean commented 6 years ago

When I do that there are no plots at all. Instead, I get the following:

null device 
          1 
clauswilke commented 6 years ago

That's good news, because I gave you the wrong code. This should work then.

grDevices::pdf(NULL)
g2 <- plot_grid(g,g)
grDevices::dev.off()
g2
DanielReedOcean commented 6 years ago

Boom! No blank plot. capture2

clauswilke commented 6 years ago

Yeah, I think I always need to open a null pdf device when converting plots, not just when one is already open. The problem is I vaguely remember that somebody objected to that. I think in some cases the pdf device is not available. I may just make this change and see if somebody complains.

DanielReedOcean commented 6 years ago

Okay, thanks for getting this figured out so quickly.

clauswilke commented 6 years ago

I committed a fix that should resolve the issue for you and will hopefully not introduce other pains. Can you install the github version of cowplot and see if things work now?

DanielReedOcean commented 6 years ago

It looks great! Thanks.

clauswilke commented 6 years ago

Closing this. It seems to work now.