美学控制出错

时间:2015-02-19 18:21:11

标签: r ggplot2 aesthetics

我已获得数据框

pGi.Fi <- data.frame(
    Metadata_Well = c("D01", "F01"), 
    Freq = c("0.3789279","0.4191564"), 
    control = c("Scramble2","Scramble2"))

置信区间CI <- c(0.03222640,0.03196117)

的向量

此代码用于生成带有error_bar的条形图

limits <- aes(ymax = Freq+CI, ymin = Freq-CI)
dodge <- position_dodge(width=0.9)
bp <- ggplot(data=pGi.Fi, aes(x=Metadata_Well, y=Freq, fill=control)) + 
    geom_bar(position = "dodge", stat="identity") + 
    geom_bar(position=dodge) + 
    geom_errorbar(limits, position=dodge, width=0.25) + 
    scale_fill_grey()

我有这个错误

  

美学必须是长度为1或与dataProblems相同的长度:Metadata_Well,Freq

感谢您的回答

1 个答案:

答案 0 :(得分:1)

无法完全重现您的错误,但我认为某些问题与您传递美学的方式有关?一般来说,最好在传递给ggplot的数据上引用变量,而不是将对数据框的一些引用和其他引用混合到本地环境中的变量。我还放弃了您的geom_bar()个来电,因为它似乎是重复的。

pGi.Fi <- data.frame(
    Metadata_Well = c("D01", "F01"), 
    Freq = c(0.3789279,0.4191564), 
    control = c("Scramble2","Scramble2"),
    ci = c(0.03222640,0.03196117)
)

dodge <- position_dodge(width=0.9)

bp <- ggplot(
  data = pGi.Fi, 
  aes(
    x = Metadata_Well, 
    y = Freq, 
    fill = control
  )) + 
  geom_bar(
    position = "dodge", 
    stat = "identity"
  ) + 
  geom_errorbar(
    aes(
      ymax = Freq+CI, 
      ymin = Freq-CI
    ), 
    position = dodge, 
    width = 0.25
  ) + 
  scale_fill_grey()

bp