将误差线添加到 ggplot2 R 中的绘图时出现问题

问题描述 投票:0回答:1

您好,我正在尝试使用 ggplot2 在线图中绘制误差线,但是我遇到了一些问题。这是我的代码:

mntd_loco<-ggplot(SES_MNTD_loco, aes(x = camp, y = mntd.obs.z, color = season, group = season)) +
  geom_point(alpha = 0.3, size= 3.5) +  # Add points with transparency
  geom_line(data = mean_data, aes(y = mean_mntd.obs.z), linewidth= 1.5) +  # Add lines for mean values
  geom_point(data = mean_data, aes(y = mean_mntd.obs.z), size = 5) +  # Add points for mean values
  geom_errorbar(data = mean_data, aes(ymin = mean_mntd.obs.z-sd_mntd.obs.z, ymax= mean_mntd.obs.z + sd_mntd.obs.z ), linewidth= 1.5)+
  geom_hline(yintercept = 0, linetype = "longdash", color = "black", linewidth= 2) +
  labs(x = "Elevation", y = "SES MNTD", color = "Season") +  # Label axes and legend
  scale_color_manual(values=c('#999999','#E69F00')) +
  theme_classic()`

当我尝试运行此程序时,出现以下错误:

`Error in `geom_errorbar()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 4th layer.
Caused by error in `FUN()`:
! object 'mntd.obs.z' not found

mean_data 数据集包含正确的变量。

> mean_data
# A tibble: 8 × 4
# Groups:   camp [4]
  camp  season mean_mntd.obs.z sd_mntd.obs.z
  <fct> <chr>            <dbl>         <dbl>
1 DG    dry             -0.413         0.607
2 DG    wet             -0.230         0.346
3 PC    dry             -1.08          0.302
4 PC    wet             -0.974         0.417
5 CL    dry             -1.08          0.173
6 CL    wet             -1.07          0.235
7 MS    dry             -0.299         0.690
8 MS    wet             -0.382         0.61

生成mean_data的代码是:

mean_data <- SES_MNTD_loco %>%
  group_by(camp, season) %>%
  summarize(mean_mntd.obs.z = mean(mntd.obs.z),
            sd_mntd.obs.z = sd(mntd.obs.z)) 

如果我在没有该层的情况下运行代码:

mntd_loco<-ggplot(SES_MNTD_loco, aes(x = camp, y = mntd.obs.z, color = season, group = season)) +
  geom_point(alpha = 0.3, size= 3.5) +  # Add points with transparency
  geom_line(data = mean_data, aes(y = mean_mntd.obs.z), linewidth= 1.5) +  # Add lines for mean values
  geom_point(data = mean_data, aes(y = mean_mntd.obs.z), size = 5) +  # Add points for mean values
  geom_hline(yintercept = 0, linetype = "longdash", color = "black", linewidth= 2) +
  labs(x = "Elevation", y = "SES MNTD", color = "Season") +  # Label axes and legend
  scale_color_manual(values=c('#999999','#E69F00')) +
  theme_classic()`

效果很好

This is the plot without the error bars

我生成mean_data的主要数据集,如下所示:

> head(SES_MNTD_loco)
         ntaxa mntd.obs mntd.rand.mean mntd.rand.sd mntd.obs.rank mntd.obs.z mntd.obs.p camp season
DG_dry.1    36 17.59933       20.40015     6.001859           355 -0.4666597 0.35464535   DG    dry
DG_dry.2    35 21.60363       20.52625     6.591346           643  0.1634539 0.64235764   DG    dry
DG_dry.3    43 13.62599       19.26860     5.472003           103 -1.0311768 0.10289710   DG    dry
DG_dry.4    35 23.39255       20.61614     7.202468           754  0.3854798 0.75324675   DG    dry
DG_dry.5    42 12.40775       19.34753     6.245307            64 -1.1111996 0.06393606   DG    dry
DG_dry.6    40 17.04743       19.79749     6.598712           389 -0.4167577 0.38861139   DG    dry

有人可以帮助我吗?我已经尝试过在其他页面找到的解决方案,但还没有成功

ggplot2 errorbar geom
1个回答
0
投票

要正确创建误差线,您需要指定以下参数:

x, y, ymin, ymax
。在您的代码中,您指定除
y
之外的所有内容,因此将使用
ggplot(...)
调用中最初指定的内容。

由于这是

y=mntd.obs.z
并且
mntd.obs.z
不在您为误差线指定的数据框中,因此您会收到此错误。

要解决此问题,您必须在

y
中再次指定
geom_errorbar
,如下所示:

geom_errorbar(data = mean_data, aes(y = mean_mntd.obs.z, 
                                    ymin = mean_mntd.obs.z-sd_mntd.obs.z, 
                                    ymax= mean_mntd.obs.z + sd_mntd.obs.z), 
              linewidth= 1.5)
© www.soinside.com 2019 - 2024. All rights reserved.