Last Updated
Viewed 1,117 Times

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

I would like to group data into multiple seasin such that my season are winter: Dec - Feb; Spring: Mar - May; Summer: Jun -Aug, and Fall: Sep - Nov. I would then like to boxplot the Winter and Spring seasonal data comparing A to B and then A to C. Here is my laborious code so far. I would appreciate an efficient way of data grouping and plotting.


Dates30s = data.frame(seq(as.Date("2011-01-01"), to= as.Date("2040-12-31"),by="day"))
colnames(Dates30s) = "date"
FakeData = data.frame(A = runif(10958, min = 0.5, max = 1.5), B = runif(10958, min = 1.6, max = 2), C = runif(10958, min = 0.8, max = 1.8))
myData = data.frame(Dates30s, FakeData)
myData = separate(myData, date, sep = "-", into = c("Year", "Month", "Day"))
myData$Year = as.numeric(myData$Year)
myData$Month = as.numeric(myData$Month)

SeasonalData =  myData %>%  group_by(Year, Month) %>% summarise_all(funs(mean)) %>% select(Year, Month, A, B, C)
Spring = SeasonalData %>% filter(Month == 3 | Month == 4 |Month == 5)
Winter1 = SeasonalData %>% filter(Month == 12)
Winter1$Year = Winter1$Year+1
Winter2 = SeasonalData %>%  filter(Month == 1 | Month == 2 )
Winter = rbind(Winter1, Winter2) %>%  filter(Year >= 2012 & Year <= 2040) %>% group_by(Year) %>% summarise_all(funs(mean)) %>% select(-"Month")
BoxData = gather(Winter, key = "Variable", value = "value", -Year )

ggplot(BoxData, aes(x=Variable, y=value,fill=factor(Variable)))+
  geom_boxplot() + labs(title="Winter") +facet_wrap(~Variable) 

I would like to have Two figures: Figure 1 split in two; one for Winter season and one for Summer season (see BoxPlot 1) and one for Monthly annual average representing average monthly values across the entire time period (2011 -2040) see Boxplot 2 Boxplot_1 Boxplot_2

I am trying to create a boxplot where I can group one variable by the levels of a second variable. I have worked out how to facet wrap, but how do I get all the bars on the same plot?

I need to create the following:

x-axis: Time period (TP) - 4 levels, 1 -4.

y-axis: CV (coefficient of variation data).

Boxplots: 4 boxplots to appear at each level of the x-axis. Split accross 4 levels of the second factor, CS (calf status, categorical, 1 - 4)

I can create split boxplots using facet_wrap using the following code:

p10 <- ggplot(, aes(x = as.factor(TP), y=CV)) +
  geom_boxplot() +
  scale_x_discrete(name = "Time period") +
  scale_y_continuous(name = "Coefficient of variation (CV)\nof adult females' association indicies") +
  ggtitle("CV adult females' association indicies by time period \n split out by calf status, 1 - 4 ") +
  theme(plot.title = element_text(hjust = 0.5)) +

But how do I get this info all on a single boxplot?

I am trying to make it look like this:

enter image description here

Similar Question 3 : Use of facet_wrap in ggplot2

Want facet_wrap to have different parameters for each plot. Example below:

x = c(43,22,53,21,13,53,23,12,32)
y = c(42,65,23,45,12,22,54,32,12)

df = cbind(x,y)

df =  

meany = mean(y)

p = ggplot(df, aes(x=x,y=y, colour=(y > meany))) + 
      geom_point() +
      geom_hline(yintercept = meany)

That works fine, there is a line at mean of y and the points are different colors above and below the line.

I have a larger data frame where I want to do this to each factor level and use facet_wrap to show all the plots. I am not sure how to get the colour and yintercept in ggplot to change for every graph in facet_wrap.

Also, I want the plot to have more layers, i.e. each plot will be comparing MSE of different models.

Thanks for the help.

Similar Question 5 (3 solutions) : Place a legend for each facet_wrap grid in ggplot2

Similar Question 6 (2 solutions) : R ggplot2 two-level facet wrap

Similar Question 8 (1 solutions) : ggplot2: control panel placement in facet_wrap

Similar Question 9 (1 solutions) : How to use geom_errorbar with facet_wrap in ggplot2