fridaymeetssunday
fridaymeetssunday

Reputation: 1148

How to stop ggplot2 facet duplicating variable?

So I am using ggplot2 to make a nice little plot with my data:

df1 <- data.frame(Background = factor(c("Input", "H3", "Overlap","Input", "H3", "Overlap"), levels=c("Input", "H3", "Overlap")), 
            Condition = factor(c("control", "control", "control","treatment", "treatment", "treatment")),
            Count = c(10, 9, 5, 8, 7, 6))

barplot = ggplot(data=df1, aes(x=Condition, y=Count, fill=Background)) +
    geom_bar(position=position_dodge()) +
    facet_grid(. ~ Condition)

And I want to separate "control" from "treatment". I sort of achieve that but both are still represented in the individual panels:

enter image description here

How to avoid this?

Cheers.

Upvotes: 1

Views: 756

Answers (1)

Justin
Justin

Reputation: 43265

Tyler's comment is one solution. But why plot the facet variable as the x variable? Instead, I would use Background as your x.

barplot = ggplot(data=df1, aes(x=Background, y=Count, fill=Background)) +   
  geom_bar(position='dodge') + 
  facet_grid(.~Condition)

Upvotes: 6

Related Questions