ggplot2 boxplot facet wrap

3k views Asked by At

I am trying to use ggplot2 in R to construct a boxplot with different parameter sets in different panels or facets. In my input datatable, I have a column "process" which contains the parameter groupings. When I plot using facet_wrap, though, all of the parameters are displayed for each facet, even though most have no data, even when I use drop=T.

Below is my code, any suggestions will be very helpful!

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)

Attached is a subset of data:

> 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
1

There are 1 answers

1
ErikJS On

It would be easier to test various options if you post some data.

It does however sound like you should subset your data before you plot:

stRep_no0s <- subset(stRep, value>0)

Then plot, using the options scales="free_x" as a substitute for drop=T as suggested by 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)