如何在ggplot2 R中绘制均值和误差条?

3
我想在R的qplot上绘制均值和误差线。这里提供一个我所说的示例:plot with means and error bars on the axes 如您所见,黄色的轴上画有均值和误差线。我希望我的qplot上也有这个功能。考虑以下数据子集:
x <- c(2.037820, 3.247560, 1.259053, 4.200520, 1.960179, 6.247880, 2.830693, 5.565390, 4.476610,
   4.627420, 2.500470, 4.156422, 2.855426, 9.210740, 2.663490, 4.412452, 3.270280, 2.838081,
   1.705650, 5.440690, 3.014000, 3.513820, 3.002930, 2.453080, 2.787320, 0.979227, 2.815368);

y <- c(2.855820, 3.332350, 1.991730, 3.688240, 3.565680, 3.525511, 4.451860, 3.233950, 6.125230,
   4.039360, 5.043330, 3.194650, 7.419020, 7.389600, 2.734740, 4.456250, 3.037665, 5.147140,
   3.184790, 3.595890, 5.457550, 1.527680, 2.848046, 1.418289, 3.996330, 4.516640, 2.884100);

fp <- qplot(x, y) + annotate("segment", x=-Inf, xend=Inf,y=-Inf, yend=Inf);
ggExtra::ggMarginal(fp, type = "density", margins = 'both')

它应该给你一个像这样的图表: 点击此处查看图表 那么,如何绘制我的平均值和误差条?在R中基本绘图中使用的axes()在ggplot2中不起作用。
我感激任何建议,即使它需要更改软件包或以不同的方式处理问题。
谢谢!
2个回答

3
也许不完全符合您的要求,但这可能是您继续工作的起点:
require(ggplot2)
require(dplyr)

df <- data.frame(x = x, y = y)


dferrx <- df %>%
  summarise(m = mean(x),
            lo = m - 1.96 * sd(x)/sqrt(n()),
            hi = m + 1.96 * sd(x)/sqrt(n()),
            x = m)

dferry <- df %>%
  summarise(m = mean(y),
            lo = m - 1.96 * sd(y)/sqrt(n()),
            hi = m + 1.96 * sd(y)/sqrt(n()),
            y = m)


ggplot(df, aes(x = x, y = y)) + 
  geom_point() +
  annotate("segment", x=-Inf, xend=Inf,y=-Inf, yend=Inf) +
  geom_errorbar(data = dferry, aes(x = 0, ymin = lo, ymax = hi)) +
  geom_errorbarh(data = dferrx, aes(y = 0, xmin = lo, xmax = hi))

谢谢,接近了!我会继续努力,但是geom_errorbarh()函数非常实用。 - Evy
2
@Evy 你可能想要将最后两行中的 x=0y=0 替换为 -Inf。这样它就会被绘制在坐标轴上,而不是在原点 0,0 上的图表上。 - Brian

0

你可能已经解决了这个问题,但是以防万一,这里是每个点的误差条代码,遵循Wietze314的贡献:

library(tidyverse)

example_DF <- tibble(x = c(2.037820, 3.247560, 1.259053, 4.200520, 1.960179, 6.247880, 2.830693, 5.565390, 4.476610,
                       4.627420, 2.500470, 4.156422, 2.855426, 9.210740, 2.663490, 4.412452, 3.270280, 2.838081,
                       1.705650, 5.440690, 3.014000, 3.513820, 3.002930, 2.453080, 2.787320, 0.979227, 2.815368),
                 y = c(2.855820, 3.332350, 1.991730, 3.688240, 3.565680, 3.525511, 4.451860, 3.233950, 6.125230,
                       4.039360, 5.043330, 3.194650, 7.419020, 7.389600, 2.734740, 4.456250, 3.037665, 5.147140,
                       3.184790, 3.595890, 5.457550, 1.527680, 2.848046, 1.418289, 3.996330, 4.516640, 2.884100))


dferrx <- example_DF %>%
  summarise(m = mean(x),
            lo = m - 1.96 * sd(x)/sqrt(n()),
            hi = m + 1.96 * sd(x)/sqrt(n()),
            x = m)

dferry <- example_DF %>%
  summarise(m = mean(y),
            lo = m - 1.96 * sd(y)/sqrt(n()),
            hi = m + 1.96 * sd(y)/sqrt(n()),
            y = m)


ggplot(example_DF, aes(x = x, y = y)) + 
  geom_point() +
  annotate("segment", x=-Inf, xend=Inf,y=-Inf, yend=Inf) +
  geom_errorbar(aes(ymin =y -dferry$lo, ymax = y+ dferry$hi))+
  geom_errorbarh(aes(xmin =x -dferrx$lo, xmax = x+ dferrx$hi))

enter image description here

它需要进行很多美学调整,但是这就是它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接