【问题标题】:Object not found in "geom_signif" function在“geom_signif”函数中找不到对象
【发布时间】:2020-07-22 12:19:35
【问题描述】:

我想在绘图中添加显着性星以进行平均差比较。没有星星的线条,情节有效:

da<-data.frame(group=c("condition1_high","condition1_low","condition2_high","condition2_low"),numb=c(30,25,26,20))

da %>% separate(group, c("A", "B"), remove = F) %>% 
  ggplot(aes(x=A, y=numb, fill = B)) +
  geom_bar(position=position_dodge(), stat="identity") +
  scale_fill_manual(values=rep(c("grey20","grey80"), ceiling(length(da$group)/2))[1:length(da$group)]) +
  geom_text(aes(label=numb), 
            position = position_dodge(width = 0.9), vjust = -0.25) +
  geom_signif(stat="identity",
              data=data.frame(x=c(0.5,1.5), xend=c(1,2),
                              y=c(30, 30), annotation=c("**", "*","***","+")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation))

现在我为我在这个平台上找到的星星添加一些代码:

da %>% separate(group, c("A", "B"), remove = F) %>% 
  ggplot(aes(x=A, y=numb, fill = B)) +
  geom_bar(position=position_dodge(), stat="identity") +
  scale_fill_manual(values=rep(c("grey20","grey80"), ceiling(length(da$group)/2))[1:length(da$group)]) +
  geom_text(aes(label=numb), 
            position = position_dodge(width = 0.9), vjust = -0.25) +
  geom_signif(stat="identity",
              data=data.frame(x=c(0.5,1.5), xend=c(1,2),
                              y=c(30, 30), annotation=c("**", "*")),
              aes(x=x,xend=xend, y=y, yend=y, annotation=annotation))

现在它说对象 B 丢失了。我能做什么?

【问题讨论】:

    标签: r ggplot2 error-handling visualization


    【解决方案1】:

    您需要将inherit.aes = FALSE 添加到geom_signif 调用中,否则它将尝试在您定义的新数据框中找到名为B 的列。这是因为您在对ggplot 的初始调用中放置了一个aes 调用。当你这样做时,默认情况下,所有后续的 geom 都将继承此调用的美学和数据。如果您将新数据传递给 geom,则它需要包含所有美学的值覆盖美学您需要使用inherit.aes = FALSE关闭继承 p>

    da %>% 
      separate(group, c("A", "B"), remove = FALSE) %>% 
      ggplot(aes(x = A, y = numb, fill = B)) +
      geom_bar(position=position_dodge(), stat = "identity") +
      scale_fill_manual(values = rep(c("grey20", "grey80"),    
                                     ceiling(length(da$group)/2))[1:length(da$group)]) +
      geom_text(aes(label=numb), 
                position = position_dodge(width = 0.9), vjust = -0.25) +
      geom_signif(stat="identity", inherit.aes = FALSE,
                  data=data.frame(x = c(0.5, 1.5), xend=c(1, 2),
                                  y = c(30, 30), annotation = c("**", "*")),
                  aes(x = x, xend = xend, y = y, yend = y, annotation = annotation))
    

    【讨论】:

      猜你喜欢
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      相关资源
      最近更新 更多