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)
就会得到下面的图

你学会了么,生活很好,有你更好
网友评论