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)
###读取图片信息,这里读取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()
####百分比饼图
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()
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()
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)]))
好了,已经分享给大家了,生活很好,有你更好,百度文库出现了大量抄袭我的文章,对此我深表无奈,我写的文章,别人挂上去赚钱,抄袭可耻,挂到百度文库的人更可耻