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

hello,周四了,一周又即将过去,不知道大家有没有感觉到时光飞逝,时间啊,慢些吧,给自己点时间可以思考。
这一篇教给大家一个简单的内容,画图,一张是细胞类型占比饼图,一张是细胞密度空间分布图,如下图:

我们首先来饼图
library(Seurat)
library(ggplot2)
###读取数据,读入自己的数据
Spatial = readRDS(Seurat_spatial_rds)
anno = read.csv(sp_sc_joint,header = T,row.names = 1,check.names = F)###单细胞空间联合分布矩阵

coor = Spatial@images$image@coordinates[,c(5,4)]
meta = rbind(coor,anno)

head(meta)

colnames(meta)[1:2] = c('x','y')
st_meta$x <- (st_meta$x - min(st_meta$x))/(max(st_meta$x) - 
        min(st_meta$x))
st_meta$y <- (st_meta$y - min(st_meta$y))/(max(st_meta$y) - 
        min(st_meta$y))
cellname <- colnames(meta)[-c(1:2)]

col_manual <- ggpubr::get_palette(palette = "lancet", 
            k = length(cellname))####颜色

ggplot2::ggplot() + scatterpie::geom_scatterpie(data = st_meta, 
        ggplot2::aes(x = x, y = y), col = cellname, color = NA, 
        pie_scale = pie_scale) + ggplot2::coord_fixed(ratio = 1) + 
        ggplot2::scale_fill_manual(values = col_manual) + ggplot2::theme_bw() + 
        ggplot2::theme(panel.grid = ggplot2::element_blank()) + 
        ggplot2::labs(x = "scaled_x", y = "scaled_y")
就可以得到细胞类型分布的饼图了

然后下一张图,看某种细胞类型的空间分布
col_manual <- ggpubr::get_palette(palette = "lancet", 
            k = length(cellname))
        point_color <- col_manual[which(cellname == celltype)]

st_meta <- st_meta[st_meta$celltype == celltype, ]
p <- ggplot2::ggplot(data = st_meta, ggplot2::aes(x, y)) + 
        ggplot2::theme_bw() + ggplot2::theme(panel.grid = ggplot2::element_blank())

p <- p + ggplot2::geom_point(size = point_size, color = point_color)

p <- p + ggplot2::stat_density2d(ggplot2::aes(color = ..level..), 
            size = size)

p + scale_color_gradient(low = color_low, high = color_high)

color_midpoint <- stats::median(st_meta$gene)

p + scale_color_gradient2(low = color_low, mid = color_mid, 
                high = color_high, midpoint = color_midpoint)

print(p)
就拿到了下面的结果

其中刚开始用
p <- p + ggplot2::stat_density2d(ggplot2::aes(fill = ..density..), 
            geom = "raster", contour = F)
就会得到下面的图

你学会了么,生活很好,有你更好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值