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

Aesthetics must be either length 1 or the same as the data: ymax, ymin, x, y

我一直在尝试解决此错误,但似乎无法解决。

我有一个如图所示的组合条形图,我想计算每个时间范围(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") 

我看过其他关于类似错误的帖子,但它们对我不起作用。感谢您提供有关如何解决此错误的任何意见。谢谢!

这是我的主要 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

这里是 summarySE_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

您不能在 aes 中使用 data$。数据来自 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") 

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

如果你在另一层使用原始数据,你可能需要在geom_errobar层设置inherit.aes = FALSE(并指定它的x美学),否则看起来对于数据中的y列,找不到,报错