R ggplot:geom_bar上有三个响应的错误栏

时间:2014-03-23 18:59:04

标签: r ggplot2 geom-bar

这是我的代码:

ggplot(data=simple, aes(x=as.factor(nOnset.fin), 
    fill=simple$stress <- factor(simple$stress, levels=ordered(c('1', '2', '3'))))) +
    geom_bar(position = 'fill') + 
    scale_fill_manual(aes(Stress), values=colours.fin, 
                      breaks = levels(simple$stress <- factor(simple$stress, levels=ordered(c('1', '2', '3'))))) + 
    scale_y_continuous(labels=percent_format()) + theme_bw() + xlab('Size') + ylab(NULL) + 
    theme(text=element_text(size=35, family="CMU Sans Serif"), legend.position = 'none') 

这就是情节的产生:

https://copy.com/ghLt3z0iORicZtup

plot

我的问题很简单:如何将误差条添加到较深的蓝色条(较轻的两个条不重要)。我一直在尝试这个:

+ geom_errorbar(aes(ymin=lower, ymax=upper, x=as.factor(nOnset.fin)))

这是我一直在犯的错误:

  

错误:美学必须是长度1或与长度相同   dataProblems:lower,upper

上面代码中的

lowerupper定义为:

se0 <- 1.96 * sd(simple$int.0) / sqrt(length(simple$int.0))

lower <- mean(simple$int.0) - se0
upper <- mean(simple$int.0) + se0

非常感谢!

1 个答案:

答案 0 :(得分:0)

simple$nOnset.fin <- factor(simple$nOnset.fin)
simple$stress <- factor(simple$stress, levels=ordered(c('1', '2', '3')))
simple$se <- 1.96 * sd(simple$int.0) / sqrt(length(simple$int.0))
simple$lower <- mean(simple$int.0) - se0
simple$upper <- mean(simple$int.0) + se0

library(ggplot2)
library(scales)
ggplot(
  data = simple, 
  aes(x = nOnset.fin, fill = stress)
) +
  geom_bar(position = 'fill') + 
  geom_errorbar(aes(ymin = lower, ymax = upper)) + 
  scale_fill_manual(values=colours.fin) + 
  scale_y_continuous("", labels = percent) + 
  xlab('Size') + 
  theme_bw() +  
  theme(
    text = element_text(size=35, family="CMU Sans Serif"), 
    legend.position = 'none'
  )




library(fortunes)
fortune("tested solution")

Tested solutions offered when reproducible examples are provided.
   -- David Winsemius (suggesting a potential solution to a vague problem description)
      R-help (April 2011)