PCA 结果的 R ggbiplot:为什么结果图这么窄以及如何调整宽度?

R ggbiplot for PCA results: why is the resulting plot so narrow and how to adjust the width?

所以我做了一个 PCA 分析,我通常用 ggplot2 绘制结果,但我最近发现了 ggbiplot,它可以用变量显示箭头。

ggbiplot 似乎工作正常,尽管它显示出一些问题(比如无法更改点大小,因此我在 MWE 中做的整个图层)。

我现在面临的问题是,虽然 ggplot2 绘图会根据绘图区域调整绘图宽度,但 ggbiplot 不会。根据我的数据,ggbiplot 非常窄并且垂直边距非常宽,即使它扩展了与 ggplot2 图相同的 x 轴间隔(实际上是相同的图)。

我在这里使用 iris 数据,所以我不得不将 png 宽度设置得特别大,这样我面临的问题就变得很明显了。请检查下面的 MWE:

data(iris)
head(iris)
pca.obj <- prcomp(iris[,1:4],center=TRUE,scale.=TRUE)
pca.df <- data.frame(Species=iris$Species, as.data.frame(pca.obj$x))
rownames(pca.df) <- NULL
png(filename="test1.png", height=500, width=1000)
print(#or ggsave()
  ggplot(pca.df, aes(x=PC1, y=PC2)) +
  geom_point(aes(color=Species), cex=3)
)
dev.off()
P <- ggbiplot(pca.obj,
         obs.scale = 1, 
         var.scale=1,
         ellipse=T,
         circle=F,
         varname.size=3,
         groups=iris$Species, #no need for coloring, I'm making the points invisible
         alpha=0) #invisible points, I add them below
P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test2.png", height=500, width=1000)
print(#or ggsave()
    P
)
dev.off()

此代码生成以下两个图像。

ggplot2 输出(所需的绘图宽度):

ggbiplot 输出(绘图区域太窄):

看看如何,当 ggplot2 将绘图宽度调整到绘图区域时,ggbiplot 不会。根据我的数据,ggbiplot 图非常窄,垂直边距很大。

我的问题是:如何让 ggbiplot 表现得像 ggplot2?如何使用 ggbiplot 将绘图宽度调整到我想要的绘图区域(png 大小)?谢谢!

coord_equal() 中的 ratio 参数更改为小于 1 的某个值(ggbiplot() 中的默认值)并将其添加到您的绘图中。来自函数描述:"Ratios higher than one make units on the y axis longer than units on the x-axis, and vice versa."

P + coord_equal(ratio = 0.5)

注意:正如@Brian 在评论中指出的那样,"changing the aspect ratio would bias the interpretation of the length of the principal component vectors, which is why it's set to 1."