仅绘制facet_grid中选择的几个面

时间:2012-05-15 20:11:27

标签: r ggplot2

我一直在寻找一种方法,使用facet_grid ggplot2来绘制,只显示几个选择方面。说我有以下情节:

enter image description here

一直在寻找一种快速方法,例如,只绘制方面1和3。

#data
y<-1:12
x<-c(1,2,3,1,2,3,1,2,3,1,2,3)
z<-c("a","a","a","b","b","b","a","a","a","b","b","b")
df<-as.data.frame(cbind(x,y,z))

#plot

a <- ggplot(df, aes(x = z, y = y,
  fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c

显然,我想出了如何首先删除我的数据,但当然必须在ggplot2进行分配。即使只是轻推也是最受欢迎的。

2 个答案:

答案 0 :(得分:10)

subset来电中使用ggplot

plot_1 = ggplot(subset(df, x %in% c(1, 2)), aes(x=z, y=y, fill=z)) +
         geom_bar(stat = "identity", position = "dodge") +
         facet_grid(. ~ x, scale = "free_y")

enter image description here

答案 1 :(得分:3)

这样可以吗,

a <- ggplot(subset(df, x != 2), aes(x = z, y = y, fill = z))
b <- a + geom_bar(stat = "identity", position = "dodge")
c <- b + facet_grid(. ~ x, scale = "free_y")
c