单细胞小提琴图

作者: 一只小脑斧 | 来源:发表于2022-07-11 16:24 被阅读0次
    4BPY(OOQ%IXO29{`L}7K2%5.png
    #############差异小提琴图
    tumor <- readRDS("tumor.RDS")
    df.data <- tumor@assays$RNA@data %>% as.data.frame()
    head(rownames(df.data))
    Cs <- df.data["Cs",] %>% as.data.frame()%>% t() %>% as.data.frame()
    Cs$id<-rownames(Cs)
    
    
    ####提取样本id
    cell <- as.data.frame(tumor@meta.data)
    cell$id<-rownames(cell)
    cell<-cell[,c("id","tissue_type")]
    
    
    count<-merge(Cs,cell,by="id")
    
    ###############小提琴图
    # 添加P值
    # 得到cluster6和cluster10中的gsva score 最大值
    count.geneset.group1 <- count$geneset[count$tissue_type == "test"]
    count.geneset.group2 <- count$geneset[count$tissue_type == "control"]
    
    # 确定P值添加的位置
    ysegment1 <- max(count.geneset.group1)
    ysegment2 <- max(count.geneset.group2)
    ysegment.max <- max(ysegment1, ysegment2)
    
    # 根据P值确定加几颗星
    pval <- 2.065159e-74
    
    if (pval < 0.001) {
      pval.label = "***"
    } else if (pval < 0.005) {
      pval.label = "**"
    } else if (pval < 0.05) {
      pval.laben = "*"
    } else if (pval >= 0.05) {
      pval.label = "non.sig"
    }
    
    # 自定义颜色
    blue <- "#619CD6"
    green <- "#89C32E"
    p <- ggplot(count, aes(x = tissue_type, y = Cs, fill = tissue_type)) +
      geom_violin() +
      geom_boxplot(size=0.5,width=0.1,color="white") +
      scale_fill_manual(values = c(blue, green)) + # 用自定义颜色填充
      theme_classic() +
      theme(panel.grid = element_blank(), 
            plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"), 
            axis.title.x = element_text(color = "black", size = 20), 
            axis.title.y = element_blank(), 
            axis.text = element_text(color = "black", size = 16), 
            axis.line = element_line(colour = "black", size = 0.6), 
            plot.title = element_text(size = 20, hjust = 0.5)) + 
      # 添加图形标题
      ggtitle("Cs") +
      guides(fill = F)
    p
    
    
    # 前面已经得到了pvalue,添加上去就可以了
    # 确定P值添加的位置
    ysegment1 <- max(count.geneset.group1)
    ysegment2 <- max(count.geneset.group2)
    ysegment.max <- max(ysegment1, ysegment2)
    
    ysegment1 <- 3.5
    ysegment2 <- 3.5
    ysegment.max <-3.5
    
    p + 
      # 图形中的一个横线和两个竖线
      #annotate("segment", x = 1, xend = 2, y = ysegment.max + 0.02, yend = ysegment.max + 0.02) + 
      #annotate("segment", x = 1, xend = 1, y = ysegment1 + 0.01, yend = ysegment.max + 0.02) +
      #annotate("segment", x = 2, xend = 2, y = ysegment2 + 0.01, yend = ysegment.max + 0.02) +
      # 添加P值对应的星号
      annotate("text", 
               size = 8, # *的大小
               x = 1.5, 
               y = ysegment.max - 0.03, #可以微调*所在的位置
               label = pval.label)
    
    ggsave("Cs.tumor.all.pdf",width = 10,height = 10)
    
    
    
    

    相关文章

      网友评论

        本文标题:单细胞小提琴图

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