rfor-loopggplot2uniqueggsave

Add relative abundance to file name


My code below makes a plot for each species in my dataset, but I was wondering if there was a way to add the relative abundance to the beginning of each file name. (i.e., file for most abundant species "1_speciesA..", 2nd most abundant species is "2_speciesB..", etc..).

data <- structure(list(year = c(2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                        2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 
                        2019, 2019, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
                        2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020
), season = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
                        1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("dry", 
                                                                                            "wet"), class = "factor"), site = structure(c(1L, 1L, 2L, 2L, 
                                                                                                                                          3L, 3L, 4L, 4L, 5L, 5L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 
                                                                                                                                          1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 1L, 1L, 2L, 2L, 3L, 3L, 
                                                                                                                                          4L, 4L, 5L, 5L), .Label = c("1", "2", "3", "4", "5"), class = "factor"), 
common_name = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                          1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 
                          2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
                          1L, 2L), .Label = c("Hardhead silverside", "Sailfin molly"
                          ), class = "factor"), num = c(0, 1, 0, 12, 0, 12, 0, 7, 0, 
                                                        13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
                                                        0, 0, 6, 0, 2, 0, 2, 0, 15, 0, 3, 0)), class = "data.frame", row.names = c(NA, 
                                                                                                                                   -40L))


allcommon <- unique(data$common_name)


#All species
for(common in allcommon){
  
  # Select species
  sp <- subset(data,common_name == common,
               select = c(year,
                          season,
                          site,
                          common_name,
                          num))
  
  cdata2 <- plyr::ddply(sp, c("year", "season"), summarise,
                        N    = length(num),
                        n_mean = mean(num),
                        n_median = median(num),
                        sd   = sd(num),
                        se   = sd / sqrt(N))
  
  cdata2 <-cdata2 %>% mutate(year=ifelse(season=="wet",year+0.5,year))
  
  ggplot(cdata2, aes(x = year, y = n_mean, color = season)) +
    geom_errorbar(aes(ymin=n_mean-se, ymax=n_mean+se), 
                  width=.2, 
                  color = "black") +
    geom_point(color = "black",
               shape = 21, 
               size = 3,
               aes(fill = season)) +
    scale_x_continuous(breaks=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2018,2019,2020)) +
    labs(x= NULL, y = "Mean count") +
    ggtitle(common)
  
  setwd('E:/.../Trend plots/Test')
  
  ggsave(paste0(common, "- IBBEAM_trend_plot.png"), 
         height = 5, width=7, units = "in")
}

Solution

  • You could calculate species ranks before your for-loop like this:

    abund <- aggregate(num ~ common_name, data = data, FUN = sum)
    abund <- abund[order(abund$num,decreasing = TRUE), ]
    abund$rank <- 1:nrow(abund)
    abund
    #>           common_name num rank
    #> 2       Sailfin molly  45    1
    #> 1 Hardhead silverside  28    2
    

    Then update your ggsave() filename with a rank value:

      ggsave(
        paste0(abund[abund$common_name == common,"rank"], "_", common, "_IBBEAM_trend_plot.png"), 
        height = 5, width=7, units = "in")