Home > Mobile >  ggplot2 syntax for grouping boxplots by month?
ggplot2 syntax for grouping boxplots by month?

Time:06-18

I have a long dataset with two primary pieces of information - collection date and results. The collection date is formatted as.Date(df, format ="%Y-%m-%d"). I am looking to create boxplots of this data, grouped by month. Using ggplot2 I can create a scatterplot and format the axis by month, but am unsuccessful at changing it to boxplots. Do I need to transform my data to group the boxplots by month? What is the syntax for the aes(group=...) function in ggplot2?

Thank you!

ggplot(Results, aes(x = COLDATE, y = `PB(ug/L)`, group=...))  
    coord_trans(y = "log10")  
    geom_boxplot(alpha = 0.5)  
    xlab("Collection Date")  
    scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y")  
    theme(axis.text.x=element_text(angle=60, hjust=1))

Screenshot of part of the data.

Scatterplot of Results

Unsuccessful boxplot of data

CodePudding user response:

Here's an example:

Results = data.frame(COLDATE = as.Date("2020-01-01")   1:1000,
                     PB = runif(1000, min = 0.3, max = 18)^3)

library(tidyverse); # or `library(ggplot2); library(dplyr)` or use base to add the year beforehand
ggplot(Results %>% mutate(year = lubridate::year(COLDATE)), 
       aes(x = COLDATE, y = PB, 
           group = lubridate::floor_date(COLDATE, "month")))  
  geom_boxplot(alpha = 0.5)  
  labs(x = "Collection Date", y = "PB(ug/L)")  
  scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y")  
  scale_y_continuous(trans = scales::log10_trans(),
                     breaks = 10^(-20:20),
                     labels = scales::label_number_si())  
  theme(axis.text.x=element_text(angle=60, hjust=1))  
  facet_wrap(~year, nrow = 1, scales = "free_x")

enter image description here

Or alternatively:

ggplot(Results %>% 
         mutate(year = lubridate::year(COLDATE),
                month = lubridate::month(COLDATE, label = TRUE)), 
       aes(x = as.factor(year), y = PB, 
           group = year))  
  geom_boxplot(alpha = 0.5)  
  labs(x = "Collection Date", y = "PB(ug/L)")  
  scale_y_continuous(trans = scales::log10_trans(),
                     breaks = 10^(-20:20),
                     labels = scales::label_number_si())  
  theme(axis.text.x=element_text(angle=60, hjust=1))  
  facet_wrap(~month, nrow = 1, scales = "free_x")

enter image description here

  • Related