为什么我在 R 中此图中的每个数据点得到 8 个误差线?

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

我正在尝试在 R 中绘制一个图,其中包含 2 个位点(“对照”和“活性位点”)的平均 Pb 同位素比(206Pb/207Pb 和 208Pb/207Pb)。我想要每个数据点(站点)上有 4 个误差线来显示标准偏差。我已经在 Excel 中计算了平均值和标准差,因此我没有粘贴所有数据,而是将平均值列为“avg206Pb207Pb”,将标准差列为“sd206Pb207Pb”。这段代码之前在我的其他数据上运行得很好,但由于某种原因,我现在每个数据点都有 8 个错误栏。此外,通常误差线的末端会有垂直线,而这些则没有。我在这里缺少什么?

数据:

|site         |avg206Pb207Pb |sd206Pb207Pb |avg208Pb207Pb|sd208Pb207Pb|
|active site  |1.2553        |0.0046       |2.5245        |0.0017     |
|control      |1.2476        |0.0021       |2.5254        |0.0027     |

代码:

pbratiofourwayerror$site <- factor(
  pbratiofourwayerror$site, 
  levels = c("activesite","control")
)

a <- ggplot(
  pbratiofourwayerror, 
  aes(x=avg208Pb207Pb, y=avg206Pb207Pb)
) + 
geom_point(
  aes(color=site, shape=site), 
  size=5
) + 
scale_shape_manual(values=c(15,19)) +
xlab(expression(""^208*"Pb"*"/"^"207"*Pb)) + 
ylab(expression(""^206*"Pb"*"/"^"207"*Pb)) + 
theme_classic() + 
geom_errorbarh(
  aes(
    xmax = avg208Pb207Pb + sd208Pb207Pb, 
    xmin = avg208Pb207Pb - sd208Pb207Pb, 
    color=site
  ), 
  height=0.25
) + 
geom_errorbar(
  aes(
    ymin=avg206Pb207Pb-sd206Pb207Pb, 
    ymax=avg206Pb207Pb+sd206Pb207Pb, 
    color=site
  ), 
  width=0.25
)

a
r ggplot2
1个回答
0
投票

我稍微简化了您的数据集,以获得最小的可重现示例。您的

geom_errobar()
/
geom_errobarh()
width
/
height
对于此数据集来说太高了。

library(tidyverse)
#> Warning: package 'ggplot2' was built under R version 4.3.2
#> Warning: package 'stringr' was built under R version 4.3.2


data  = tibble ( site = c("a","c"), 
                  avg1 =c(1.25, 1.24),
                 sd1 = c(0.0046, 0.021), 
                  avg2 = c(2.52, 2.56),
                 sd2 = c(0.0017,0.0027))

 ggplot(
  data, 
  aes(x=avg1, y=avg2)
) + 
  geom_point(
    aes(color=site, shape=site), 
    size=5
  ) + 
  scale_shape_manual(values=c(15,19)) +
  xlab(expression(""^208*"Pb"*"/"^"207"*Pb)) + 
  ylab(expression(""^206*"Pb"*"/"^"207"*Pb)) + 
  theme_classic() + 
  geom_errorbarh(
    aes(
      xmax = avg1 + sd1, 
      xmin = avg1 - sd1, 
      color=site
    ),#
   height=0.001
  ) + 
  geom_errorbar(
    aes(
      ymin=avg2-sd2, 
      ymax=avg2+sd2, 
      color=site
    ), 
    width=0.001
  )

创建于 2024-04-29,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.