美文网首页
ggplot作图实现线条注释-气泡图展示单细胞cluster与c

ggplot作图实现线条注释-气泡图展示单细胞cluster与c

作者: KS科研分享与服务 | 来源:发表于2024-09-11 12:52 被阅读0次

    好看的图千千万,我就中意这一款。关于作图的注释,有很多种方法,有段时间,我很中意线条式的注释。一般情况,用AI直接画挺方便,但是还是想使用代码进行实现。发现ggh4x包有类似的效果,可以实现,所以这里演示一下。我们的演示示例式关于单细胞marker基因气泡图的注释,至于其他的图,基于ggplot作图的都可以使用这样的方式进行注释。

    当然了,在尝试过程中,我们尝试拓展了效果,例如增粗线条,修改注释颜色,文字颜色等等,我们的效果如下:


    image.png

    获取作图数据:

    
    setwd("/home/ks_ts/data_analysis/ggh4x/")
    
    library(Seurat)
    library(ggh4x)
    library(ggplot2)
    library(dplyr)
    
    p = DotPlot(sce_cca, features = markers)
    dat <- p$data
    
    #获取cluster注释
    anno <- distinct(df, sce_cca$seurat_clusters,sce_cca$celltype)
    colnames(anno) <- c("id","celltype")
    df <- left_join(dat, anno, by = "id")
    
    
    cluster.order <- c(2,4,5,6,8,15,16,18,0,1,3,22,11,12,9,7,10,21,14,13,17,19,20)
    dat$id <- factor(dat$id, levels= cluster.order)
    colnames(dat)
    # [1] "avg.exp"        "pct.exp"        "features.plot"  "id"            
    # [5] "avg.exp.scaled"
    

    ggplot正常气泡图:

    
    ggplot(dat, aes(features.plot, id,size=pct.exp, fill=avg.exp.scaled)) + 
      geom_point(shape = 21, colour="black", stroke=0.5) +
      guides(size=guide_legend(override.aes=list(shape=21, colour="black", fill=NA))) + 
      theme(
        panel.background = element_blank(),
        panel.border = element_rect(fill = NA),
        panel.grid.major.x = element_line(color = "grey80"),
        panel.grid.major.y = element_line(color = "grey80"),
        axis.title = element_blank(),
        axis.text.y = element_text(color='black',size=12),
        axis.text.x = element_text(color='black',size=12, angle = 90, hjust = 1, vjust = 0.5))+
      scale_fill_gradientn(colours = c('#5749a0', '#0f7ab0', '#00bbb1',
                                        '#bef0b0', '#fdf4af', '#f9b64b',
                                        '#ec840e', '#ca443d', '#a51a49'))
    
    image.png
    
    #plot2 结合celltype与cluster
    p = ggplot(df, aes(features.plot, interaction(id, celltype),size=pct.exp, fill=avg.exp.scaled)) + 
      geom_point(shape = 21, colour="black", stroke=0.5) +
      theme(
        panel.background = element_blank(),
        panel.border = element_rect(fill = NA),
        panel.grid.major.x = element_line(color = "grey80"),
        panel.grid.major.y = element_line(color = "grey80"),
        axis.title = element_blank(),
        axis.text.y = element_text(color='black',size=12),
        axis.text.x = element_text(color='black',size=12, angle = 90, hjust = 1, vjust = 0.5))+
      scale_fill_gradientn(colours = c('#5749a0', '#0f7ab0', '#00bbb1',
                                       '#bef0b0', '#fdf4af', '#f9b64b',
                                       '#ec840e', '#ca443d', '#a51a49'))+
      guides(y = "axis_nested")
    
    image.png
    #添加注释\把celltype颜色标记,与UMAP对应一致即可
    p+theme(ggh4x.axis.nesttext.y = element_text(colour = c('#E58606', '#5D69B1', '#52BCA3', '#99C945', '#CC61B0', '#24796C', '#DAA51B',
                                                            '#2F8AC4', '#764E9F', '#ED645A', '#CC3A8E')),
            ggh4x.axis.nestline.y = element_line(size=2))
    
    image.png
    #当然,我们也可以直接标注线条,与UMAP对应一致即可
    p+theme(ggh4x.axis.nestline.y = element_line(size=1,
                                                 colour = c('#E58606', '#5D69B1', '#52BCA3', '#99C945', '#CC61B0', '#24796C', '#DAA51B',
                                                            '#2F8AC4', '#764E9F', '#ED645A', '#CC3A8E')))
    
    
    image.png

    完美,非常满意,觉得分享有用的点个赞再走呗!

    相关文章

      网友评论

          本文标题:ggplot作图实现线条注释-气泡图展示单细胞cluster与c

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