0

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")
}
Nate
  • 411
  • 2
  • 10
  • How are do we determine the relative abundance of a species? Is it based on the plots? I see there is a column called `num`, does that have to do with abundance? – neuron Sep 16 '21 at 17:52
  • I was going to suggest using summarize from dplyr, but didn't want to steer the conversation any one direction. I tried using this method: spSummary <- data %>% group_by(common_name) %>% dplyr::summarize(total.numbers=sum(num)) %>% arrange(-total.numbers) spSummary splist<-spSummary$common_name #Unique names to use in file naming – Nate Sep 16 '21 at 17:56
  • Species rank, I should say. – Nate Sep 16 '21 at 18:09

1 Answers1

2

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")
Skaqqs
  • 4,010
  • 1
  • 7
  • 21
  • I get this error: Error: Unknown graphics device '' Run `rlang::last_error()` to see where the error occurred. > rlang::last_error() Unknown graphics device '' Backtrace: 1. ggplot2::ggsave(...) 2. ggplot2:::plot_dev(device, filename, dpi = dpi) – Nate Sep 16 '21 at 18:26
  • I updated my answer a couples times to make it more clear. Try it again, please! – Skaqqs Sep 16 '21 at 18:27
  • Sorry, same error. Not sure what I'm doing wrong.. – Nate Sep 16 '21 at 18:39
  • I made a mistake adding `sep = "_"` to `paste0()`. That added a trailing underscore to the filename. I updated my answer again. Sorry about that. – Skaqqs Sep 16 '21 at 18:46