CoolFace
Apppublic

yongcaihuang-lab/Maize_Stem_Spatial_Project

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
Umap_plot.R89 linesDownload Raw Back to Shiny
1All_Feature_Plot <- function(GeneID){2  3  FeatureDimplot_function <- function(Sample, ID, limits = NULL) {4    5    # =======================================================6    # 1. 自动匹配样本名称逻辑 (已修复)7    # =======================================================8    # 获取传入对象的变量名作为字符串9    var_name <- deparse(substitute(Sample))10    11    # [修复] 映射关系:传入的变量名 (Key) = 图上想显示的标题 (Value)12    name_map <- c(13      "B73" = "B73",14      "Ames21814" = "Ames21814" # 识别到 Ames21814 对象时,标题显示为 Teo15    )16    17    # 获取最终标题18    final_title <- name_map[var_name]19    20    # [修复] 如果没匹配到,直接用变量名本身,去掉了未定义的 current_ident21    if (is.na(final_title)) final_title <- var_name 22    23    # =======================================================24    # 2. 生成基础绘图对象25    # =======================================================26    plot <- SCP::FeatureDimPlot(27      srt = Sample,28      features = ID,29      reduction = "UMAP",30      theme_use = "theme_blank",31      assay = "SCT",32      pt.size = 1,33      pt.alpha = 0.8,34      theme_args = list(strip.text = ggplot2::element_blank())35    ) +36      labs(title = final_title) + 37      theme(38        text = element_text(family = "serif"), 39        plot.title = element_text(hjust = 0.5, size = 16, family = "serif", face = "bold"),40        plot.background = element_rect(fill = "transparent", color = NA),41        panel.background = element_rect(fill = "transparent", color = NA),42        panel.grid.major = element_blank(),43        panel.grid.minor = element_blank(),44        legend.background = element_rect(fill = "transparent", color = NA),45        legend.key = element_rect(fill = "transparent", color = NA),46        legend.text = element_text(family = "serif"),47        legend.title = element_text(family = "serif")48      )49    50    # =======================================================51    # 3. 修改内部对象以统一 Scale (保留原逻辑)52    # =======================================================53    if (!is.null(limits)) {54      scale_idx <- which(sapply(plot$scales$scales, function(x) "colour" %in% x$aesthetics))55      56      if (length(scale_idx) > 0) {57        plot$scales$scales[[scale_idx]]$limits <- limits58        plot$scales$scales[[scale_idx]]$oob <- scales::squish 59      } else {60        scale_idx_fill <- which(sapply(plot$scales$scales, function(x) "fill" %in% x$aesthetics))61        if(length(scale_idx_fill) > 0){62          plot$scales$scales[[scale_idx_fill]]$limits <- limits63          plot$scales$scales[[scale_idx_fill]]$oob <- scales::squish 64        }65      }66    }67    68    return(plot)69  }70  71  # =======================================================72  # 获取极值与拼图73  # =======================================================74  # [修复] 提取数据时加入 na.rm = TRUE 防御性编程75  all_vals <- c(76    Seurat::FetchData(B73, vars = GeneID)[,1], 77    Seurat::FetchData(Ames21814, vars = GeneID)[,1]78  )79  my_limits <- c(0, max(all_vals, na.rm = TRUE))80  81  # 传入 limits82  B73_UMAP <- FeatureDimplot_function(B73, GeneID, limits = my_limits)83  Teo_UMAP <- FeatureDimplot_function(Ames21814, GeneID, limits = my_limits)84  85  # 拼图 (确保加载了 patchwork 包)86  All_Feature_plot <- B73_UMAP + Teo_UMAP + patchwork::plot_layout(guides = "collect")87  88  return(All_Feature_plot)89}