lundi 6 septembre 2021

Automating a ggplot for each level in a group

My apologies in advance, I wasn't sure how to recreate this dataframe here. My data is comprised of a column of fish counts with the corresponding when and where of each catch. Column 1 (col.1) is the common name of a fish, col.2 is the # of fish caught ("num"), col.3 is the year, col.4 is the season (dry or wet), and col.5 is the site (1-47).

I want to make a function that first summarizes the data of each species and then saves a png file plot for each unique species name (or print's to the screen if filename = NULL). I've gotten this far, but not sure what to fix it...

data <- read.csv('counts_data.csv')


GetMatrix=function(data) {
  cdata2 <- ddply(data, c("year", "season"), summarise,
                  N    = length(num),
                  n_mean = mean(num),
                  n_median = median(num),
                  sd   = sd(num),
                  se   = sd / sqrt(N))
  cdata2$year_season <- paste(cdata2$year, "_", cdata2$season, sep = "")
  cdata2 <- within(cdata2, year[year == 2005 & season == 'wet'] <- 2005.75)
  cdata2 <- within(cdata2, year[year == 2006 & season == 'wet'] <- 2006.75)
  cdata2 <- within(cdata2, year[year == 2007 & season == 'wet'] <- 2007.75)
  cdata2 <- within(cdata2, year[year == 2008 & season == 'wet'] <- 2008.75)
  cdata2 <- within(cdata2, year[year == 2009 & season == 'wet'] <- 2009.75)
  cdata2 <- within(cdata2, year[year == 2010 & season == 'wet'] <- 2010.75)
  cdata2 <- within(cdata2, year[year == 2011 & season == 'wet'] <- 2011.75)
  cdata2 <- within(cdata2, year[year == 2012 & season == 'wet'] <- 2012.75)
  cdata2 <- within(cdata2, year[year == 2013 & season == 'wet'] <- 2013.75)
  cdata2 <- within(cdata2, year[year == 2014 & season == 'wet'] <- 2014.75)
  cdata2 <- within(cdata2, year[year == 2015 & season == 'wet'] <- 2015.75)
  cdata2 <- within(cdata2, year[year == 2016 & season == 'wet'] <- 2016.75)
  cdata2 <- within(cdata2, year[year == 2017 & season == 'wet'] <- 2017.75)
  cdata2 <- within(cdata2, year[year == 2018 & season == 'wet'] <- 2018.75)
  cdata2 <- within(cdata2, year[year == 2019 & season == 'wet'] <- 2019.75)
  cdata2 <- within(cdata2, year[year == 2020 & season == 'wet'] <- 2020.75)
  cdata2 <- within(cdata2, year[year == 2005 & season == 'dry'] <- 2005.25)
  cdata2 <- within(cdata2, year[year == 2006 & season == 'dry'] <- 2006.25)
  cdata2 <- within(cdata2, year[year == 2007 & season == 'dry'] <- 2007.25)
  cdata2 <- within(cdata2, year[year == 2008 & season == 'dry'] <- 2008.25)
  cdata2 <- within(cdata2, year[year == 2009 & season == 'dry'] <- 2009.25)
  cdata2 <- within(cdata2, year[year == 2010 & season == 'dry'] <- 2010.25)
  cdata2 <- within(cdata2, year[year == 2011 & season == 'dry'] <- 2011.25)
  cdata2 <- within(cdata2, year[year == 2012 & season == 'dry'] <- 2012.25)
  cdata2 <- within(cdata2, year[year == 2013 & season == 'dry'] <- 2013.25)
  cdata2 <- within(cdata2, year[year == 2014 & season == 'dry'] <- 2014.25)
  cdata2 <- within(cdata2, year[year == 2015 & season == 'dry'] <- 2015.25)
  cdata2 <- within(cdata2, year[year == 2016 & season == 'dry'] <- 2016.25)
  cdata2 <- within(cdata2, year[year == 2017 & season == 'dry'] <- 2017.25)
  cdata2 <- within(cdata2, year[year == 2018 & season == 'dry'] <- 2018.25)
  cdata2 <- within(cdata2, year[year == 2019 & season == 'dry'] <- 2019.25)
  cdata2 <- within(cdata2, year[year == 2020 & season == 'dry'] <- 2020.25)
}



Plot<-function(data,species,fileName=NULL) {
  ggplot(cdata2, aes(x = year, y = n_mean, color = season)) +
  annotate(geom = "rect", xmin = 2010, xmax = 2010.5, ymin = -Inf, ymax = Inf,
           fill = "lightblue", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2013.5, xmax = 2014, ymin = -Inf, ymax = Inf,
           fill = "lightgreen", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2017.5, xmax = 2018, ymin = -Inf, ymax = Inf,
           fill = "#E0E0E0", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2011.5, xmax = 2012, ymin = -Inf, ymax = Inf,
           fill = "pink", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2015.5, xmax = 2016, ymin = -Inf, ymax = Inf,
           fill = "pink", colour = NA, alpha = 0.4) +
  annotate(geom = "rect", xmin = 2018.5, xmax = 2019, ymin = -Inf, ymax = Inf,
           fill = "orange", colour = NA, alpha = 0.4) +
  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_fill_manual(values=c("white", "#C0C0C0")) +   scale_x_continuous(breaks=c(2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018,2018,2019,2020)) +
  theme(panel.border = element_rect(fill = NA, color = "black"),
        panel.background = element_blank(), 
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
  labs(title="Blue crab (C. sapidus)",x="Year", y = "Mean count") +
  theme(plot.title = element_text(hjust = 0.5)) +
  theme(axis.text.y = element_text(size = 10, face = "bold")) +
  theme(axis.text.x = element_text(size = 10, face = "bold")) +
  theme(axis.title = element_text(size = 14, face = "bold"))

}

spSummary <- data %>% group_by(species) %>%
  summarize(total.number=sum(num)) %>%
  arrange(-total.number)
spSummary

splist<-spSummary$species


dataList<-list()
filenameVal<-paste0(1:length(splist),splist,"- IBBEAM_trend_plot.png")

setwd('C:/Users/...Trend plots')

for(run in 1:length(splist)) {
  dataList[[run]]<-GetMatrix(data,splist[run])
  Plot(data=dataList[[run]],splist[run],fileName=filenameVal[run])
  print(paste(run,splist[run]))
}

Aucun commentaire:

Enregistrer un commentaire