1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
   | 
 
 
 
 
 
 
  setup_arial_fonts <- function(force_reload = FALSE, quiet = FALSE) {      if(!quiet) cat("=== 开始配置Arial字体环境 ===\n")         required_packages <- c("extrafont", "Cairo", "ggplot2")   for(pkg in required_packages) {     if(!require(pkg, character.only = TRUE, quietly = quiet)) {       if(!quiet) cat("安装缺失包:", pkg, "\n")       install.packages(pkg)       library(pkg, character.only = TRUE)     }   }         if(force_reload || !"Arial" %in% fonts()) {     if(!quiet) cat("导入Arial字体...\n")     font_import(pattern = "Arial")   }         loadfonts(device = "pdf", quiet = quiet)   loadfonts(device = "postscript", quiet = quiet)         if("Arial" %in% fonts()) {     if(!quiet) cat("✅ Arial字体配置成功\n")     return(TRUE)   } else {     if(!quiet) cat("❌ Arial字体配置失败\n")     return(FALSE)   } }
 
 
 
 
 
  create_scientific_plot <- function(data, x_col, y_col, title = "Scientific Plot") {         if(!setup_arial_fonts(quiet = TRUE)) {     warning("Arial字体配置失败,使用默认字体")   }         p <- ggplot(data, aes_string(x = x_col, y = y_col)) +     geom_col(fill = "#2166AC", alpha = 0.8, width = 0.7) +          labs(title = title,          x = tools::toTitleCase(gsub("_", " ", x_col)),          y = tools::toTitleCase(gsub("_", " ", y_col))) +          theme_classic(base_family = "Arial", base_size = 12) +     theme(       text = element_text(family = "Arial"),       plot.title = element_text(size = 16, face = "bold", hjust = 0.5,                                family = "Arial", margin = margin(b = 20)),       axis.title = element_text(size = 14, face = "bold", family = "Arial"),       axis.text = element_text(size = 11, family = "Arial"),       panel.border = element_rect(color = "black", fill = NA, size = 0.8)     )      return(p) }
 
 
 
 
 
  safe_save_plot <- function(plot, filename, width = 12, height = 8) {         ext <- tools::file_ext(filename)         if(ext == "pdf") {     ggsave(filename, plot, width = width, height = height,            device = cairo_pdf, bg = "white")               tryCatch({       embed_fonts(filename)       cat("✅ 字体已嵌入:", filename, "\n")     }, error = function(e) {       cat("⚠️  字体嵌入失败,但文件已保存\n")     })        } else if(ext %in% c("png", "jpg", "jpeg")) {     ggsave(filename, plot, width = width, height = height,            dpi = 600, bg = "white", type = "cairo")        } else {          ggsave(filename, plot, width = width, height = height, bg = "white")   }      cat("✅ 图表已保存:", filename, "\n") }
 
 
 
  if(setup_arial_fonts()) {         example_data <- data.frame(     treatment = c("Control", "Treatment A", "Treatment B", "Treatment C"),     expression = c(1.0, 2.3, 1.8, 3.1),     se = c(0.1, 0.2, 0.15, 0.25)   )         my_plot <- ggplot(example_data, aes(x = treatment, y = expression)) +     geom_col(fill = "#D73027", alpha = 0.8, width = 0.7) +     geom_errorbar(aes(ymin = expression - se, ymax = expression + se),                   width = 0.2, size = 0.8) +          labs(title = "Gene Expression Analysis",          x = "Treatment Groups",          y = expression(paste("Relative Expression (", mu, "g/ml)"))) +          theme_classic(base_family = "Arial") +     theme(       text = element_text(family = "Arial"),       plot.title = element_text(size = 16, face = "bold", hjust = 0.5,                                family = "Arial"),       axis.title = element_text(size = 14, face = "bold", family = "Arial"),       axis.text = element_text(size = 12, family = "Arial")     )         print(my_plot)   safe_save_plot(my_plot, "final_scientific_plot.pdf", 10, 6)      cat("=== 流程执行完成 ===\n") }
 
  |