I want to remove the whole panel from ggplot
2-way facet_grid
. I have taken some lead from this question. Here is the code with example data
library(ggplot2)
library(ggh4x)
df <- data.frame(x=rnorm(80),y=rnorm(80),
col=rep(c(" ", "B","C"," ","B","C","B","C"),each=10),
row=rep(c("a","a","a","b","b","b","c","c"),each=10))
ggplot(data=df,aes(x=x,y=y))+
geom_point()+
facet_grid2(row~col, scales = "free", independent = "all")
I want to remove the left panel. How can it be done?
Noted from your comment that subsetting the dataset beforehand isn't an option for you, so here's a post-production approach:
# create plot
p <- ggplot(data=df,aes(x=x,y=y))+
geom_point()+
facet_grid2(row~col, scales = "free", independent = "all")
# convert to grob
gp <- ggplotGrob(p)
# check which are the columns of the gtable to strip out
# (for the example, we want to strip out content from cols 4-5)
gp # examine layout in console
gtable::gtable_show_layout(gp) # examine layout in viewer
# strip them out
gp <- gp %>%
cowplot::gtable_remove_grobs(names = gp$layout %>%
filter(r %in% 4:5) %>%
pull(name)) %>%
cowplot::gtable_squash_cols(cols = 4:5)
# plot the result
plot(gp)
data:
set.seed(123)
df <- data.frame(x=rnorm(80),y=rnorm(80),
col=rep(c(" ", "B","C"," ","B","C","B","C"),each=10),
row=rep(c("a","a","a","b","b","b","c","c"),each=10))