2016-02-22 30 views
1

難以使我的分面圖僅顯示數據,而不顯示沒有數據的分面。刪除空的分面類別

下面的代碼:

p<- ggplot(spad.data, aes(x=Day, y=Mean.Spad, color=Inoc))+ 
    geom_point() 

p + facet_grid(N ~ X.CO2.) 

提供了以下圖文: enter image description here

,我與它玩耍了一段時間,但似乎無法找出一個解決方案。

數據幀在這裏查看:https://docs.google.com/spreadsheets/d/11ZiDVRAp6qDcOsCkHM9zdKCsiaztApttJIg1TOyIypo/edit?usp=sharing

重複的例子,在這裏可見:https://docs.google.com/document/d/1eTp0HCgZ4KX0Qavgd2mTGETeQAForETFWdIzechTphY/edit?usp=sharing

+0

你可以包含一些示例數據,可能使用命令'dput(頭(spad.data))'。 – Mist

+0

不太清楚如何使用該函數,因爲我對R相當新穎。我共享了一個指向數據的鏈接。我希望這可以幫助 –

+0

請添加數據的問題。 – astrosyam

回答

2

你的問題在於你的x軸和y變量缺少的意見。這些不影響創建方面,這隻受到數據中存在的分面變量級別的影響。下面是一個使用樣本數據的說明:

#generate some data 
nobs=100 
set.seed(123) 
dat <- data.frame(G1=sample(LETTERS[1:3],nobs, T), 
        G2 = sample(LETTERS[1:3], nobs, T), 
        x=rnorm(nobs), 
        y=rnorm(nobs)) 
#introduce some missings in one group 
dat$x[dat$G1=="C"] <- NA 

#attempt to plot 
p1 <- ggplot(dat, aes(x=x,y=y)) + facet_grid(G1~G2) + geom_point() 
p1 #facets are generated according to the present levels of the grouping factors 

enter image description here

#possible solution: remove the missing data before plotting 
p2 <- ggplot(dat[complete.cases(dat),], aes(x=x, y=y)) + facet_grid(G1 ~G2) + geom_point() 
p2 

enter image description here