将相对丰度添加到文件名
Add relative abundance to file name
我下面的代码为我的数据集中的每个物种绘制了一个图,但我想知道是否有一种方法可以将相对丰度添加到每个文件名的开头。 (即,最丰富的物种文件“1_speciesA..”,第二丰富的物种是“2_speciesB..”,等等)。
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")
}
您可以像这样在 for 循环之前计算物种等级:
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
然后用排名值更新您的 ggsave()
文件名:
ggsave(
paste0(abund[abund$common_name == common,"rank"], "_", common, "_IBBEAM_trend_plot.png"),
height = 5, width=7, units = "in")
我下面的代码为我的数据集中的每个物种绘制了一个图,但我想知道是否有一种方法可以将相对丰度添加到每个文件名的开头。 (即,最丰富的物种文件“1_speciesA..”,第二丰富的物种是“2_speciesB..”,等等)。
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")
}
您可以像这样在 for 循环之前计算物种等级:
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
然后用排名值更新您的 ggsave()
文件名:
ggsave(
paste0(abund[abund$common_name == common,"rank"], "_", common, "_IBBEAM_trend_plot.png"),
height = 5, width=7, units = "in")