6

I am using R Notebooks and have a chunk with some code for arranged histograms. When I use a common legend it produces an extra empty plot, which looks terrible in the rendered html file. This phenomenon disappears without a shared legend, but the plot looks terrible since they are not of the same size. Is there any way to stop it from producing an extra empty graph?

Notebook chunk output

and the code being used in the chunk

 ```{r}

ggarrange(

gghistogram(data, title="MOTIVATION SCORES", x="MOTIVATION", y="..density..", 
add_density=TRUE, add = "median", rug = TRUE, bins=15, color="#69c8ECFF", 
fill="#69c8ECFF") , 


gghistogram(data, title="MOTIVATION BY AGE GROUP", x = "MOTIVATION", 
y="..density..", add_density=TRUE,
          add = "median", rug = TRUE, bins=15,
          color = "AGE_GROUP", fill = "AGE_GROUP",
          palette = c("#69c8ECFF", "#E762D7FF")
          )

, legend = "bottom" 
, common.legend = TRUE

)
```
bdemarest
  • 14,397
  • 3
  • 53
  • 56
BKV
  • 223
  • 3
  • 4

3 Answers3

1

Edit: If you run the 2nd block below as a block of code in an rmarkdown document, it still generates the extra blank plot. If you run each line of the 2nd block manually (i.e. one at a time) it generates just the single desired plot. I think this still counts as a solution though because even running the first code block one at a time results in the extra blank plot.

This seems to reproduce the problem when run in an rnotebook:

p1 = ggplot(mtcars, aes(x = mpg, y = cyl)) +
    geom_point()
p2 = ggplot(mtcars, aes(x = drat, y = vs)) +
    geom_point()
ggarrange(p1, p2, ncol = 2, nrow = 1, common.legend = TRUE, legend = "bottom", labels = c("A", "B", "C"))

If instead I assigned the ggarrange object to p, then the problem goes away:

p1 = ggplot(mtcars, aes(x = mpg, y = cyl)) +
    geom_point()
p2 = ggplot(mtcars, aes(x = drat, y = vs)) +
    geom_point()
p = ggarrange(p1, p2, ncol = 2, nrow = 1, common.legend = TRUE, legend = "bottom", labels = c("A", "B", "C"))
p

No idea why. Highly unsatisfactory for me, but it seems to work.

buggaby
  • 359
  • 2
  • 13
  • 1
    if you are creating the plots with ggplot syntax, I'd recommend using the patchwork package instead of ggarrange. – tjebo Jun 30 '20 at 19:31
  • 1
    if `p` is in a separate code chunk, then no blank plot is produced – mac Feb 17 '21 at 14:40
  • This is now fixed in the latest dev version of ggpubr package, which can be installed using `devtools::install_github("kassambara/ggpubr")` – A. Kassambara Dec 04 '22 at 21:36
1

You can fix the problem by opening a null pdf device

p1 <-gghistogram(data, title="MOTIVATION SCORES", x="MOTIVATION", y="..density..", 
add_density=TRUE, add = "median", rug = TRUE, bins=15, color="#69c8ECFF", 
fill="#69c8ECFF") , 

p2 <- gghistogram(data, title="MOTIVATION BY AGE GROUP", x = "MOTIVATION", 
y="..density..", add_density=TRUE,
          add = "median", rug = TRUE, bins=15,
          color = "AGE_GROUP", fill = "AGE_GROUP",
          palette = c("#69c8ECFF", "#E762D7FF")
          )


pdf(NULL)
res <- ggarrange(p1,p2, legend = "bottom", common.legend = TRUE)
dev.off()
res
0

Instead of ggarrange you may consider switching over to patchwork. It does not have that "white page" issue and has a nice syntax.

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(x=mpg,y=disp)) + geom_point() + ggtitle("plotA")
p2 <- ggplot(mtcars, aes(x=mpg,y=qsec)) + geom_point() + ggtitle("plotB")
p3 <- ggplot(mtcars, aes(x="cars", y=hp)) + geom_boxplot() + ggtitle("plotC")

#/ Lets make P1 and P2 share a column, and give P3 its own column:
p1 / p2 | p3

enter image description here

ATpoint
  • 603
  • 5
  • 17