10X空间转录组数据分析之细胞百分比饼图的绘制
2024-04-09 16:15:57  阅读数 1680

作者,追风少年i

最近一直有人问我空间转录组的饼图该如何绘制,如下图

图片.png

之前呢,说过一些,大家可以借鉴

10X空间转录组绘图之细胞类型百分比饼图和空间密度分布图

其中stlearn提供了封装好的函数,不过美观度上不够好。

相信大多数还是利用Seurat来进行联合分析,如果用了其他软件,可以将联合的矩阵结果进行替换,我们还是以之前的结果为例,HE染色和单细胞空间联合分析的结果部分展示如下

图片.png
图片.png
图片.png

画出来的效果如下图,颜色根据自己的喜好进行搭配

图片.png

好了,我们要开始了,一定要记住,前面的单细胞空间联合要亲自做

library(Seurat)
library(ggplot2)

####读取数据
cortex_sp <- readRDS(spatial.rds)
####单细胞空间联合的结果,这个如果已经导入到了rds,就不用再读取了
decon_mtrx = read.csv(sp.predictions.csv,row.names = 1)

####数据处理
cell_types_all <- colnames(decon_mtrx)[which(colnames(decon_mtrx) != "max")]

decon_df <- decon_mtrx %>%
  data.frame(check.names = F) %>%
  tibble::rownames_to_column("barcodes")
###添加比例到meta
cortex_sp@meta.data <- cortex_sp@meta.data %>%
  tibble::rownames_to_column("barcodes") %>%
  dplyr::left_join(decon_df, by = "barcodes") %>%
  tibble::column_to_rownames("barcodes")

###plot dot
slice <- names(cortex_sp@images)[1]
metadata_ds <- data.frame(cortex_sp@meta.data)
colnames(metadata_ds) <- colnames(cortex_sp@meta.data)
cell_types_interest <- cell_types_all

metadata_ds <- metadata_ds %>% tibble::rownames_to_column("barcodeID") %>%
            dplyr::mutate(rsum = base::rowSums(.[, cell_types_interest,
                drop = FALSE])) %>% dplyr::filter(rsum != 0) %>%
            dplyr::select("barcodeID") %>% dplyr::left_join(metadata_ds %>%
            tibble::rownames_to_column("barcodeID"), by = "barcodeID") %>%
            tibble::column_to_rownames("barcodeID")

spatial_coord <- data.frame(cortex_sp@images[[slice]]@coordinates) %>%
        tibble::rownames_to_column("barcodeID") %>% dplyr::mutate(imagerow_scaled = imagerow *
        cortex_sp@images[[slice]]@scale.factors$lowres, imagecol_scaled = imagecol *
        cortex_sp@images[[slice]]@scale.factors$lowres) %>% dplyr::inner_join(metadata_ds %>%
        tibble::rownames_to_column("barcodeID"), by = "barcodeID")

head(spatial_coord)
图片.png
就这样,得到了每个spot的坐标和细胞类型的比例,接下来就要绘图了,
###读取图片信息,这里读取Spaceranger分析的tissue_lowres_image.png即可
img <- png::readPNG(img)

###网格化
img_grob <- grid::rasterGrob(img, interpolate = FALSE, width = grid::unit(1,
        "npc"), height = grid::unit(1, "npc"))

####百分比饼图
scatterpie_pie <- suppressMessages(ggplot2::ggplot() + ggplot2::annotation_custom(grob = img_grob,
        xmin = 0, xmax = ncol(img), ymin = 0, ymax = -nrow(img)) +
        scatterpie::geom_scatterpie(data = spatial_coord, ggplot2::aes(x = imagecol_scaled,
            y = imagerow_scaled), cols = cell_types_all, color = NA,
            alpha = 1, pie_scale = 0.4) +
        ggplot2::scale_y_reverse() + ggplot2::ylim(nrow(img),
        0) + ggplot2::xlim(0, ncol(img)) + cowplot::theme_half_open(11,
        rel_small = 1) + ggplot2::theme_void() + ggplot2::coord_fixed(ratio = 1,
        xlim = NULL, ylim = NULL, expand = TRUE, clip = "on"))

pdf('sample.pie.pdf')
print(scatterpie_pie)
dev.off()
MK.pie.png

图例和配色有点差,我们来调整一下,首先调节一下图例的大小

####百分比饼图
scatterpie_pie <- suppressMessages(ggplot2::ggplot() + ggplot2::annotation_custom(grob = img_grob,
        xmin = 0, xmax = ncol(img), ymin = 0, ymax = -nrow(img)) +
        scatterpie::geom_scatterpie(data = spatial_coord, ggplot2::aes(x = imagecol_scaled,
            y = imagerow_scaled), cols = cell_types_all, color = NA,
            alpha = 1, pie_scale = 0.4) +
        ggplot2::scale_y_reverse() + ggplot2::ylim(nrow(img),
        0) + ggplot2::xlim(0, ncol(img)) + cowplot::theme_half_open(11,
        rel_small = 1) + ggplot2::theme_void() + ggplot2::coord_fixed(ratio = 1,
        xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") + ggplot2::theme(legend.key.size = unit(50,'pt'),legend.title = element_text(size = 60),legend.text = element_text(size = 40)))

png(paste(outdir,paste(sample,'pie.png',sep = '.'),sep = '/'),width = 9 * 200,height = 7 * 200,type = 'cairo')
print(scatterpie_pie)
dev.off()
MK.pie.png

接下来颜色搭配

defined_cols = c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6', '#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080')

scatterpie_pie <- suppressMessages(ggplot2::ggplot() + ggplot2::annotation_custom(grob = img_grob,
        xmin = 0, xmax = ncol(img), ymin = 0, ymax = -nrow(img)) +
        scatterpie::geom_scatterpie(data = spatial_coord, ggplot2::aes(x = imagecol_scaled,
            y = imagerow_scaled), cols = cell_types_all, color = NA,
            alpha = 1, pie_scale = 0.35) +
        ggplot2::scale_y_reverse() + ggplot2::ylim(nrow(img),
        0) + ggplot2::xlim(0, ncol(img)) + cowplot::theme_half_open(11,
        rel_small = 1) + ggplot2::theme_void() + ggplot2::coord_fixed(ratio = 1,xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") + ggplot2::theme(legend.key.size = unit(50,'pt'),legend.title = element_text(size = 60),legend.text = element_text(size = 40)) + scale_fill_manual(values = defined_cols[1:length(cell_types_all)]))

png(paste(outdir,paste(sample,'pie.png',sep = '.'),sep = '/'),width = 9 * 200,height = 7 * 200,type = 'cairo')
print(scatterpie_pie)
dev.off()
图片.png

好多了,借鉴一下第一张图的搭配

ggplot2::annotation_custom(grob = img_grob,
        xmin = 0, xmax = ncol(img), ymin = 0, ymax = -nrow(img)) +
        scatterpie::geom_scatterpie(data = spatial_coord, ggplot2::aes(x = imagecol_scaled,
            y = imagerow_scaled), cols = cell_types_all, color = NA,
            alpha = 1, pie_scale = 0.35) +
        ggplot2::scale_y_reverse() + ggplot2::ylim(nrow(img),
        0) + ggplot2::xlim(0, ncol(img)) + cowplot::theme_half_open(11,
        rel_small = 1) + ggplot2::theme_void() + ggplot2::coord_fixed(ratio = 1, xlim = NULL, ylim = NULL, expand = TRUE, clip = "on") + ggplot2::theme(legend.key.size = unit(50,'pt'),legend.title = element_text(size = 60),legend.text = element_text(size = 40)) + scale_fill_manual(values = defined_cols[1:length(cell_types_all)]))
图片.png

嗯,还行

来对比一下其他文章的图片形式

图片.png

好了,已经分享给大家了,生活很好,有你更好,百度文库出现了大量抄袭我的文章,对此我深表无奈,我写的文章,别人挂上去赚钱,抄袭可耻,挂到百度文库的人更可耻