2012-05-15 88 views
5

我正在寻找一种方法来使用facet_gridggplot2中只绘制几个选择方面。说我有以下情节:只绘制facet_grid中的几个方面

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即使只是微调将是最受欢迎的。

+3

不知道你的 “分配在GGPLOT2” 的意思。当你将它传递给ggplot时,你这样做是为了将数据分类。 – joran

+0

嗯..我只是过了复杂的事情。非常感谢你。 –

回答

8

使用subsetggplot通话。

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

2

这会是好的,

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