1

I am currently doing some variable exploration and have generated box plots for 3 different climate parameters (Tmin,Tmean,Tmax) I would like to know how to group those variables in one single box plot with a structure similar to this: enter image description here I've seen a couple tutorials online but they all require the grouping parameter to be assigned in the rows of the data frame not the head of the column. I tried adding +tmax as an argument inside tmin but that yielded errors. The code I have used to generate mine are is the following:

tmin<-  ggplot(prism, aes(x = factor(season, levels=c("spring","summer","fall","winter")), 
                                  y = tmin_c)) +
                geom_boxplot(fill = fill, colour = line, alpha = 0.7) +
                theme_bw() +
                scale_y_continuous(name = "Temperature C") +
                scale_x_discrete(name = "Season") +
                ggtitle("MRL Temperature 1980-2013") +
                theme(plot.title = element_text(hjust = 0.5))
tmin

enter image description here

Solved, here is the final working output for future refrence:

#Temperature
dat <- prism
dat <- dat %>%
       select(1,2,4,5,6) #1year,2season,4tmin,5tmean,6tmax

dat <- reshape2::melt(dat, measure.vars=3:5)


ggplot(dat, aes(y = value, 
                x = factor(season, levels=c("spring","summer","fall","winter")), 
                fill=factor(variable))) +
            geom_boxplot() +
            theme_bw() +
            scale_y_continuous(name = "Temperature C") +
            scale_x_discrete(name = "Season") +
            ggtitle("MRL Temperature 1980-2013") +
            theme(plot.title = element_text(hjust = 0.5))

Result: enter image description here

k3r0
  • 357
  • 1
  • 10
  • 3
    Try boxplot(prism$tmin_c~factor(prism$season, levels=c("Spring","Summer","Fall","Winter")), data=airquality, main="MRL TMin 1980-2013", xlab="Season", ylab="Tmin (C)", col="firebrick2", border="brown" ) – Mohanasundaram May 03 '20 at 03:46
  • Thanks! I had added the levels arguments incorrect and that was why it was not working. Any suggestions on adding additional box plots for tmean and tmax? – k3r0 May 03 '20 at 04:30
  • 1
    @k3r0 Does this answer your question: https://stackoverflow.com/a/47479817/6574038 ? – jay.sf May 03 '20 at 05:01
  • @jay.sf Kind of, I took the advice from the post and used the ggplot approach but I haven't been able to include each temperature variation within each season – k3r0 May 03 '20 at 05:54

1 Answers1

1

I refer to the original version of your code using airquality data.

Imagine Month is your season variable. First, reshape data into long format using reshape2::melt; your measure.vars would be t_min, t_max, ....

dat <- reshape2::melt(airquality, measure.vars=1:3)

summary(dat)
# Temp           Month            Day          variable       value       
# Min.   :56.00   Min.   :5.000   Min.   : 1.0   Ozone  :153   Min.   :  1.00  
# 1st Qu.:72.00   1st Qu.:6.000   1st Qu.: 8.0   Solar.R:153   1st Qu.: 10.30  
# Median :79.00   Median :7.000   Median :16.0   Wind   :153   Median : 24.00  
# Mean   :77.88   Mean   :6.993   Mean   :15.8                 Mean   : 80.86  
# 3rd Qu.:85.00   3rd Qu.:8.000   3rd Qu.:23.0                 3rd Qu.:136.00  
# Max.   :97.00   Max.   :9.000   Max.   :31.0                 Max.   :334.00  
#                                                              NA's   :44 

Second, use boxplot with : for the Month(i.e. your season)-factors.

at.key <- c(1:3, 5:7, 9:11, 13:15, 17:19)
# at.key <- (1:(5*4))[(1:(5*4)) %% ((5*4)/4-1) != 0]  ## alternatively

b <- boxplot(value ~ variable:Month, border=2:4, col="white",
             data=dat, at=at.key, xaxt="n",
             main="MRL TMin 1980-2013",
             xlab="Season",
             ylab="Tmin (C)")

mtext(b$names, 1, .5, at=at.key, cex=.8, las=2)

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 1
    That did it, I was not understanding the reshape2::melt step from other sources, thanks for clearing that up! – k3r0 May 03 '20 at 09:09