2013-07-26 20 views
3

喜有此數據集:爲什麼這個facet_grid不會刪除列?

tdat=structure(list(Condition = structure(c(1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L), .Label = c("AS", "Dup", "MCH"), class = "factor"), 
    variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
    3L, 3L, 3L), .Label = c("Bot", "Top", "All"), class = "factor"), 
    value = c(1.782726022, 1, 2.267946449, 1.095240234, 1, 1.103630141, 
    1.392545278, 1, 0.854984833, 4.5163067, 1, 4.649271897, 0.769428018, 
    1, 0.483117123, 0.363854608, 1, 0.195799358, 0.673186975, 
    1, 1.661568993, 1.174998373, 1, 1.095026419, 1.278455823, 
    1, 0.634152231)), .Names = c("Condition", "variable", "value" 
), row.names = c(NA, -27L), class = "data.frame") 

> head(tdat) 
    Condition variable value 
1  AS  Bot 1.782726 
2  MCH  Bot 1.000000 
3  Dup  Bot 2.267946 
4  AS  Bot 1.095240 
5  MCH  Bot 1.000000 
6  Dup  Bot 1.103630 

您可以繪製它這樣使用此代碼:

ggplot(tdat, aes(x=interaction(Condition,variable,drop=TRUE,sep='-'), y=value, 
       fill=Condition)) + 
       geom_point() + 
       scale_color_discrete(name='interaction levels')+ 
           stat_summary(fun.y='mean', geom='bar', 
       aes(label=signif(..y..,4),x=as.integer(interaction(Condition,variable))))+ 
           facet_grid(.~variable) 

enter image description here

但你可以看到它並不會刪除未使用的列每個方面,你知道爲什麼嗎?

回答

5

由於使用了所有級別,因此可以獲得圖上顯示的所有級別。如果完全不使用它們,級別將被丟棄。要刪除每個方面的級別,請將scale="free_x"添加到facet_grid()。但是這在特定情況下不起作用,因爲您在調用ggplot()stat_summary()時使用了x值的不同陳述。我建議在繪製交互之前添加新列。

tdat$int<-with(tdat,interaction(Condition,variable,drop=TRUE,sep='-')) 
ggplot(tdat,aes(int,value,fill=Condition))+ 
    stat_summary(fun.y='mean', geom='bar')+ 
    geom_point()+ 
    facet_grid(.~variable,scales="free_x") 

enter image description here

在這種情況下,因爲你也可以使用facet_grid()您可以簡化代碼,無需interaction()

ggplot(tdat,aes(Condition,value,fill=Condition))+ 
    stat_summary(fun.y='mean', geom='bar')+ 
    geom_point()+ 
    facet_grid(.~variable) 

enter image description here

+0

哦,好了,這是一個錯誤嗎?假設我願意使用interaction()而不是facet_grid(),你會知道如何糾正覆蓋問題嗎? – Wicelo

+0

已更新我的回答,以顯示您的代碼的實際問題和級別下降。 –

+0

@Wicelo這不是一個錯誤。 – joran

相關問題