在这个帖子中:用 SPOTlight 的函数绘制空转Seurat注释结果的大饼子!,眼明心亮的网友留言说在这里观察到了:饼图绘图中上下方向颠倒了。提问如下
此外,如果你有任何生信疑问,可以看看我们的这两个专辑哦:《生信马拉松答疑》和《马拉松授课互动答疑》,也欢迎大家图文并茂发邮件:jzhang910@qq.com 或者加微信咨询:Biotree123。
如果你在学习生信的过程中觉得孤军难以奋战,可以报名参加我们每月一起的生信入门课程,最新一期在6月2号开课,跟大家一起成长与学习:生信入门&数据挖掘线上直播课6月班。
还有一个数据在分析中发现方向也是反的:12个Cell杂志的疑难杂症空转读取与分析:GSE144240,这个里面已经使用坐标调整进行了解决。下面来看看 SPOTlight 函数绘图问题!
数据我还是使用这个帖子中的:用 SPOTlight 的函数绘制空转Seurat注释结果的大饼子!,一直运行到 SPOTlight 绘图之前的反卷积注释部分(注意这里运行的时候使用 Seuratv4 版本,下面的绘图代码暂不支持 seuratv5 ! )
既然 SPOTlight 这个函数绘图上下颠倒了,那我们就看看其源码是如何绘图的好了。
源码链接:https://github.com/MarcElosua/SPOTlight/blob/spotlight-0.1.7/R/scatterpie_plot_fun.R
先把数据整理好:
#################################
mat[1:8, 1:8]
decon_mtrx <- mat
#### 数据处理
cell_types_all <- colnames(decon_mtrx)
cell_types_all
decon_df <- decon_mtrx %>%
data.frame(check.names = F) %>%
tibble::rownames_to_column("barcodes")
###添加比例到meta
cortex_sp <- spatial
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")
head(cortex_sp@meta.data)
### plot dot
slice <- names(cortex_sp@images)[1]
slice
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)
mat 为反卷积的结果:
head(spatial_coord) 的结果示意如下,就是seurat 对象的metadata 与 反卷积结果的合并在一起:
接着读取 染色切片:
img <- png::readPNG("../seurat/V1_Mouse_Brain_Sagittal_Anterior/spatial/tissue_lowres_image.png")
###网格化
img_grob <- grid::rasterGrob(img, interpolate = FALSE, width = grid::unit(1, "npc"), height = grid::unit(1, "npc"))
颜色定义:
# 颜色定义
paletteMartin <- c(
"#000000", "#004949", "#009292", "#ff6db6", "#ffb6db",
"#490092", "#006ddb", "#b66dff", "#6db6ff", "#b6dbff",
"#920000", "#924900", "#db6d00", "#24ff24", "#ffff6d")
pal <- colorRampPalette(paletteMartin)(length(ct))
names(pal) <- ct
pal
SPOTlight 你可以看出来 源码是使用了 scatterpie 进行的绘图:
#### 饼图绘制
scatterpie_pie <- 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 = 0.6, 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::guides(color = guide_legend(ncol = 2)) +
ggplot2::scale_fill_manual(values = pal)
结果如下:
现在方向就不是反的了吧!
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有