柱状图 | 百分比柱状图

【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))

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容