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

R语言绘制叶片腐烂养分时序图遇summarySE报错,绘图无数据点

问题

我正在制作一个R程序,用于绘制叶片腐烂过程中养分随时间变化的数据图。使用R 4.3.2,在Visual Studio和R可执行文件中均遇到相同问题。

代码

library(ggplot2)
library(dplyr)
library(patchwork)
library(Rmisc)

Nutrient_data <- read.csv("C:\\Users\\MyName\\Downloads\\Nutrient_data.csv")

Nitrite_plotdat <- Nutrient_data %>% filter(Site=="Nitrite")
Orthophosphate_plotdat <- Nutrient_data %>% filter(Site=="Orthophosphate")

Nitrite_plotdat <- summarySE(data = Nitrite_plotdat, measurevar = "Quality", groupvars = "Site", na.rm = TRUE)
Orthophosphate_plotdat <- summarySE(data = Orthophosphate_plotdat, measurevar = "Quality", groupvars = "Site", na.rm = TRUE)

p_Nitrite <- ggplot(Nitrite_plotdat, aes(x = Site, y = Nitrite)) + 
  geom_bar(stat = "identity") + 
  geom_errorbar(aes(ymin = Nitrite - se, ymax = Nitrite + se), width = 0.2) + 
  theme_classic()

p_Orthophosphate <- ggplot(Orthophosphate_plotdat, aes(x = Site, y = Orthophosphate)) + 
  geom_bar(stat = "identity") + 
  geom_errorbar(aes(ymin = Orthophosphate - se, ymax = Orthophosphate + se), width = 0.2) + 
  theme_classic()

combined_plot <- p_Nitrite + p_Orthophosphate
combined_plot


# n_model <- aov(nitrate_sum$Quality~nitrite_sum$Site)
# summary(n_model)
# o_model<- aov(orthophosphate_sum$Quality~orthophosphate_sum$Site)
# summary(o_model)

错误信息

运行代码时,第11、12行出现如下错误:

> Nitrite_plotdat <- summarySE(data = Nitrite_plotdat, measurevar = "Quality",$
The following `from` values were not present in `x`: mean
> Orthophosphate_plotdat <- summarySE(data = Orthophosphate_plotdat, measureva$
The following `from` values were not present in `x`: mean

生成的图表有正确的坐标轴,但没有绘图点。

CSV数据片段

Day,Group,Site,Nitrite,Orthophosphate,,SE Nitrite,SE Orthophosphate,,,SD Nitrite,SD Orthophosphate

Monday,1,Townhouse_pond,0.002,0.37,,0.000812409,0.153933338,,,0.005914424,1.088473073

Monday,1,St_Johns_upstream,0.001,0.21,,,,,,,

Monday,1,St_Johns_downstream,0.007,0.11,,,,,,,

Monday,2,Townhouse_pond,0.002,0.53,,,,,,,

Monday,2,St_Johns_upstream,0.003,0.23,,,,,,,

Monday,2,St_Johns_downstream,0.003,0.17,,,,,,,

Monday,3,Townhouse_pond,0.005,0.56,,,,,,,

Monday,3,St_Johns_upstream,0.009,0.2,,,,,,,

Monday,3,St_Johns_downstream,0.016,0.24,,,,,,,

Monday,4,Townhouse_pond,0.013,0.57,,,,,,,

Monday,4,St_Johns_upstream,0.007,0.19,,,,,,,

Monday,4,St_Johns_downstream,0.01,0.17,,,,,,,

Tuesday am,1,Townhouse_pond,0.001,0.57,,,,,,,

Tuesday am,1,St_Johns_upstream,0.009,6.3,,,,,,,

已确认库导入正确,尝试了所有能想到的修复方法,恳请提供进一步的建议和帮助。


解决方案

核心问题分析

  1. 数据过滤逻辑错误:CSV中Site列是采样点名称(如Townhouse_pond),而非养分类型(Nitrite/Orthophosphate),用filter(Site=="Nitrite")会过滤掉所有数据,导致summarySE处理空数据集引发错误。
  2. summarySE参数错误:measurevar指定了不存在的Quality列,实际应统计Nitrite或Orthophosphate;groupvars仅设为Site无法体现时间维度的变化,需按Day+Site分组。
  3. 绘图映射错误:空数据集无法生成绘图元素,且后续绘图的y轴映射逻辑不符合summarySE的输出结构。

修复步骤

步骤1:修正数据处理逻辑

library(ggplot2)
library(dplyr)
library(patchwork)
library(Rmisc)

# 读取数据并移除空列(CSV中存在多个无意义空列)
Nutrient_data <- read.csv("C:\\Users\\MyName\\Downloads\\Nutrient_data.csv", na.strings = "") %>%
  select(-c(X, X.1, X.2))  # 移除自动命名的空列

# 按时间+采样点分组,统计亚硝酸盐的均值和标准误
Nitrite_plotdat <- summarySE(data = Nutrient_data,
                             measurevar = "Nitrite",
                             groupvars = c("Day", "Site"),
                             na.rm = TRUE)

# 按时间+采样点分组,统计正磷酸盐的均值和标准误
Orthophosphate_plotdat <- summarySE(data = Nutrient_data,
                                    measurevar = "Orthophosphate",
                                    groupvars = c("Day", "Site"),
                                    na.rm = TRUE)

步骤2:修正绘图代码

# 亚硝酸盐时间变化图
p_Nitrite <- ggplot(Nitrite_plotdat, aes(x = Day, y = Nitrite, fill = Site)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
  geom_errorbar(aes(ymin = Nitrite - se, ymax = Nitrite + se),
                width = 0.2, position = position_dodge(width = 0.8)) +
  labs(title = "亚硝酸盐浓度随时间变化", y = "浓度") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# 正磷酸盐时间变化图
p_Orthophosphate <- ggplot(Orthophosphate_plotdat, aes(x = Day, y = Orthophosphate, fill = Site)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
  geom_errorbar(aes(ymin = Orthophosphate - se, ymax = Orthophosphate + se),
                width = 0.2, position = position_dodge(width = 0.8)) +
  labs(title = "正磷酸盐浓度随时间变化", y = "浓度") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

# 合并图表并统一图例
combined_plot <- p_Nitrite + p_Orthophosphate + plot_layout(guides = "collect")
combined_plot

步骤3:方差分析代码修正(可选)

# 亚硝酸盐方差分析(时间和采样点作为因子)
n_model <- aov(Nitrite ~ Day + Site, data = Nutrient_data)
summary(n_model)

# 正磷酸盐方差分析
o_model <- aov(Orthophosphate ~ Day + Site, data = Nutrient_data)
summary(o_model)

额外说明

  • 若不想用summarySE重新计算,可直接使用CSV中已有的SE Nitrite、SD Nitrite列,但需确保数据与分组逻辑对齐。
  • 若更侧重时间趋势展示,可将geom_bar替换为geom_line+geom_point,更直观体现变化。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 04:21:05