如何在条形图上添加错误栏

时间:2016-03-01 08:29:16

标签: r csv errorbar

我对R中的工作感到困惑。我想在条形图上显示具有组变量的误差条。这是数据(文件名s4.csv)。我使用以下代码:

a<- read.csv(file= "s4.csv")

error<- a$Zn_STD
ggplot(a, aes(Variety, Zn))+labs(title="Zinc in flesh and pits of dates") + geom_bar(stat="identity",   group= "Variety", fill='steelblue') +
        facet_wrap(~ Part)+theme(panel.background = element_rect (fill = "White"))+
ggplot(geom_errorbar(aes(ymin=a$Zn, ymax=a$Zn+error, width = 0.2), position=position_dodge(width=0.8))) 


# Here is my Data
# This data contain 3 varieties designated as V and 2 parts (designated as Flesh and Pits) Standard deviation is mentioned as Zn_STD.

Variety Part        Zn       Zn_STD
V 1    Flesh    0.313333333  0.087
V 2    Flesh    1.113333333  0.43
V 3    Flesh    1.38         0.34
V 1    Pits     1.913333333  0.87
V 2    Pits     1.113333333  0.38
V 3    Pits     1.202222222  0.28

1 个答案:

答案 0 :(得分:1)

除了计数数据外,请不要使用条形图(例如参见this citation)。每次你这样做,统计学家都会跳下一座高大的桥梁。改用点。 ggplot2为此提供了一个很好的geom。以下示例取自ggplot2 documentation page

df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

library(ggplot2)

ggplot(df, aes(trt, resp, colour = group)) +
  geom_pointrange(aes(ymin = lower, ymax = upper))

enter image description here