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

生态学实习:如何按植物物种拆分绘制种子萌发率折线图

实现建议

R 环境(生态学常用工具链)

假设你的数据框名为germ_data,包含Species、Soil_origin、Day_since_cooling、Proportion_germination四列。

快速生成多子图预览

用ggplot2的facet_wrap可一键拆分所有物种的图表,适合快速查看整体趋势:

library(ggplot2)

ggplot(germ_data, aes(x = Day_since_cooling, y = Proportion_germination, color = Soil_origin)) +
  geom_line(linewidth = 1) + # 调整线条粗细增强可读性
  facet_wrap(~Species, scales = "free_y") + # 每个物种单独成图,y轴自适应范围
  labs(x = "冷却后天数", y = "萌发率比例", color = "土壤类型") +
  theme_bw()

批量生成独立图表并保存

如果需要每个物种的单独文件,用purrr批量处理:

library(ggplot2)
library(purrr)
library(dplyr)

# 按物种拆分数据框
species_data_list <- split(germ_data, germ_data$Species)

# 遍历每个物种数据,绘图并保存
walk(species_data_list, function(species_df) {
  species_name <- unique(species_df$Species)
  plot <- ggplot(species_df, aes(x = Day_since_cooling, y = Proportion_germination, color = Soil_origin)) +
    geom_line(linewidth = 1) +
    labs(x = "冷却后天数", y = "萌发率比例", color = "土壤类型",
         title = species_name) +
    theme_bw() +
    theme(plot.title = element_text(hjust = 0.5)) # 标题居中
  
  # 保存为PNG文件,文件名以物种名命名
  ggsave(paste0(species_name, "_germination_trend.png"), plot, width = 8, height = 6, dpi = 300)
})

Python 环境

若使用Python分析,基于pandas+seaborn实现:

快速生成多子图预览

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# 假设数据框为germ_df
g = sns.FacetGrid(germ_df, col="Species", col_wrap=2, sharey=False)
g.map(sns.lineplot, "Day_since_cooling", "Proportion_germination", "Soil_origin", marker="o")
g.add_legend(title="土壤类型")
g.set_axis_labels("冷却后天数", "萌发率比例")
plt.tight_layout()
plt.show()

批量生成独立图表并保存

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt

# 获取所有唯一物种
unique_species = germ_df["Species"].unique()

for sp in unique_species:
    # 筛选当前物种的数据
    subset = germ_df[germ_df["Species"] == sp]
    # 创建画布
    plt.figure(figsize=(8, 6))
    # 绘制折线图
    sns.lineplot(data=subset, x="Day_since_cooling", y="Proportion_germination", 
                 hue="Soil_origin", marker="o", linewidth=1.2)
    # 设置图表属性
    plt.title(sp, fontsize=14, pad=15)
    plt.xlabel("冷却后天数", fontsize=12)
    plt.ylabel("萌发率比例", fontsize=12)
    plt.legend(title="土壤类型", bbox_to_anchor=(1.05, 1), loc="upper left")
    plt.tight_layout()
    # 保存文件
    plt.savefig(f"{sp}_germination_trend.png", dpi=300, bbox_inches="tight")
    plt.close()

内容的提问来源于stack exchange,提问作者Stefanie van den Berg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 00:55:08