R: Using dplyr group_by with ggplot2 and changing title per group

Viewed 73

I have the following example dataframe df_summarised which in reality will have more years and Isotope data.

structure(list(Isotope = c("TC99M", "TC99M", "TC99M", "TC99M", 
"TC99M", "TC99M", "TC99M", "TC99M", "TL201", "TL201", "TL201", 
"TL201", "TL201", "TL201", "TL201", "TL201"), month = structure(c(1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("Jan", 
"Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", 
"Nov", "Dec"), class = c("ordered", "factor")), year = c(2021, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, 2021, 2021), TotalMonthlyExcretion = c(17705.648, 
20717.156, 27443.508, 19528.556, 22711.792, 20083.524, 17791.076, 
9223.8, 163.695, 738.51, 1055.775, 906.438, 482.082, 741.984, 
605.211, 265.641), MonthlyLimits_MBq = c(80000, 80000, 80000, 
80000, 80000, 80000, 80000, 80000, 5600, 5600, 5600, 5600, 5600, 
5600, 5600, 5600)), .Names = c("Isotope", "month", "year", "TotalMonthlyExcretion", 
"MonthlyLimits_MBq"), row.names = c(NA, -16L), class = "data.frame")

I want to create an individual plot for each isotope, year combination and save as a separate png file. For each isotope/year pairing, I want to change the title to reflect the pairing. I have tried the following:

ActivityPlots <- df_summarised %>%
    group_by(Isotope,year) %>%
    do(
       plots = ggplot(data=., aes(x=month,y=TotalMonthlyExcretion)) +
       geom_bar(stat="identity", width = .5, fill="tomato3") +
       labs(title = paste("Liquid Radioactive Waste. Year: ", df_summarised$year," Isotope: ", df_summarised$Isotope),
           x = "Month",
           y = "Total Monthly Excretion (MBq)",
           caption = paste("Year: ",df_summarised$year)
           ) +
       theme_bw() +
       theme(
            plot.title = element_text(color="black", size=14, face="bold.italic"),
            axis.title.x = element_text(size=14, face="bold"),
            axis.title.y = element_text(size=14, face="bold"))
       )

However, the plot title for each plot is the same: 'Liquid Radioactive Waste. Year 2021 Isotope TC99M'. I'm assuming this is picking up the values from the first entry of the data frame.

What is the best way around this?

1 Answers

Use split and map. To save the plots as png you can include ggsave.

library(tidyverse)

df_summarised %>%
  group_split(Isotope,year) %>%
  map(~{
    plots = ggplot(data=.x, aes(x=month,y=TotalMonthlyExcretion)) +
      geom_bar(stat="identity", width = .5, fill="tomato3") +
      labs(title = paste("Liquid Radioactive Waste. Year: ", .x$year[1]," Isotope: ", .x$Isotope[1]),
           x = "Month",
           y = "Total Monthly Excretion (MBq)",
           caption = paste("Year: ",.x$year[1])
      ) +
      theme_bw() +
      theme(
        plot.title = element_text(color="black", size=14, face="bold.italic"),
        axis.title.x = element_text(size=14, face="bold"),
        axis.title.y = element_text(size=14, face="bold"))
    
    ggsave(sprintf('plots%s_%s.png', .x$Isotope[1], .x$year[1]), plots)
  })
Related