我想添加显着性星,用于对图进行均值差比较。没有星星的线条,该图有效:
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丢失了。我能做什么?
最佳答案
您需要将inherit.aes = FALSE
添加到geom_signif
调用中,否则它将尝试在您定义的新数据框中找到名为B
的列。这是因为您在对aes
的初始调用中放入了ggplot
调用。当您执行此操作时,默认情况下,所有后续几何都将继承此调用的外观和数据。如果将新数据传递给Geom,则它需要包括所有这些美学的值或覆盖美学,或者您需要使用inherit.aes = FALSE
关闭继承
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))
关于r - 在 “geom_signif”函数中找不到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63034060/