0

Let's say I have a 3 variables that were collected over 2 years

Var1 <- c(1:20)
Var2 <- c(41:60)
Var3<-c(81:100)
Date<- sample(seq(as.Date('2000/01/01'), as.Date('2002/12/31'), by="day"), 60)
data<-data.frame(Var1,Var2,Var3,Date)

I could like to create boxplots for each variable, combining by month, then plotting the months chronologically (Jan-Dec).

Right now my code looks like

boxplot(data$Var1 ~ reorder(format(data$Date,'%b %y'),data$Date))

or

boxplot(data$Var1~months(data$Date))

this gives me the full two years in chronological order, rather than just Months, or the months but in alphabetical order. Hope that makes sense! I am new to R but am learning ggplot as well.

1 Answers1

0

You have to reorder the month names proberly:

library(dplyr)
library(forcats)

data2 <- 
  data %>% 
  mutate(
    month = format(Date, "%m"),
    label = format(Date, "%b"),
    label = fct_reorder(label, month, min)
  )
boxplot(data2$Var1 ~ data2$label)
eastclintw00d
  • 2,250
  • 1
  • 9
  • 18