柱状图 | 百分比柱状图

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

M39B_XUO}OBDEY~~DSSNIL9.png
################一、从单细胞数据中取出信息
cell <- [email protected]
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))

你可能感兴趣的:(柱状图 | 百分比柱状图)