rfor-loopggplot2ggtitle

Specifying different ggplot titles when using for loop to make multiple plots


I am using a loop to easily plot my data for different groups. I need a different plot for each cruise # (each year + season). I am having issues specifying a title that includes the Season and Year for each plot.

Example df (original data has more sites, species, cruises, dates, etc..)

structure(list(Cruise = c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 44L, 44L, 44L, 44L, 
44L, 44L, 44L, 44L, 44L), Season = c("Winter", "Winter", "Winter", 
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Summer", 
"Summer", "Summer", "Summer", "Summer", "Summer", "Summer", "Summer", 
"Summer", "Summer", "Summer", "Summer", "Summer", "Summer", "Summer", 
"Summer", "Summer", "Summer"), Year = c(2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 
2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 
2019L, 2019L, 2019L, 2019L, 2019L, 2019L), Taxon = c("Calanus finmarchicus", 
"Calanus finmarchicus", "Calanus finmarchicus", "Centropages typicus", 
"Centropages typicus", "Centropages typicus", "Temora longicornis", 
"Temora longicornis", "Temora longicornis", "Calanus finmarchicus", 
"Calanus finmarchicus", "Calanus finmarchicus", "Centropages typicus", 
"Centropages typicus", "Centropages typicus", "Temora longicornis", 
"Temora longicornis", "Temora longicornis", "Calanus finmarchicus", 
"Calanus finmarchicus", "Calanus finmarchicus", "Centropages typicus", 
"Centropages typicus", "Centropages typicus", "Temora longicornis", 
"Temora longicornis", "Temora longicornis"), Site = c(2L, 3L, 
5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L, 
2L, 3L, 5L, 2L, 3L, 5L, 2L, 3L, 5L), Density = c(0, 295.4030121, 
8032.665912, 14219.19758, 36113.01823, 20261.50059, 823.2167022, 
590.8060242, 59.945268, 27877.49705, 46128.12363, 25033.45344, 
341310.9774, 134047.5388, 11149.35321, 36918.84745, 37848.71683, 
140.2434366, 7684.064241, 10316.29105, 10720.82713, 89738.8931, 
17193.81842, 25152.7098, 30187.39523, 0, 0)), class = "data.frame", row.names = c(NA, 
-27L))

For loop for plots:

Cru <- unique(EXAMPLE$Cruise)

Cru_Dens <- list()

for(Cru_ in Cru) {
  Cru_Dens[[Cru_]] = ggplot(EXAMPLE %>% filter(Cruise == Cru_), 
                            aes(x = Site, 
                                y=Density, 
                                group = Taxon,
                                fill = Taxon)) +
    geom_bar(stat = "identity") +
    ggtitle(paste0(" ", EXAMPLE$Season , EXAMPLE$Year)) + #THIS IS WHAT I NEED HELP WITH
    labs(x="Site", caption = paste0("Cruise: ", Cru_)) +
    ylab(expression("Density (Ind \u00D7" ~ m^{2} ~ ")")) +
    theme_bw() +
    scale_fill_manual(values = mycolors) +
    theme(legend.position = "right",
          plot.title = element_text(hjust=0.5, face = "bold", 
                                    size = 15),
          legend.title = element_blank(),
          legend.text = element_text(size = 10),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          axis.title.x = element_text(size = 14,
                                      color = "black"),
          axis.title.y = element_text(size = 14,
                                      color = "black"),
          axis.text.x = element_text(color = "black", 
                                     size = 12, angle = 45, hjust = 1),
          axis.text.y = element_text(color = "black", 
                                     size = 12),
          axis.ticks.length = unit(0.2,"cm"),
          plot.caption.position =  "plot",
          plot.caption = element_text(size = 12)) +
    scale_y_continuous(expand = expansion(mult = c(0,0.1)))
  print(Cru_Dens[[Cru_]])
  ggsave(Cru_Dens[[Cru_]], file=paste0("CruiseSeason_Dens_bar_", Cru_, ".png"))
}

I am not sure how to set the title so it shows the season + year for each plot. The previous code keeps the same season + year for all plots

I also tried:

Season <- unique(EXAMPLE$Season)
Seas_l <- list()
Year <- unique(EXAMPLE$Year)
Year_l <- list()

with either:

ggtitle(paste0(" ", Seas_l , Year_l)) 

or

ggtitle(paste0(" ", Season , Year)) 

How can I have it display the correct title? For this example, cruise 17 should have "Summer 2018" as title; cruise 44 "Summer 2019"; etc..

I haven't been able to find the answer to this after searching for a while and I struggle quite a bit with loops. Happy to add additional information if this is not clear. Thank you.


Solution

  • There might be a better way to code this, but this would work:

    ggtitle(paste0(" ", filter(EXAMPLE, Cruise==Cru_)$Season[1] , " ", filter(EXAMPLE, Cruise==Cru_)$Year[1])) + #THIS IS WHAT I NEED HELP WITH