2012-05-15 84 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