美学必须是长度1或与数据相同:ymax,ymin,x,y

时间:2018-06-14 14:12:25

标签: r ggplot2 errorbar

我一直试图解决这个错误,但似乎无法解决它。

我有一个组合的条形图,如图所示,我想计算每个时间帧(6,8,12周和终端)的误差条。我使用summarySE函数来查找每个时间帧的值,这是我用来创建图表和错误栏的代码:

limits <- aes (ymax= summarySE_data$mean + summarySE_data$se, ymin = summarySE_data$mean - summarySE_data$se)

p <- ggplot(df, aes(x = df$time_frame,  y = df$Engraftment_Efficiency , group = time_frame)) + scale_fill_discrete(breaks = c("six weeks", "eight weeks", "twelve weeks", "terminal point"))

p + geom_bar(stat = "identity", position = position_dodge(0.9)) + geom_errorbar(limits, position = position_dodge(0.9), width = 0.25, group = "Sample." )+ labs(x = NULL , y= "Engraftment Efficiency %") + ggtitle("PBL Engraftment Efficiency") 

我已经看过其他关于类似错误的帖子,但是它们对我没用。任何关于如何解决此错误的意见表示赞赏。谢谢!

enter image description here

这是我的主要df:

    time_frame Engraftment_Efficiency
1       six weeks                     49.8
2       six weeks                     47.3
3       six weeks                     56.1
4       six weeks                     36.7
5       six weeks                     54.8
6       six weeks                     48.0
7     eight weeks                     64.7
8     eight weeks                     52.0
9     eight weeks                     68.1
10    eight weeks                     47.2
11    eight weeks                     59.1
12    eight weeks                     65.5
13   twelve weeks                     72.6
14   twelve weeks                     55.0
15   twelve weeks                     77.3
16   twelve weeks                     61.4
17   twelve weeks                     73.4
18   twelve weeks                     72.6
19 terminal point                     69.8
20 terminal point                     43.2

这里是摘要SE_data:

       time_frame  N     mean        sd       se        ci
1      six weeks  6       48.78333  6.922259 2.826000  7.264465
2    eight weeks  6       59.43333  8.302690 3.389559  8.713139
3   twelve weeks  6       68.71667  8.572611 3.499754  8.996404
4 terminal point 11       71.48182 20.684817 6.236707 13.896249

1 个答案:

答案 0 :(得分:3)

您无法在data$内使用aes。数据来自data =参数,在aes()内,您应该只有列名。

这是我最有效的猜测。无法在没有看到您的数据的情况下进行测试,而group = "Sample."非常奇怪,因为它不在aes()中,它是一个字符串,无法判断它是否是列名,所以我只是将其删除了

p <-
ggplot(
    df,
    aes(x = time_frame,
        y = Engraftment_Efficiency ,
        group = time_frame)
  ) +
  scale_fill_discrete(breaks = c("six weeks", "eight weeks", "twelve weeks", "terminal point")) +
  geom_bar(stat = "identity", position = position_dodge(0.9)) +
  geom_errorbar(
    data = summarySE_data,
    aes(ymax = mean + se,
        ymin = mean - se),
    position = position_dodge(0.9),
    width = 0.25
  ) +
  labs(x = NULL , y = "Engraftment Efficiency %", title = "PBL Engraftment Efficiency") 

这是我对你想要的最好的猜测:

ggplot(
    summarySE_data,
    aes(x = time_frame,
        y = mean,
        group = time_frame)
  ) +
    geom_bar(stat = "identity", position = position_dodge(0.9)) +
  geom_errorbar(
    data = summarySE_data,
    aes(ymax = mean + se,
        ymin = mean - se),
    position = position_dodge(0.9),
    width = 0.25
  ) +
  labs(x = NULL , y = "Engraftment Efficiency %", title = "PBL Engraftment Efficiency") 

这里我们只使用摘要数据框 - 条形的高度是平均值,误差条显示+/-标准误差。看起来情节似乎不需要原始数据框。

如果您确实在其他图层中使用原始数据,则可能必须在inherit.aes = FALSE图层中设置geom_errobar(并指定其x美学),否则它将查找数据中的y列,找不到它,并抱怨。