ggplot的置信区间误差线 [英] confidence interval error bars for ggplot

查看:71
本文介绍了ggplot的置信区间误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为ggplot放置置信区间误差线.

I want to put confidence interval error bars for ggplot.

我有一个数据集,并使用ggplot将其绘制为:

I have a dataset and I am plotting it with ggplot as:

df <- data.frame(
        Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"), 
        Weight=c(10.5, NA, 4.9, 7.8, 6.9))

p <- ggplot(data=df, aes(x=Sample, y=Weight)) + 
geom_bar(stat="identity", fill="black") + 
scale_y_continuous(expand = c(0,0), limits = c(0, 8)) + 
theme_classic() + 
theme(axis.text.x = element_text(angle = 45, hjust = 1)

p

我不熟悉添加错误栏.我使用geom_bar查看了一些选项,但无法正常工作.

I am new to adding error bars. I looked at some options using geom_bar but I could not make it work.

对于将置信区间误差线放在小节图中的任何帮助,我将不胜感激.谢谢!

I will appreciate any help to put confidence interval error bars in the barplot. Thank you!

推荐答案

使用 geom_errorbar

df <- data.frame(
  Sample=c("Sample1", "Sample2", "Sample3", "Sample4", "Sample5"), 
  Average.Weight=c(10.5, NA, 4.9, 7.8, 6.9),
  # arbitrarily make up some Standard Errors for each mean:
  SE = c(1, NA, .3, .25, .2)) # JUST MAKING THINGS UP HERE

现在您有了一个数据框,其中包含您研究中每个样本的平均权重 SE 列.使用 ggplot 进行绘制:

Now you have a data frame with a column of Average Weight and the SE for each sample in your study. Use ggplot to plot:

ggplot(data = na.omit(df)) + #don't bother plotting the NA
  geom_bar(stat = "identity", aes(x = Sample,y = Average.Weight)) +
  geom_errorbar(
    aes(x=Sample, 
        ymin = Average.Weight - 2*SE, 
        ymax = Average.Weight + 2*SE), 
    color = "red"
  )

这篇关于ggplot的置信区间误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆