You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言如何为ggsave导出的绘图文件名添加物种相对丰度排序前缀

解决方法

你需要先对所有物种按总丰度降序排序并分配排名序号,再将序号拼接到导出文件名开头即可,修改后的完整代码如下:

# 加载所需依赖包
library(plyr)
library(dplyr)
library(ggplot2)

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))

# 新增步骤:统计每个物种总丰度,降序排序后分配排名序号
sp_rank <- data %>%
  group_by(common_name) %>%
  summarise(total_num = sum(num)) %>%
  arrange(desc(total_num)) %>%
  mutate(rank = row_number())

# 存储路径放在循环外,避免重复执行
setwd('E:/.../Trend plots/Test')

# 按丰度排序后的顺序循环处理每个物种
for(i in 1:nrow(sp_rank)){
  common <- sp_rank$common_name[i]
  rank_num <- sp_rank$rank[i]
  
  # 筛选对应物种数据
  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)) +
    # 原代码x轴刻度存在重复2018,已调整为匹配示例数据的刻度,可根据实际数据范围自行修改
    scale_x_continuous(breaks=c(2019,2020)) +
    labs(x= NULL, y = "Mean count") +
    ggtitle(common)
  
  # 文件名开头拼接排名序号
  ggsave(paste0(rank_num, "_", common, "- IBBEAM_trend_plot.png"), 
         height = 5, width=7, units = "in")
}

你提供的示例数据中Sailfin molly总丰度更高,会排在第一位,导出文件名为1_Sailfin molly- IBBEAM_trend_plot.png,Hardhead silverside总丰度更低,导出文件名为2_Hardhead silverside- IBBEAM_trend_plot.png,完全符合需求。

内容的提问来源于stack exchange,提问作者Nate

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 23:48:03