【问题标题】:R ggplot: Error bars on geom_bar with three responsesR ggplot:geom_bar 上的误差线具有三个响应
【发布时间】:2014-03-23 18:59:04
【问题描述】:

这是我的代码:

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

我的问题很简单:如何将误差线添加到较深的蓝色条中(较浅的两个条在这里无关紧要)。我一直在尝试这个:

+ 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

非常感谢!

【问题讨论】:

  • A dput() 的变量将有助于重现您当前的结果。
  • 映射美学(aes 内部的那些需要是数据框中的列,或者它们的简单转换)。 Set 美学(aes()outside)需要是单个值。也许您打算将stress 转换为first 因子,然后将填充映射到压力?在ggplot 调用中使用&lt;- 确实很奇怪。

标签: r ggplot2 geom-bar


【解决方案1】:
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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-05-06
    • 2015-04-18
    • 2022-12-07
    • 1970-01-01
    相关资源
    最近更新 更多