如何阻止ggplot2 facet复制变量?

时间:2012-09-28 18:01:56

标签: r ggplot2 facet

所以我使用ggplot2用我的数据制作一个漂亮的小图:

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)

我想将“控制”与“治疗”区分开来。我有点实现了这一点,但两者仍然在各个小组中表现出来:

enter image description here

如何避免这种情况?

干杯。

1 个答案:

答案 0 :(得分:6)

泰勒的评论是一个解决方案。但为什么将facet变量绘制为x变量?相反,我会使用Background作为你的x。

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