在ggplot条形图中的每个x轴内的多个条之间添加显着性星号

问题描述

我正在尝试在ggplot条形图中指出与我的每个x轴标签中的控件相比有哪些明显不同的处理方法。 我将分类法作为x轴,每个分类中有四个不同的条形,将每种分类法的实验处理和生长指示为y轴。

我附上了使用提供的代码(从代码中消除了误差线)以及一些虚拟数据(其中“ Y”表示控制的重要性)制作的示例图的屏幕截图(如果更容易,请随意编号)

我曾经尝试使用geom_signif,但似乎无法在同一x轴列中添加多个比较结果,从而表明仅列之间存在差异。

我还尝试用每个星号的值绘制geom_point,但无法将星号与其各自的处理条对齐。

任何帮助将不胜感激。

ggplot(Experiment,aes(x=Taxa,y=Growth,fill=Treatment)) + 
  geom_bar(stat="identity",size=0.5,color="black",position=position_dodge())+
  scale_fill_manual(values=colorsvalue,name = expression(paste(Treatment)),labels=leglabs,position="top")+
  ylab(expression( Growth~ (day^{-1})))+
  theme_classic()+
  theme (axis.title.x=element_blank()) +
  scale_x_discrete (labels = xlabs)+
  theme (axis.text.x = element_text(angle=90,hjust=1,size=10))+
  theme(text = element_text(size=20))

enter image description here

样本数据

enter image description here

解决方法

一种方法是绘制包含“ Y”作为星号的因子。您可以通过首先将每个值的位置计算为new.x并使用新的y值进行偏移来实现。您可以将new.y值调整为适合您需要的值。因为每个图上都有四个图,所以条形图的中心是两端的(0.25 + 0.5)/ 2,中间条是(0.25 + 0)/ 2。负数在条形图的左侧。作为参考,我在this question.

上使用了第二个示例
library(dplyr)
    Experiment <- Experiment %>% mutate(new.x = ifelse(Treatment == "0.25",-0.375,ifelse(Treatment == "Control",-0.125,ifelse(Treatment == "D. magna",0.125,0.375))) + 
                                                as.numeric(as.factor(Taxa)))
    Experiment$new.y <- Experiment$Growth +1
    
    ggplot(Experiment,aes(x=Taxa,y=Growth,fill=Treatment)) + 
      geom_col(size=0.5,color="black",position=position_dodge())+
      geom_point(data=subset(Experiment,Star == 'Y'),aes(x=new.x,y=new.y),shape=8,show.legend=FALSE) +
      #scale_fill_manual(values=colorsvalue,name = expression(paste(Treatment)),labels=leglabs,position="top")+
      ylab(expression( Growth~ (day^{-1})))+
      theme_classic()+
      theme (axis.title.x=element_blank()) +
      #scale_x_discrete (labels = xlabs)+
      theme (axis.text.x = element_text(angle=90,hjust=1,size=10))+
      theme(text = element_text(size=20))

enter image description here

,

内置在this answer中的技巧是将position_dodge的宽度设置为0.9以使点与条对齐。然后,您可以将形状添加到原始aes中,但将一个形状设置为NA,将一个形状设置为8(星号)。您也可以在aes的{​​{1}}中添加1。

geom_point

enter image description here

数据

ggplot(Experiment,aes(x = Taxa,y = Growth,fill = Treatment,shape = star)) + 
  geom_bar(stat = "identity",color = "black",position = position_dodge()) +
  geom_point(aes(y = Growth + 1),position = position_dodge(0.9),show.legend = FALSE) +
  scale_shape_manual(values = c(NA,8))

编辑

一个带有负数的示例,它使用structure(list(Taxa = c("Cyano","Cyano","Dolicho","Gompho","Nosto","Nosto"),Treatment = c("Control","D. pulex","D. magna","0.25","Control","0.25"),Growth = 2:17,star = c("","Y","","Y")),class = "data.frame",row.names = c(NA,-16L)) 语句在恒星的y位置加1或减去。

ifelse

enter image description here

带负数的数据

ggplot(Experiment,fill=Treatment,shape = star)) + 
  geom_bar(stat="identity",position=position_dodge()) +
  geom_point(aes(y = ifelse(Growth > 0,Growth + 1,Growth - 1)),position=position_dodge(0.9),show.legend=FALSE) +
  scale_shape_manual(values = c(NA,8))

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...