• R语言绘制PCA双标图、碎石图、变量载荷图和变量贡献图


    1、原论文数据双标图

    代码:

    1. setwd("D:/Desktop/0000/R") #更改路径
    2. #导入数据
    3. df <- read.table("Input data.csv", header = T, sep = ",")
    4. # -----------------------------------
    5. #所需的包:
    6. packages <- c("ggplot2", "tidyr", "dplyr", "readr", "ggrepel", "cowplot", "factoextra")
    7. #安装你尚未安装的R包
    8. installed_packages <- packages %in% rownames(installed.packages())
    9. if (any(installed_packages == FALSE)) {
    10. install.packages(packages[!installed_packages])
    11. }
    12. invisible(lapply(packages, library, character.only = TRUE))
    13. # -----------------------------------
    14. # 设置一些颜色、文字的基础设置
    15. # Colors:
    16. CatCol <- c(
    17. CSH = "#586158", DBF = "#C46B39", EBF = "#4DD8C0", ENF = "#3885AB", GRA = "#9C4DC4",
    18. MF = "#C4AA4D", OSH = "#443396", SAV = "#CC99CC", WET = "#88C44D", WSA = "#AB3232"
    19. )
    20. Three_colorblind <- c("#A8AD6F", "#AD6FA8", "#6FA8AD") #c("#809844", "#4f85b0", "#b07495")
    21. graph_elements_dark <- "black"
    22. plot_elements_light <- "gray75"
    23. plot_elements_dark <- "gray25"
    24. # Transparency:
    25. boot_alpha_main <- 0.9
    26. boot_alpha_small <- 0.05
    27. # Text:
    28. # if (n_pcs > 3) {x_angle <- 270; x_adjust <- 0.25} else {x_angle <- 0; x_adjust <- 0} # option to change orientation of x axis text
    29. x_angle <- 0; x_adjust <- 0
    30. title_text <- 9 # Nature Communications: max 7 pt; cowplot multiplier: 1/1.618; 7 pt : 1/1.618 = x pt : 1; x = 7 / 1/1.618; x = 11.326 (round up to integer)
    31. subtitle_text <- 9
    32. normal_text <- 9 # Nature Communications: min 5 pt; cowplot multiplier: 1/1.618; 5 pt : 1/1.618 = x pt : 1; x = 5 / 1/1.618; x = 8.09 (round up to integer)
    33. # Element dimensions:
    34. plot_linewidth <- 0.33
    35. point_shape <- 18
    36. point_size <- 1.5
    37. # Initialize figure lists:
    38. p_biplot <- list(); p_r2 <- list(); p_load <- list(); p_contr <- list(); col_ii <- list()
    39. # Labels:
    40. veg_sub_labels <- c("All Sites", "All Forests", "Evergreen Needle-Forests")
    41. # -----------------------------------
    42. #选择PCA所需的数据
    43. codes_4_PCA <- c("SITE_ID", "IGBP", "GPPsat", "wLL", "wNmass", "wLMA", "RECOmax") # 选择需要的列数据
    44. #执行筛选
    45. df_subset <- df %>%
    46. dplyr::select(all_of(codes_4_PCA))
    47. #运行PCA。dplyr::select(-species):将不需要的列数据去除
    48. pca_result <- FactoMineR::PCA(df_subset %>% dplyr::select(-SITE_ID, -IGBP), scale.unit = T, ncp = 10, graph = F)
    49. # -----------------------------------
    50. #绘图
    51. p1<- fviz_pca_biplot(pca_result,
    52. axes = c(1, 2),
    53. col.ind = df_subset$IGBP, #"grey50",
    54. # col.ind = NA, #plot_elements_light, #"white",
    55. geom.ind = "point",
    56. palette = CatCol,#'futurama',
    57. label = "var",
    58. col.var = plot_elements_dark,
    59. labelsize = 3,
    60. repel = TRUE,
    61. pointshape = 16,
    62. pointsize = 2,
    63. alpha.ind = 0.67,
    64. arrowsize = 0.5)
    65. # -----------------------------------
    66. # 它是ggplot2对象,我们在此基础上进一步修改一下标注。
    67. p1<-p1+
    68. labs(title = "",
    69. x = "PC1",
    70. y = "PC2",
    71. fill = "IGBP") +
    72. guides(fill = guide_legend(title = "")) +
    73. theme(title = element_blank(),
    74. text = element_text(size = normal_text),
    75. axis.line = element_blank(),
    76. axis.ticks = element_blank(),
    77. axis.title = element_text(size = title_text, face = "bold"),
    78. axis.text = element_text(size = normal_text),
    79. #plot.margin = unit(c(0, 0, 0, 0), "cm"),
    80. # legend.position = "none"
    81. legend.text = element_text(size = subtitle_text),
    82. legend.key.height = unit(5, "mm"),
    83. legend.key.width = unit(2, "mm")
    84. )
    85. p1

    参考:Leaf-level coordination principles propagate to the ecosystem scale (https://doi.org/10.1038/s41467-023-39572-5)、主成分分析图

    2、我选用iris数据进行重新绘制测试双标图

    代码:

    1. setwd("D:/Desktop/0000/R") #更改路径
    2. #导入数据
    3. df <- read.table("iris1.csv", header = T, sep = ",")
    4. # -----------------------------------
    5. #所需的包:
    6. packages <- c("ggplot2", "tidyr", "dplyr", "readr", "ggrepel", "cowplot", "factoextra")
    7. #安装你尚未安装的R包
    8. installed_packages <- packages %in% rownames(installed.packages())
    9. if (any(installed_packages == FALSE)) {
    10. install.packages(packages[!installed_packages])
    11. }
    12. invisible(lapply(packages, library, character.only = TRUE))
    13. # -----------------------------------
    14. # 设置一些颜色、文字的基础设置
    15. # Colors:
    16. CatCol <- c(
    17. setosa = "#586158", versicolor = "#C46B39", virginica = "#4DD8C0") # 设置类别颜色
    18. Three_colorblind <- c("#A8AD6F", "#AD6FA8", "#6FA8AD") #c("#809844", "#4f85b0", "#b07495")
    19. graph_elements_dark <- "black"
    20. plot_elements_light <- "gray75"
    21. plot_elements_dark <- "gray25"
    22. # Transparency:
    23. boot_alpha_main <- 0.9
    24. boot_alpha_small <- 0.05
    25. # Text:
    26. # if (n_pcs > 3) {x_angle <- 270; x_adjust <- 0.25} else {x_angle <- 0; x_adjust <- 0} # option to change orientation of x axis text
    27. x_angle <- 0; x_adjust <- 0
    28. title_text <- 9 # Nature Communications: max 7 pt; cowplot multiplier: 1/1.618; 7 pt : 1/1.618 = x pt : 1; x = 7 / 1/1.618; x = 11.326 (round up to integer)
    29. subtitle_text <- 9
    30. normal_text <- 9 # Nature Communications: min 5 pt; cowplot multiplier: 1/1.618; 5 pt : 1/1.618 = x pt : 1; x = 5 / 1/1.618; x = 8.09 (round up to integer)
    31. # Element dimensions:
    32. plot_linewidth <- 0.33
    33. point_shape <- 18
    34. point_size <- 1.5
    35. # Initialize figure lists:
    36. p_biplot <- list(); p_r2 <- list(); p_load <- list(); p_contr <- list(); col_ii <- list()
    37. # Labels:
    38. veg_sub_labels <- c("All Sites", "All Forests", "Evergreen Needle-Forests")
    39. # -----------------------------------
    40. #选择PCA所需的数据
    41. codes_4_PCA <- c("sepal_length", "sepal_width", "petal_length", "petal_width", "species") # 选择需要的列数据
    42. #执行筛选
    43. df_subset <- df %>%
    44. dplyr::select(all_of(codes_4_PCA))
    45. #运行PCA。dplyr::select(-species):将不需要的列数据去除
    46. pca_result <- FactoMineR::PCA(df_subset %>% dplyr::select(-species), scale.unit = T, ncp = 10, graph = F)
    47. # -----------------------------------
    48. #绘图
    49. p1<- fviz_pca_biplot(pca_result,
    50. axes = c(1, 2),
    51. col.ind = df_subset$species, #"grey50",
    52. # col.ind = NA, #plot_elements_light, #"white",
    53. geom.ind = "point",
    54. palette = CatCol,#'futurama',
    55. label = "var",
    56. col.var = plot_elements_dark,
    57. labelsize = 3,
    58. repel = TRUE,
    59. pointshape = 16,
    60. pointsize = 2,
    61. alpha.ind = 0.67,
    62. arrowsize = 0.5)
    63. # -----------------------------------
    64. # 它是ggplot2对象,我们在此基础上修改一下标注。
    65. p1<-p1+
    66. labs(title = "",
    67. x = "PC1",
    68. y = "PC2",
    69. fill = "IGBP") +
    70. guides(fill = guide_legend(title = "")) +
    71. theme(title = element_blank(),
    72. text = element_text(size = normal_text),
    73. axis.line = element_blank(),
    74. axis.ticks = element_blank(),
    75. axis.title = element_text(size = title_text, face = "bold"),
    76. axis.text = element_text(size = normal_text),
    77. #plot.margin = unit(c(0, 0, 0, 0), "cm"),
    78. # legend.position = "none"
    79. legend.text = element_text(size = subtitle_text),
    80. legend.key.height = unit(5, "mm"),
    81. legend.key.width = unit(2, "mm")
    82. )
    83. p1

    3、iris数据进行绘制碎石图、变量载荷图、变量贡献图

    代码:

    1. #加载包
    2. library(dplyr) #用于数据预处理
    3. library(tidyr) #用于数据预处理
    4. library(stringr) #用于字符串处理
    5. library(modelr) #用于自助法重抽样
    6. library(FactoMineR) #用于PCA
    7. library(ade4) #用于PCA
    8. library(factoextra) #用于PCA结果提取及绘图
    9. #所需的包:
    10. packages <- c("ggplot2", "tidyr", "dplyr", "readr", "ggrepel", "cowplot", "factoextra")
    11. #安装你尚未安装的R包
    12. installed_packages <- packages %in% rownames(installed.packages())
    13. if (any(installed_packages == FALSE)) {
    14. install.packages(packages[!installed_packages])
    15. }
    16. invisible(lapply(packages, library, character.only = TRUE))
    17. setwd("D:/Desktop/0000/R") #更改路径
    18. # 加载数据
    19. df <- read.csv("iris.csv",header = T, row.names = 1) # row.names = 1: 第一列为标签,这时赋值给df时就没有这列了
    20. #重抽样
    21. set.seed(123) #设置随机种子
    22. tt=99 #设置重抽样的次数。iris[,-5]:表示去除第5列,因为这是类别
    23. df_boot <- iris[,-5] %>% modelr::bootstrap(n = tt) #重抽样,结果是一个列表,包含499个数据框
    24. #使用循环对每一个数据集进行PCA
    25. #初始化3个空变量
    26. N_PCS <- tibble() #使用维数检验保留的PC数量
    27. pca_stats <- tibble() #变量的贡献和载荷
    28. R2 <- c() #解释方差占比
    29. #使用循环对每一个数据集进行PCA
    30. #初始化3个空变量
    31. N_PCS <- tibble() #使用维数检验保留的PC数量
    32. pca_stats <- tibble() #变量的贡献和载荷
    33. R2 <- c() #解释方差占比
    34. #循环
    35. for (j in 1:tt) {
    36. ##提取第j次bootstrap的数据
    37. dat <- df_boot %>%
    38. slice(j) %>% # 选择第j行
    39. pull(strap) %>% # 提取列表
    40. as.data.frame() # 提取数据集
    41. #使用FactoMineR包执行PCA
    42. pca_result <- FactoMineR::PCA(dat, scale.unit = T, ncp = 4, graph = F) # ncp = 4:降维几个主成分,设置最大即为全部
    43. #使用ade4包执行PCA
    44. # center:指定是否对数据进行中心化,默认为 TRUE。中心化意味着将数据减去各自的均值,使得数据在每个维度上的平均值为零。
    45. # scale:指定是否对数据进行缩放,默认为 TRUE。缩放意味着将数据除以各自的标准差,使得数据在每个维度上的标准差为一。
    46. # scannf:指定是否计算特征值和特征向量,默认为 FALSE。如果设置为 TRUE,则会计算特征值和特征向量。
    47. pca1 <- ade4::dudi.pca(dat, center = TRUE, scale = TRUE, scannf = FALSE, nf = 4) # nf= 4:降维几个主成分,设置最大即为全部
    48. #检测不确定性和显著性
    49. #执行维数检验
    50. pc_tested <-testdim(pca1, nrepet = 999)
    51. ###提取bootstrap数据集的PCA结果
    52. N_PCS <- N_PCS %>%
    53. bind_rows(tibble(strap = j, n_pcs = pc_tested$nb.cor)) #第j次运行的PCA
    54. pca_stats <- bind_rows(pca_stats,
    55. pca_result$var$contrib %>% # add contributions
    56. as_tibble(rownames = "var") %>%
    57. pivot_longer(cols = !var, names_to = "PC", values_to = "contrib") %>%
    58. left_join(pca_result$var$coord %>% # add loadings
    59. as_tibble(rownames = "var") %>%
    60. pivot_longer(cols = !var, names_to = "PC", values_to = "loading"),
    61. by = c("var", "PC")
    62. ) %>%
    63. mutate(PC = str_sub(PC, start = 5), #提取PC名称中的数字
    64. strap = j) # bootstrap run number
    65. ) #得到变量贡献和载荷
    66. R2 <- bind_rows(R2,
    67. tibble(PC = pca_result[["eig"]]%>% rownames(),
    68. exp_var = pca_result[["eig"]][,2],
    69. strap = j) %>%
    70. mutate(PC = str_sub(PC, start = 6)) #提取PC名称中的数字
    71. )
    72. }
    73. #保留的PC数量
    74. N_PCS <- N_PCS %>%
    75. group_by(n_pcs) %>%
    76. summarise(n_rep = n()) %>% #对重复值进行计数
    77. mutate(retained = n_rep / tt * 100) #计算运行次数百分比
    78. pc_ret <- N_PCS %>% filter(retained == max(retained))
    79. #输出结果的摘要
    80. print(paste0("Number of statistical significant components according to Dray method (Dray et al., 2008) was ",pc_ret[1,1], " in ", round(pc_ret[1,3], digits = 1), "% of runs."))
    81. n_pcs <- NA #保留PC数的初始设置
    82. # n_pcs <- 2 #可以手动设置保留PC数
    83. if (is.na(n_pcs)) {
    84. n_pcs <- N_PCS %>%
    85. filter(retained == max(retained)) %>%
    86. select(n_pcs) %>%
    87. unlist() %>% unname()
    88. } #按照Dray等人的方法设置保留PC数
    89. ##变量贡献和载荷
    90. pca_stats <- pca_stats %>%
    91. group_by(PC, var) %>%
    92. mutate(
    93. contrib_mean = mean(contrib),
    94. contrib_median = median(contrib),
    95. contrib_std = sd(contrib),
    96. # contrib_q25 = quantile(contrib, 0.25), contrib_q75 = quantile(contrib, 0.75),
    97. loading_mean = mean(loading),
    98. loading_median = median(loading),
    99. loading_std = sd(loading),
    100. # loading_q25 = quantile(loading, 0.25), loading_q75 = quantile(loading, 0.75)
    101. ) %>%
    102. ungroup() %>%
    103. dplyr::rename(contrib_boot = contrib, loading_boot = loading) #重命名以免后续的匹配过程出现混乱
    104. ##修改PC名称
    105. pca_stats <- pca_stats %>%
    106. mutate(PC_name = paste0("PC", PC))
    107. ##解释方差占比
    108. R2 <- R2 %>%
    109. group_by(PC) %>%
    110. mutate(
    111. R2_mean = mean(exp_var),
    112. R2_median = median(exp_var),
    113. R2_std = sd(exp_var),
    114. # R2_q25 = quantile(exp_var, 0.25), R2_q75 = quantile(exp_var, 0.75)
    115. ) %>%
    116. ungroup() %>%
    117. dplyr::rename(R2_boot = exp_var) #重命名以免后续的匹配过程出现混乱
    118. ##添加到pca_stats的表格中
    119. pca_stats <- pca_stats %>% left_join(R2, by = c("PC", "strap"))
    120. #对原始数据的PCA
    121. pca_result <- FactoMineR::PCA(iris[,-5], scale.unit = T, ncp = 4, graph = F)
    122. #添加原始数据计算得到的实际值
    123. pca_stats <- pca_stats %>%
    124. dplyr::left_join( #添加原始数据的R2(不是bootstrapping的均值)
    125. tibble(PC = pca_result[["eig"]] %>% rownames(),
    126. R2 = pca_result[["eig"]][,2]
    127. ) %>%
    128. mutate(PC = str_sub(PC, start = 6)), #提取PC数
    129. by = "PC"
    130. ) %>%
    131. dplyr::left_join( #添加原始数据的变量贡献(不是bootstrapping的均值)
    132. pca_result$var$contrib %>% #添加贡献
    133. as_tibble(rownames = "var") %>%
    134. pivot_longer(cols = !var, names_to = "PC", values_to = "contrib") %>%
    135. mutate(PC = str_sub(PC, start = 5)), #提取PC数
    136. by = c("PC", "var")
    137. ) %>%
    138. dplyr::left_join( #添加原始数据的变量载荷(不是bootstrapping的均值)
    139. pca_result$var$coord %>% #添加载荷
    140. as_tibble(rownames = "var") %>%
    141. pivot_longer(cols = !var, names_to = "PC", values_to = "loading") %>%
    142. mutate(PC = str_sub(PC, start = 5)), # extract PC numbers
    143. by = c("PC", "var")
    144. )
    145. ## 添加PC数的保留百分比(在自助法中PC被保留得有多频繁)
    146. pca_stats <- pca_stats %>%
    147. dplyr::left_join(N_PCS %>% dplyr::mutate(PC = n_pcs %>% as.character) %>% dplyr::select(PC, retained),
    148. by = "PC"
    149. )
    150. # -----------------------------------
    151. # 绘制图碎石图
    152. dat_boot <- pca_stats %>%
    153. dplyr::select(PC_name, PC, R2_boot) %>% unique()%>% #去除重复
    154. dplyr::mutate(PC = as.character(PC))
    155. dat_true <- pca_stats %>%
    156. dplyr::select(PC_name, PC, R2, R2_median, R2_std) %>% unique() %>% #去除重复
    157. dplyr::mutate(PC = as.character(PC))
    158. p2 <- ggplot(data = dat_true, aes(x = PC_name, y = R2, group = 1)) + # x = PC -> only numbers on axis, x = PC_name -> can give problems with PC10 being ordered before PC2;
    159. # group 1 是用来避免某些warning/error的
    160. geom_errorbar(aes(ymin = R2 - R2_std, ymax = R2 + R2_std),
    161. color = Three_colorblind[1], linewidth = plot_linewidth, width = 0.4) + # bootstrapping的标准差
    162. # geom_bar(stat = "identity", position = position_dodge(), fill = Three_colorblind[1], width = 0.61) + #b07a4f, #9c6a5e, #643c3c
    163. geom_line(color = Three_colorblind[1]) +
    164. geom_point(color = Three_colorblind[1], size = point_size) + #实际值
    165. geom_jitter(data = dat_boot, aes(x = PC_name, y = R2_boot, group = 1), alpha = 0.1,
    166. color = "black", shape = point_shape, size = 0.5, width = 0.1) + #每次自助样本的值
    167. geom_point(aes(x = PC_name, y = R2_median), color = plot_elements_dark,
    168. alpha = boot_alpha_main, shape = point_shape, size = point_size) + #添加自助法得到的中位数值
    169. geom_text(aes(x = PC_name, y = R2 + R2_std + 2, label = paste0(R2 %>% round(digits = 1), "%")),
    170. nudge_x = 0.33, size = 2) + #添加数值标注
    171. labs(title = "", x = "", y = "Explained variance") +
    172. theme_classic() +
    173. theme(title = element_blank(),
    174. text = element_text(size = normal_text),
    175. axis.line = element_line(color = graph_elements_dark),
    176. axis.ticks.x = element_line(color = graph_elements_dark),
    177. axis.ticks.y = element_blank(),
    178. axis.title = element_text(size = title_text, face = "bold"),
    179. # axis.title.x = element_blank(), #已经在'labs'中指定
    180. axis.text = element_text(size = normal_text),
    181. axis.text.y = element_blank(),
    182. plot.margin = unit(c(0, 1, 0, 1), "cm"),
    183. legend.position = "none"
    184. ) +
    185. NULL
    186. p2
    187. # -----------------------------------
    188. # 绘制变量载荷图
    189. dat_boot <- pca_stats %>%
    190. dplyr::filter(PC <= n_pcs[1]) %>% #去除额外的PC
    191. dplyr::select(PC_name, var, loading_boot) %>% unique() #去除重复
    192. dat_true <- pca_stats %>%
    193. dplyr::filter(PC <= n_pcs[1]) %>% #去除额外的PC
    194. dplyr::select(PC_name, var, loading, loading_median, loading_std) %>% unique() #去除重复
    195. p3 <- ggplot(data = dat_true, aes(x = var, y = loading)) +
    196. facet_grid(. ~ PC_name, scales = "free_y") +
    197. geom_errorbar(aes(ymin = loading - loading_std, ymax = loading + loading_std), # loading_q25, ymax = loading_q75
    198. color = Three_colorblind[2], linewidth = plot_linewidth, width = 0.9) + # standard error = std from bootstrapping
    199. geom_bar(stat = "identity", position = position_dodge(), fill = Three_colorblind[2]) + #b07a4f, #9c6a5e, #643c3c
    200. geom_hline(yintercept = 0, color = graph_elements_dark) +
    201. geom_jitter(data = dat_boot, aes(x = var, y = loading_boot), alpha = boot_alpha_small, color = plot_elements_dark,
    202. shape = point_shape, size = 0.2, width = 0.1) + #每次自助抽样的值
    203. geom_point(aes(x = var, y = loading_median), alpha = boot_alpha_main, shape = point_shape,
    204. size = point_size, color = plot_elements_dark) + #添加自助法得到的中位数值
    205. coord_flip() + #对调坐标轴以更好地展示图形
    206. scale_y_continuous(breaks = waiver(), n.breaks = 4) + #修改x轴(对调后,这就是y轴)
    207. labs(y = "Loadings", x = "", title = "") +
    208. theme_classic() +
    209. theme(title = element_text(size = normal_text, face = "bold"),
    210. text = element_text(size = normal_text),
    211. axis.line.x = element_line(color = graph_elements_dark),
    212. axis.line.y = element_blank(),
    213. axis.ticks.x = element_line(color = graph_elements_dark),
    214. axis.ticks.y = element_blank(),
    215. axis.title = element_text(size = title_text),
    216. axis.text = element_text(size = normal_text),
    217. axis.text.x = element_text(angle = x_angle, vjust = x_adjust),
    218. legend.position = "none",
    219. legend.title = element_text(size = title_text),
    220. legend.text = element_text(size = subtitle_text),
    221. legend.key.height = unit(1.0, "mm"),
    222. legend.key.width = unit(1.0, "mm"),
    223. plot.margin = unit(c(0, 0, 0, 0), "cm"),
    224. strip.text = element_text(face = "bold", size = title_text),
    225. strip.background = element_blank()
    226. ) +
    227. NULL
    228. p3
    229. # -----------------------------------
    230. # 绘制变量贡献图
    231. dat_boot <- pca_stats %>%
    232. dplyr::filter(PC <= n_pcs[1]) %>% #去除额外的PC
    233. dplyr::select(PC_name, var, contrib_boot) %>% unique() #去除重复
    234. dat_true <- pca_stats %>%
    235. dplyr::filter(PC <= n_pcs[1]) %>% # remove additional PCs
    236. dplyr::select(PC_name, var, contrib, contrib_median, contrib_std) %>% unique() #去除重复
    237. p4<- ggplot(data = dat_true, aes(x = var, y = contrib)) +
    238. facet_grid(. ~ PC_name, scales = "free_y") +
    239. geom_errorbar(aes(ymin = contrib_median - contrib_std, ymax = contrib_median + contrib_std), # ymin = contrib_q25, ymax = contrib_q75
    240. color = Three_colorblind[3], linewidth = plot_linewidth, width = 0.9) + # standard error = standard deviation from bootstrapping
    241. geom_bar(stat = "identity", position = position_dodge(), fill = Three_colorblind[3]) + #4f85b0, #59918e, #3c6464
    242. geom_hline(yintercept = 0, color = graph_elements_dark) +
    243. geom_jitter(data = dat_boot, aes(x = var, y = contrib_boot), alpha = boot_alpha_small, color = plot_elements_dark,
    244. shape = point_shape, size = 0.2, width = 0.1) + #每次自助抽样的值
    245. geom_point(aes(x = var, y = contrib_median), alpha = boot_alpha_main, shape = point_shape,
    246. size = point_size, color = plot_elements_dark) + #添加自助法得到的中位数值
    247. coord_flip() + #对调坐标轴以更好地展示图形
    248. scale_y_continuous(breaks = waiver(), n.breaks = 4) + #添加自助法得到的中位数值
    249. labs(y = "Contribution [%]", x = "", title = "") +
    250. theme_classic() +
    251. theme(title = element_text(size = normal_text, face = "bold"),
    252. text = element_text(size = normal_text),
    253. axis.line.x = element_line(color = graph_elements_dark),
    254. axis.line.y = element_blank(),
    255. axis.ticks.x = element_line(color = graph_elements_dark),
    256. axis.ticks.y = element_blank(),
    257. axis.title = element_text(size = title_text),
    258. axis.text = element_text(size = normal_text),
    259. axis.text.x = element_text(angle = x_angle, vjust = x_adjust),
    260. legend.position = "none",
    261. legend.title = element_text(size = title_text),
    262. legend.text = element_text(size = subtitle_text),
    263. legend.key.height = unit(1.0, "mm"),
    264. legend.key.width = unit(1.0, "mm"),
    265. # plot.margin = unit(c(0, 0, 0, 0), "cm"),
    266. strip.text = element_text(face = "bold", size = title_text),
    267. strip.background = element_blank()
    268. ) +
    269. NULL
    270. p4
    271. # -----------------------------------
    272. # 拼图
    273. library(patchwork)
    274. p2+p3/p4

    文中用到的数据代码:R语言绘制 PCA 双标图、碎石图、变量载荷图和变量贡献图(self).zip - 蓝奏云

  • 相关阅读:
    一次不成功的抓包过程
    136. 只出现一次的数字
    【计算机网络】应用层自定义协议
    CPU和GPU有什么区别?
    我们该如何运营Facebook账号呢?
    条件式注解@ConfigurationOnXXX原理详解
    【C++初阶】前言——C++的发展简述及学习方法分享
    2023年9月21日
    基于Matlab使用地面雷达探测和跟踪LEO卫星星座仿真(附源码)
    golang数组和Slice地址
  • 原文地址:https://blog.csdn.net/qq_45100200/article/details/132941674