Bas Allema
Bas Allema

Reputation: 11

How to remove a group from a nested facet in R?

In the graph below I want to remove the empty groups. Please help me if you have an idea.

enter image description here

df = data.frame(cultivar = c("Fontane","Fontane","Fontane","Muse"), 
                 teeltnr = c("Laat","Laat","Vroeg","Vroeg"),
                 treat = c("ICM","Ref","Ref","ICM"),
                 obs = c(388, 390, 395, 346))

p = ggplot(df, aes(treat, obs, fill = treat)) + theme_classic() + 
  facet_nested(~ teeltnr + cultivar) +
  geom_bar(stat="identity") 

Upvotes: 1

Views: 257

Answers (1)

TarJae
TarJae

Reputation: 78937

You can use scales="free"

library(ggh4x)
df = data.frame(cultivar = c("Fontane","Fontane","Fontane","Muse"), 
                teeltnr = c("Laat","Laat","Vroeg","Vroeg"),
                treat = c("ICM","Ref","Ref","ICM"),
                obs = c(388, 390, 395, 346))

p = ggplot(df, aes(treat, obs, fill = treat)) + theme_classic() + 
  facet_nested(~ teeltnr + cultivar, scales="free") +
  geom_bar(stat="identity") 
p

enter image description here

Upvotes: 2

Related Questions