簡體   English   中英

ggplot的置信區間誤差線

[英]confidence interval error bars for ggplot

我想為 ggplot 放置置信區間誤差線。

我有一個數據集,我用 ggplot 將其繪制為:

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 查看了一些選項,但無法使其工作。

我將不勝感激將置信區間誤差線放入條形圖中的任何幫助。 謝謝!

使用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

現在,您有一個數據框,其中有一列Average Weight和研究中每個樣本的SE 使用ggplot到 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"
  )

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM