yongcaihuang-lab/Maize_Stem_Spatial_Project
0
1All_Spatial_plot <- function(GeneID){2 3 SpatialPlot_Function <- function(Sample, ID, titleID, limits = NULL) {4 5 # =======================================================6 # 1. 自动匹配样本名称逻辑7 # =======================================================8 current_ident <- as.character(unique(Sample$orig.ident))9 10 name_map <- c(11 "B73_1" = "B73-1",12 "B73_2" = "B73-2",13 "Ames21814_1" = "Ames21814-1",14 "Ames21814_2" = "Ames21814-2"15 )16 17 final_title <- name_map[current_ident]18 if (is.na(final_title)) final_title <- current_ident19 20 # =======================================================21 # 2. 基础绘图 (使用 suppressMessages 屏蔽内置 scale 警告)22 # =======================================================23 SFP <- suppressMessages({24 Seurat::SpatialFeaturePlot(25 Sample,26 features = ID,27 crop = TRUE,28 keep.scale = "feature",29 pt.size.factor = 2.1,30 image.alpha = 1,31 alpha = c(0.6, 1),32 stroke = NA,33 shape = 2134 ) + 35 # =======================================================36 # 3. 设置标题37 # =======================================================38 labs(title = final_title) + 39 40 # =======================================================41 # 4. 颜色与刻度42 # =======================================================43 scale_fill_gradient(44 low = "white",45 high = "darkred",46 limits = limits,47 oob = scales::squish,48 na.value = "lightgrey",49 guide = guide_colourbar(frame.linewidth = 1, ticks.linewidth = 0.5)50 ) +51 # =======================================================52 # 5. 主题设置53 # =======================================================54 theme(55 text = element_text(family = "serif"),56 plot.title = element_text(size = 16, hjust = 0.5, color = "black", family = "serif", face = "bold"),57 legend.position = "right", # 图例位置58 legend.title = element_blank(),59 plot.background = element_rect(fill = "transparent", color = NA),60 panel.background = element_rect(fill = "transparent", color = NA),61 panel.grid.major = element_blank(),62 panel.grid.minor = element_blank(),63 legend.background = element_rect(fill = "transparent", color = NA),64 legend.key = element_rect(fill = "transparent", color = NA)65 )66 })67 68 return(SFP)69 }70 71 # 提取数据时加入 na.rm 防止意外报错72 all_vals <- c(73 Seurat::FetchData(B73_1, vars = GeneID)[,1],74 Seurat::FetchData(B73_2, vars = GeneID)[,1],75 Seurat::FetchData(Ames21814_1, vars = GeneID)[,1],76 Seurat::FetchData(Ames21814_2, vars = GeneID)[,1]77 )78 79 # 设定统一范围80 global_limits <- c(0, max(all_vals, na.rm = TRUE))81 82 # 画图(传入 limits)83 p_B3 <- SpatialPlot_Function(B73_1, GeneID, GeneID, limits = global_limits)84 p_B7 <- SpatialPlot_Function(B73_2, GeneID, GeneID, limits = global_limits)85 p_T1 <- SpatialPlot_Function(Ames21814_1, GeneID, GeneID, limits = global_limits)86 p_T3 <- SpatialPlot_Function(Ames21814_2, GeneID, GeneID, limits = global_limits)87 88 # =======================================================89 # 核心修改:拼图并合并刻度90 # =======================================================91 # 使用 patchwork 的语法: (上排) / (下排)92 # guides = "collect" 会自动提取所有相同的图例,并只保留一个93 Spatial_plot <- (p_B3 + p_B7) / (p_T1 + p_T3) + 94 patchwork::plot_layout(guides = "collect")95 96 return(Spatial_plot)97}