使用自动绘图绘制 PCA 双标图:修改箭头粗细

Dro*_*sof 2 r pca ggfortify biplot

我正在使用下面的代码所示的autoplot函数,ggfortify使用iris.pca. 这个例子只有三个变量(因此有 3 个加载),但我的数据集有更多的变量,所以我希望我可以减少箭头的大小以使绘图更具可读性。我认为默认大小是 1 pt,我想把它降到 0.5 之类的东西。虽然有修改加载颜色和标签大小的参数,但我找不到调整箭头粗细的方法。有谁知道如何做到这一点?

library(ggplot2)
library(ggfortify)

df <- iris[c(1, 2, 3, 4)]
iris.pca<-(prcomp(df))

autoplot(iris.pca, data=iris, colour="Species", loadings=TRUE, loadings.colour = "black")+
  scale_colour_manual(values=c("forestgreen","red","blue"))+
  scale_fill_manual(values=c("forestgreen","red","blue"))+
  scale_shape_manual(values=c(25,22,23))+
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

Sta*_*ann 5

问题是ggfortify已经创建了一个 ggplot2 对象。因此,如果您不想手动重新创建绘图(这将是这里更清晰的解决方案),则必须按以下方式修改现有绘图:

旧代码

library(ggplot2)
library(ggfortify)

df <- iris[c(1, 2, 3, 4)]
iris.pca<-(prcomp(df))

d <- autoplot(iris.pca, data=iris, colour="Species", loadings=TRUE, loadings.colour = "black", scale = 1)+
  scale_colour_manual(values=c("forestgreen","red","blue")) +
  scale_fill_manual(values=c("forestgreen","red","blue")) +
  scale_shape_manual(values=c(25,22,23))+
  theme_bw()
Run Code Online (Sandbox Code Playgroud)

修改

d$layers[[2]]$aes_params$size <- 0.5
d$layers[[2]]$geom_params$arrow$length <- unit(6, units = "points")
d
Run Code Online (Sandbox Code Playgroud)

这基本上是手动创建箭头线的尺寸美感,并缩小箭头的尖端: 结果图

  • “长度”是指箭头的大小。如果您使线条更细,为了视觉完整性,您还应该使箭头更小。 (2认同)