2015-06-09 63 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