美文网首页IMP research
柱状图 | 百分比柱状图

柱状图 | 百分比柱状图

作者: 一只小脑斧 | 来源:发表于2022-07-04 20:13 被阅读0次

    【R>>barplot】堆叠柱状图+百分比展示 - 简书 (jianshu.com)
    (125条消息) R语言的各种假设检验_hellolijunshy的博客-CSDN博客_r语言假设检验

    M39B_XUO}OBDEY~~DSSNIL9.png
    ################一、从单细胞数据中取出信息
    cell <- tumor@meta.data
    input <- cell[,c(5,15)]#seurat_clusters,tissue_type
    a <- data.frame(table(input$seurat_clusters,input$tissue_type))
    ##算百分比
    library(plyr)
    a<- ddply(a,.(Var1),transform,percent=Freq/sum(Freq)*100) 
    a$label = paste0(sprintf("%.1f", a$percent), "%")
    
    ################二、批量算p值
    n<-length(unique(a$Var1))
    
    out <- numeric()
    
    #a$percent <- a$percent/100
    for ( i in 0:(n-1)){
      #i=1
      shi<-a[a$Var1==i,]
      p <- binom.test(round(shi[2,4]),100,p = 0.5)$p.value
      out <- c(out,p)
    }
    
    out
    
    pvalue <- out
    
    colnames(a)[1]<-"cluster"
    colnames(a)[2]<-"group"
    
    ggplot(a,aes(cluster,percent,fill=group))+
      geom_bar(stat="identity",position = position_stack())+
      scale_fill_manual(values = c("#008ECA","#DB423E"),label=c("Control","Test"))+#更改ggplot填充颜色
      scale_y_continuous(labels = scales::percent_format(scale = 1))+ #百分比y轴
      labs(x="Tumor",y="",fill="")+
      annotate("text", x ="0", y = 100.5,label = c("***"))+
      annotate("text", x ="1", y = 100.5,label = c("***"))+
      annotate("text", x ="2", y = 100.5,label = c("***"))+
      annotate("text", x ="3", y = 100.5,label = c("***"))+
    annotate("text", x ="7", y = 100.5,label = c("***"))+
    annotate("text", x ="8", y = 100.5,label = c("***"))+
    annotate("text", x ="9", y = 100.5,label = c("***"))+
      theme_classic()+
      theme(legend.position = "top",
            legend.text = element_text(size=12),
            axis.text = element_text(size=12),
            axis.title = element_text(size=12))
    
    

    相关文章

      网友评论

        本文标题:柱状图 | 百分比柱状图

        本文链接:https://www.haomeiwen.com/subject/asbdbrtx.html