rnorouzian
rnorouzian

Reputation: 7517

Don't show empty facets using facet_grid

In my plot below, I was wondering how to keep everything as is but just remove the empty plots?

library(tidyverse)
hsb <- read.csv('https://raw.githubusercontent.com/rnorouzian/e/master/hsb.csv')

hsb <- mutate(hsb, sector= factor(ifelse(sector==0,"pub","cath")))
hsb <- mutate(hsb, sector= relevel(sector, ref= "pub"))

nine <- subset(hsb, sch.id %in% unique(sch.id)[1:9]) 
nine$sch.id <- factor(nine$sch.id)
ggplot(nine) + aes(ses, math)+ geom_point() + facet_grid(sector~sch.id)+
  geom_smooth(method="lm",se=F, aes(color=sector))

enter image description here

Upvotes: 2

Views: 338

Answers (1)

zx8754
zx8754

Reputation: 56149

I think your faceting should based only on sch.id, try:

ggplot(nine, aes(ses, math)) +
  geom_point()+
  facet_grid(.~sch.id)+
  geom_smooth(method="lm",se=F, aes(color = sector))

enter image description here

Upvotes: 2

Related Questions