I'm working with time series data for species counts that I need to plot and save to individual folders that have already been created. My code makes 1 plot for each species in order of species rank (ranking of most abundant to least). This is also the naming scheme of each plot file and the folders. I need help telling R to save each plot to the right folder. The for-loop is taken from my other script for making heatmaps. I've tried to adapt it for plotting my time series code, but I get this error:
Error in getPlot1(data = dataList[[run]], folder_name[run], fileName = paste0(file.path(folder_name[run]), :
unused arguments (folder_name[run], fileName = paste0(file.path(folder_name[run]), "/", filenameVal[run]))
My code:
library(ggprism)
library(dplyr)
library(ggplot2)
setwd('C:/Users/.../CSV')
dat <- read.csv('Epifauna - Zero_counts_&_Env._data.csv')
make_summary=function(data,spToSum) {
newdat <- filter(data,common_name %in% spToSum)
cdata2 <- plyr::ddply(newdat, c("CYR", "Season"), summarise,
N = length(num),
n_mean = mean(num),
n_median = median(num),
sd = sd(num),
se = sd / sqrt(N))
cdata2 <- cdata2 %>% mutate(CYR=ifelse(Season=="WET",CYR+0.5,CYR))
}
interleave <- function(x,y){
lx <- length(x)
ly <- length(y)
n <- max(lx,ly)
as.vector(rbind(rep(x, length.out=n), rep(y, length.out=n)))
}
CYR <- seq(2005,2021.5,0.5)
labs <- interleave(seq(2005,2021,by=1), "")
getPlot1<-function(data) {
ggplot(data, aes(x = CYR, y = n_mean, color = Season)) +
annotate(geom = "rect", xmin = 2010, xmax = 2010.25, ymin = -Inf, ymax = Inf,
fill = "lightblue", colour = NA, alpha = 0.4) +
annotate(geom = "rect", xmin = 2013.5, xmax = 2013.75, ymin = -Inf, ymax = Inf,
fill = "lightgreen", colour = NA, alpha = 0.4) +
annotate(geom = "rect", xmin = 2017.5, xmax = 2017.75, ymin = -Inf, ymax = Inf,
fill = "#E0E0E0", colour = NA, alpha = 0.4) +
annotate(geom = "rect", xmin = 2011.5, xmax = 2011.75, ymin = -Inf, ymax = Inf,
fill = "pink", colour = NA, alpha = 0.4) +
annotate(geom = "rect", xmin = 2015.5, xmax = 2015.75, ymin = -Inf, ymax = Inf,
fill = "pink", colour = NA, alpha = 0.4) +
annotate(geom = "rect", xmin = 2018.5, xmax = 2018.75, ymin = -Inf, ymax = Inf,
fill = "orange", colour = NA, alpha = 0.2) +
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)) +
ylim(0, NA) +
scale_fill_manual(values=c("white", "#C0C0C0")) +
scale_x_continuous(breaks= CYR, labels = labs) +
theme(panel.border = element_rect(fill = NA, color = "black"),
panel.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
labs(x= NULL, y = "Mean count") +
ggtitle(common) +
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 = 8, face = "bold")) +
theme(axis.title = element_text(size = 14, face = "bold"))
}
spSummary<- dat %>% group_by(common_name) %>%
summarize(total.number=sum(num,na.rm=TRUE),
observations=length(num[num>0 &!is.na(num)])) %>%
arrange(-total.number)
splist<-spSummary$common_name[spSummary$observations>=1]
length(splist)
setwd('.../Epifauna') # Location of 154 folders with naming scheme
# Folder names: "1. Rainwater Killifish", "2. Zostera Shrimp", etc...
# Number preceding name is abundance rank
dataList<-list()
filenameVal<-paste0(1:length(splist),". ",splist," - time_series_plot.png")
folder_name<-paste0(1:length(splist),". ",splist)
for(run in 1:length(splist)) {
dataList[[run]]<-make_summary(dat,splist[run])
# dir.create(file.path(folder_name[run]), recursive = TRUE) # Code that created folders
getPlot1(data=dataList[[run]],folder_name[run], fileName=paste0(file.path(folder_name[run]), "/", filenameVal[run]))
print(paste(run,splist[run]))
}
Data (CYR = Calendar year 2005-2021, Season = DRY/WET):
> head(dat, 10)
CYR Season common_name num
1 2005 DRY Sergeant Major 10
2 2005 DRY Lined Sole 0
3 2005 DRY Bonefish 0
4 2005 DRY Snapping Shrimp (A. angulosus) 6
5 2005 DRY Snapping Shrimp (A armillatus) 0
6 2005 DRY Snapping Shrimp (A carlae) 0
7 2005 DRY Estuarine Snapping Shrimp 1
8 2005 DRY Bigclaw Snapping Shrimp 0
9 2005 WET Grass Shrimp (A normanni) 1
10 2006 WET Grass Shrimp (A nuttingi) 0