Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make one panel blank in ggplot2

Tags:

r

ggplot2

p <- ggplot(mtcars, aes(mpg, wt)) 
p + geom_point()+facet_grid(cyl ~ vs)+theme_bw()

Pic

I would like to make the panel (1 vs 8 in the lower right hand corner) blank where no data points are shown, but at the same time, I would like to keep this arrangement.

so facet_wrap(cyl ~ vs) could not solve my problem.

maybe more general question is that is it possible to arrange each panel in ggplot2?

like image 731
user36102 Avatar asked Dec 12 '13 16:12

user36102


People also ask

How do you make a blank ggplot in R?

The two most easy ways to create an empty plot using ggplot2 are using geom_blank function and also adding the theme_bw along with the geom_blank.

Does par () work with ggplot?

The base R functions such as par() and layout() will not work with ggplot2 because it uses a different graphics system and this system does not recognize base R functionality for plotting. However, there are multiple ways you can combine plots from ggplot2 . One way is using the cowplot package.

What is Geom_blank?

geom_blank.Rd. The blank geom draws nothing, but can be a useful way of ensuring common scales between different plots. See expand_limits() for more details.

Why are my Ggplots blank?

Why does my plot show an empty axis or not display? it is likely that your graphics driver needs to be updated. You can update your display driver from Device Manager in Windows or from the vendors website.


2 Answers

You can do that, but not with facet_wrap (as far as I know). Create your seperate sub-plots. For a detailed step by step approach, see my answer here.

Create a blank plot & with the package gridExtra you can combine the plots:

library(gridExtra)
library(grid)

blank <- grid.rect(gp=gpar(col="white"))

grid.arrange(plot1, plot2, blank, plot3, ncol=2)

This approach will give you also a lot influence on the appearance of you final plot (IMHO).

like image 120
Jaap Avatar answered Oct 17 '22 05:10

Jaap


You change the table grobs.

## get the table grobs
g1 <- ggplot_gtable(ggplot_build(p))

library(gtable)
library(grid)
## here the main modification
## change one panel by a new rectangle.
pp <- gtable_add_grob(g1,rectGrob(gp=gpar(col=NA)),t=8,l=6,b=8,r=6)
grid.draw(pp)

enter image description here

like image 24
agstudy Avatar answered Oct 17 '22 07:10

agstudy