2015-06-09 39 views
0

我正嘗試在R中使用ggplot2來構造不同參數集在不同面板或構面上的boxplot。在我的輸入數據表中,我有一個包含參數分組的「process」列。但是,當我使用facet_wrap進行繪圖時,即使大部分數據都沒有數據,即使使用drop = T,也會爲每個方面顯示所有參數。ggplot2 boxplot facet wrap

下面是我的代碼,任何建議都會非常有幫助!

ggplot(stRep, aes(x=Parameter, y=value, fill=Disease), facets=process) + 
    geom_boxplot(outlier.shape=NA) + 
    ylab("Posterior distribution") + 
    xlab("Parameter") + theme_bw() + 
    scale_fill_grey() + coord_flip() + 
    ylim(-6, 10) + 
    facet_wrap(~ process, drop=T, ncol=1) 

附件是數據的一個子集:

> test[c(1:5, 39995:40005),] 
      value   Parameter Disease    process 
5001 -4.52611948 initial probability tree General parameters 
5002 6.73178928  pers.intercept tree   Persistence 
5003 6.00318901  pers.intercept tree   Persistence 
5004 -4.05923658 pers. nei. effect tree   Persistence 
5005 0.05733596 pers. nei. effect tree   Persistence 
39995 -0.10238927 col. tick effect corn Initial colonization 
39996 -0.12752092 col. tick effect corn Initial colonization 
39997 -0.17067746 col. tick effect corn Initial colonization 
39998 -0.06580708 col. tick effect corn Initial colonization 
39999 -0.13382417 col. tick effect corn Initial colonization 
40000 -0.12990795 col. tick effect corn Initial colonization 
40001 0.22196724 col. Lyme effect corn Initial colonization 
40002 0.24598469 col. Lyme effect corn Initial colonization 
40003 0.26436187 col. Lyme effect corn Initial colonization 
40004 0.23429840 col. Lyme effect corn Initial colonization 
40005 0.22931861 col. Lyme effect corn Initial colonization 
+0

是否使用'+ facet_wrap(〜process,scales =「free_x」,ncol = 1)'做你想做的事情? 'drop = T'意味着沒有數據的'process'級別沒有繪製方面。 – Stibu

回答

0

這將是更容易,如果您發佈的一些數據,以測試各種選項。

然而,這聽起來像你應該在你面前子集的數據圖:

stRep_no0s <- subset(stRep, value>0) 

隨後的情節,使用選項scales="free_x"作爲drop=T的替代品的建議通過Stibu:

ggplot(stRep_no0s, aes(x=Parameter, y=value, fill=Disease), facets=process) + 
    geom_boxplot(outlier.shape=NA) + 
    ylab("Posterior distribution") + 
    xlab("Parameter") + theme_bw() + 
    scale_fill_grey() + coord_flip() + 
    ylim(-6, 10) + 
    facet_wrap(~ process, scales="free_x", ncol=1) 
+0

是的,scales =「free_x」選項確實是我想使用的,但不適用於ggplot2中的coord_flip()選項。有沒有解決這個問題的方法? – ksw