yongcaihuang-lab/Maize_Stem_Spatial_Project
0
1Violin_plot <- function(GeneID){2 3 # =======================================================4 # 1. 提取基因表达数据5 # =======================================================6 B73_Spot_Data <- Seurat::FetchData(B73, vars = c(GeneID, "annotation_final_detailed"), layer = "data")7 colnames(B73_Spot_Data) <- c("Value", "Tissue")8 B73_Spot_Data$Sample <- "B73"9 B73_Spot_Data$Barcode <- rownames(B73_Spot_Data)10 11 Teo_Spot_Data <- Seurat::FetchData(Ames21814, vars = c(GeneID, "annotation_final_detailed"), layer = "data")12 colnames(Teo_Spot_Data) <- c("Value", "Tissue")13 Teo_Spot_Data$Sample <- "Ames21814"14 Teo_Spot_Data$Barcode <- rownames(Teo_Spot_Data)15 16 All_Spots_Df <- rbind(B73_Spot_Data, Teo_Spot_Data)17 All_Spots_Df$Value <- as.numeric(All_Spots_Df$Value)18 19 # =======================================================20 # 2. 定义 Cell 风格的高级配色21 # =======================================================22 cell_colors <- c(23 "B73" = "#0072B5", 24 "Ames21814" = "#BC3C29" 25 )26 27 # =======================================================28 # 3. 绘制高级感小提琴图29 # =======================================================30 p_final <- ggplot(All_Spots_Df, aes(x = Tissue, y = Value, fill = Sample)) +31 32 # --- 1. 绘制小提琴图 ---33 geom_violin(34 position = position_dodge(width = 0.8),35 scale = "width",36 trim = TRUE,37 alpha = 0.95, 38 color = "#FAF9F6", 39 linewidth = 0.6, 40 width = 0.841 ) +42 43 # --- 2. 添加显著性检验 ---44 ggpubr::stat_compare_means( aes(group = Sample), 45 method = "wilcox.test", 46 label = "p.signif", 47 vjust = 0.5, 48 symnum.args = list( cutpoints = c(0, 1e-4, 1e-3, 1e-2, 5e-2, 1), 49 symbols = c("****","***","**","*","ns") ) )+50 51 # --- 3. 应用自定义 Cell 配色 ---52 scale_fill_manual(values = cell_colors) +53 54 # --- 4. 主题设置 ---55 theme_bw() + 56 57 labs(58 y = "Expression Level", 59 x = NULL,60 caption = "**** p < 1e-4, *** p < 1e-3, ** p < 1e-2, * p < 5e-2, ns p > 5e-2" 61 ) 62 63 theme(64 text = element_text(family = "serif"),65 plot.title = element_blank(),66 67 # 坐标轴文字68 axis.text.x = element_text(angle = 45, hjust = 1, size = 11, face = "bold", color = "black"),69 axis.text.y = element_text(size = 10, color = "black"),70 axis.title.y = element_text(size = 12, face = "bold", margin = margin(r = 10)),71 72 # ==========================================73 # 修改点:图例文字加粗、纯黑74 # ==========================================75 legend.position = "right",76 legend.title = element_blank(),77 legend.text = element_text(size = 11, face = "bold", color = "black"), 78 79 # 底部注释80 plot.caption = element_text(hjust = 0, size = 10, face = "italic", color = "black"),81 82 # 去掉网格线83 panel.grid.major = element_blank(),84 panel.grid.minor = element_blank(),85 86 # 去掉矩形框,只留 L 型坐标轴 87 panel.border = element_blank(),88 axis.line = element_line(color = "black", linewidth = 0.8),89 axis.ticks = element_line(color = "black", linewidth = 0.8)90 )91 92 return(p_final)93}