做一个好看点的箱线分页图

作者: 一只烟酒僧 | 来源:发表于2020-12-17 20:52 被阅读0次

    ggplot分页修饰:https://zhuanlan.zhihu.com/p/225852640

    library(ggplot2)
    library(reshape2)
    library(stringr)
    library(ggsignif)
    library(ggsci)
    library(Hmisc)
    expr_mat<-seurat@assays$RNA@data[c("ZP1","ZP2","ZP3"),]
    expr_mat<-as.data.frame(expr_mat)
    expr_mat$gene=rownames(expr_mat)
    
    #宽数据转长数据
    expr_mat_log<-melt(expr_mat,id.vars = length(colnames(expr_mat)),variable.name = "sample",value.name = "expression")
    seurat@meta.data$stage=str_extract(seurat@meta.data$cell.type,"primordial|primary|secondary|antral|preovulatory")
    metadata<-seurat@meta.data[,c("stage","cell.type")]
    metadata$sample=rownames(metadata)
    expr_mat_log_anno<-merge(expr_mat_log,metadata,by.x = "sample",by.y = "sample",all.x = T)
    expr_mat_log_anno$cell.type<-str_extract(expr_mat_log_anno$cell.type,"Granulosa|Oo")
    expr_mat_log_anno$stage<-factor(expr_mat_log_anno$stage,levels = c("primordial","primary","secondary","antral","preovulatory"))
    #作图
    ggplot(expr_mat_log_anno,aes(x=stage,y=expression,fill=cell.type))+
      geom_boxplot(notch = T,notchwidth = 0.5,show.legend = T,outlier.size = NULL)+ 
      geom_jitter(size=0.2,show.legend = F)+
      facet_wrap(~gene,ncol = 1)+
      scale_fill_aaas()+
      scale_fill_discrete(label=c("Granulosa","Oocyte"))+ #修改图例的label
      scale_x_discrete(label=capitalize(levels(expr_mat_log_anno$stage)))+ #修改轴label
      theme(axis.title.x = element_blank(),
            axis.title.y = element_text(size = 15),
            axis.text = element_text(size = 15),
            axis.line = element_line(size = 0.5),#以上为修改轴参数
            strip.text = element_text(size = 15,color = "white"),
            strip.background = element_rect(fill=pal_aaas()(10)[5],color = "black",size=1),#以上为修改分页参数
            panel.grid = element_blank(),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_line(size = 0.5,color = "grey",linetype = 2),
            panel.background = element_blank(),##以上为修改背景参数
            legend.position = "top",,
            legend.text = element_text(size = 15),
            legend.title = element_text(size = 15))+#以上为修改图例参数
      ylab("log2(FPKM+1)")
    
    
    image.png

    相关文章

      网友评论

        本文标题:做一个好看点的箱线分页图

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