You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ggplot2绘制的PCA图中调整loadings.label位置?

解决ggfortify绘制PCA图时载荷标签与箭头重叠的问题

问题背景

使用ggplot2结合ggfortify绘制PCA分析图时,出现载荷标签(loadings.label)与箭头重叠的情况,导致图表可读性下降。以下是相关数据、代码及原图:

部分数据

Linfoprolif CORT Testo  FDL Ac.GRO ifn.g il.4 Profile
1        23.76 0.27  0.96 2.41      6   307   69       1
2           NA 2.59  0.07 0.39      4   117   58        
3        25.53 0.16  0.71 2.17      5   273   54       1
4        31.67 0.88  0.07 0.55      5   211   48       1
5         6.15 0.24  0.23 1.07      5   224   48       1
6        26.19 0.74  0.04 0.60      4   308   59       1
7        10.31 0.34  0.75 2.29      7   295   49       1
8        22.30 0.42  0.07 0.63      5   271   52       1
9        24.74 0.29  1.18 2.91      4   236   56       1
10        9.51 2.19  0.07 0.40      5    54   62       2
11       22.59 0.19  0.40 3.28      4   272   58       1
12       22.01 0.28  0.04 0.54      4    67   64       1
13       39.21 0.21  0.82 1.91      4   235   56       1
14       42.07 0.32  0.16 0.70      5   362   54       3
15       13.45 0.30  0.24 2.21      6   146   68       1
16       15.08 2.19  0.08 0.34      5    58   63       2
17       20.48 0.38  1.27 2.40      4   278   52       1
18       12.10 0.83  0.11 0.53      2   146   41       1
19       61.56 0.07  0.09 1.09      9   305   52       3
20       35.06 0.59  0.05 0.67      4   220   54       1
21       33.48 0.68  0.99 1.24      3   102   58       1
22       20.56 0.94  0.06 1.71      3    58   45       2
23       26.46 0.12  0.29 1.60      3   210   55       1
24       24.91 0.56  0.11 0.55      5   108   56       1
25       29.22 0.42  2.60 1.55      3    84   69       1
26       19.30 1.63  0.02 0.78      3    62   69       2
27       14.45 0.22  0.79 1.89      4   245   59       1
28       20.89 0.72  0.04 0.57      4    85   53       1
29       26.70 0.36  1.02 2.05      3   309   45       1
30       27.83 2.66  0.04 0.54      3    52   65       2
31       34.70 0.46  0.83 1.39      5   120   65       1

原绘图代码

library(ggfortify)
p_pca<-d_e_b[c(1,2,3,4,5,6,7)]

p_pca<-na.omit(p_pca)
pca_res <- prcomp(p_pca, scale. = TRUE)


pca_b<-autoplot(pca_res, data = d_e_b, colour = "Profile",
                loadings = TRUE, loadings.colour = 'gray30',loadings.size = 5,
                loadings.label = TRUE, loadings.label.color='black',
                loadings.label.size = 4) + theme_classic()+
  scale_colour_discrete("Profile")+
  theme(text = element_text(size = 20 ),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
        axis.text.y=element_text(colour="black"))
pca_b

重叠问题的PCA图

PCA plot


解决方法

方法1:使用ggrepel自动避免标签重叠

借助ggrepel包的geom_text_repel函数,替代ggfortify默认的载荷标签,自动调整标签位置避免重叠。

代码示例:

library(ggfortify)
library(ggrepel)

p_pca<-d_e_b[c(1,2,3,4,5,6,7)]
p_pca<-na.omit(p_pca)
pca_res <- prcomp(p_pca, scale. = TRUE)

# 提取载荷数据
loadings_data <- as.data.frame(pca_res$rotation[, 1:2])
loadings_data$var <- rownames(loadings_data)

# 绘制PCA图,不使用默认标签,手动添加repel标签
pca_b<-autoplot(pca_res, data = d_e_b, colour = "Profile",
                loadings = TRUE, loadings.colour = 'gray30',loadings.size = 5,
                loadings.label = FALSE) + # 关闭默认标签
  geom_text_repel(data = loadings_data, aes(x = PC1, y = PC2, label = var),
                  color = 'black', size = 4) +
  theme_classic()+
  scale_colour_discrete("Profile")+
  theme(text = element_text(size = 20 ),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
        axis.text.y=element_text(colour="black"))
pca_b

方法2:微调标签的偏移量

使用ggfortify自带的loadings.label.vjust和loadings.label.hjust参数,手动调整标签的垂直/水平偏移,远离箭头。

代码示例:

library(ggfortify)

p_pca<-d_e_b[c(1,2,3,4,5,6,7)]
p_pca<-na.omit(p_pca)
pca_res <- prcomp(p_pca, scale. = TRUE)

pca_b<-autoplot(pca_res, data = d_e_b, colour = "Profile",
                loadings = TRUE, loadings.colour = 'gray30',loadings.size = 5,
                loadings.label = TRUE, loadings.label.color='black',
                loadings.label.size = 4,
                loadings.label.vjust = -0.8, # 垂直向上偏移
                loadings.label.hjust = 0.5) + # 水平居中
  theme_classic()+
  scale_colour_discrete("Profile")+
  theme(text = element_text(size = 20 ),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
        axis.text.y=element_text(colour="black"))
pca_b

注:可根据实际重叠情况调整vjust和hjust的数值(正负控制方向)。

方法3:放大箭头与标签的间距

通过减小loadings.size让箭头更短,同时配合标签偏移,拉开两者距离;或者自定义箭头长度,再添加标签。

代码示例:

library(ggfortify)

p_pca<-d_e_b[c(1,2,3,4,5,6,7)]
p_pca<-na.omit(p_pca)
pca_res <- prcomp(p_pca, scale. = TRUE)

pca_b<-autoplot(pca_res, data = d_e_b, colour = "Profile",
                loadings = TRUE, loadings.colour = 'gray30',loadings.size = 3, # 缩小箭头长度
                loadings.label = TRUE, loadings.label.color='black',
                loadings.label.size = 4,
                loadings.label.vjust = -0.5) + # 标签上移
  theme_classic()+
  scale_colour_discrete("Profile")+
  theme(text = element_text(size = 20 ),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
        axis.text.y=element_text(colour="black"))
pca_b

方法4:完全自定义载荷绘制(最高灵活性)

手动用geom_segment绘制箭头,geom_text或geom_text_repel添加标签,完全控制每个元素的位置。

代码示例:

library(ggplot2)
library(ggrepel)

p_pca<-d_e_b[c(1,2,3,4,5,6,7)]
p_pca<-na.omit(p_pca)
pca_res <- prcomp(p_pca, scale. = TRUE)

# 提取样本点的PCA坐标
sample_data <- as.data.frame(pca_res$x[, 1:2])
sample_data$Profile <- d_e_b$Profile[match(rownames(sample_data), rownames(d_e_b))]

# 提取载荷数据,可缩放载荷值调整箭头长度
loadings_data <- as.data.frame(pca_res$rotation[, 1:2] * 1.2) # 1.2为缩放系数,控制箭头长度
loadings_data$var <- rownames(loadings_data)

# 手动绘制PCA图
ggplot(sample_data, aes(x = PC1, y = PC2, color = Profile)) +
  geom_point(size = 3) +
  # 绘制载荷箭头
  geom_segment(data = loadings_data, aes(x = 0, y = 0, xend = PC1, yend = PC2),
               color = 'gray30', arrow = arrow(length = unit(0.2, "cm"))) +
  # 添加repel标签
  geom_text_repel(data = loadings_data, aes(x = PC1, y = PC2, label = var),
                  color = 'black', size = 4) +
  theme_classic()+
  scale_colour_discrete("Profile")+
  theme(text = element_text(size = 20 ),
        axis.line.x = element_line(color="black", size = 1),
        axis.line.y = element_line(color="black", size = 1),
        axis.text.x=element_text(colour="black",angle = 360,vjust = 0.6),
        axis.text.y=element_text(colour="black"))

内容的提问来源于stack exchange,提问作者MyName

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 02:48:21