如何手动添加多个geom_signif()到一个带有for循环的聚类条形图中?

x8diyxa7  于 2023-10-13  发布在  其他
关注(0)|答案(1)|浏览(75)

我正在创建一个聚类条形图,并希望根据以前计算的统计数据中出现的重要性添加重要的星号。由于在某些列之间可能会注解多个星号,所以我想用geom_sign()对ggplot()进行for循环。

df <- data.frame(
  mouse = rep(1:5, each = 4),
  treatment = rep(rep(c("A", "B"), each = 2), times = 5),
  poke_type = rep(rep(c("Left", "Right"), times = 2), times = 5),
  pokes = rpois(20, lambda = 5)
)

#means
mean_df%>%
 group_by(treatment, poke_type)%>%
 summarize(mean=mean(pokes))

#complete pairwise comparison and produce dataframe with astricks for annotations

annotations_df <- data.frame(
 main=c("A","B","Left","Right"),
 group1=c("Left","Right","A","B"),
 group2=c("Right","Left","B","A"),
 pval_symbol=c("*","n.s","**","n.s"),
 xmin_index=c(0.8,2.8,0.8,2.8),
 xmax_index=c(1.2,3.2,2.8,3.2),
 y=c(210,210,210,210))

#cluster bar graph
plot<-mean_df%>%
 ggplot(aes(x=treatment,y=mean, fill=poke_type)+
 geom_bar(stat='identity',position='dodge',color= 'black',size=0.7)

#add significance on cluster bar graph according to annotation_df

到目前为止,我的尝试是:

if(any(annotations_df$pval_symbol!="n.s")){
    row_index<<-which(annotations_df$pval_symbol != "n.s")
    for(i in row_index){
      index <- as.numeric(i)
      plot<- plot+geom_signif(
        data = annotations_df,
        aes(xmin=annotations_df$xmin_index[index],
            xmax=annotations_df$xmax_index[index],
            annotations=annotations_df$pval_symbol[index],
            y_position=annotations_df$y[index]),
        textsize=10,size=0.8,manual=TRUE,inherit.aes = FALSE, tip_length = 0.1)}
  } else {}
watbbzwu

watbbzwu1#

不需要for循环

if(any(annotations_df$pval_symbol!="n.s")){
    index_df<-annotations_df[annotations_df$pval_symbol != "n.s",]

    plot<- plot+geom_signif(
      data = index_df,
      aes(xmin=index_df$xmin_index,
          xmax=index_df$xmax_index,
          annotations=index_df$pval_symbol,
          y_position=index_df$y),
      textsize=10,size=0.8,manual=TRUE,inherit.aes = FALSE)

} else {}

相关问题