简体   繁体   English

在 R 中仅绘制选定的载荷

[英]Plotting only selected loadings in R

I have a PCA with more than 150 variables, when plotting the loadings the PCA become obviously a mess.我有一个包含 150 多个变量的 PCA,当绘制负载时,PCA 显然变得一团糟。 Is there a way to plot only selected loadings?有没有办法只绘制选定的载荷? As an example: with iris I end up with 4 loadings, how can I only plot 1 (let say Sepal.Width).举个例子:使用 iris 我最终得到 4 个负载,我怎么能只绘制 1 个(比如说 Sepal.Width)。

library(ggfortify)
df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)
autoplot(pca_res, data = iris, colour = 'Species', loadings = TRUE, loadings.label=1)

PCA example with iris and 4 loadings带有 iris 和 4 个载荷的 PCA 示例

1

Hi Werc welcome to SO:嗨 Werc 欢迎来到 SO:

a small disclaimer: This is not a proper Solution to this missing feature but more of a hack, a proper solution imo, would contain editing the source code of the ggfortify package and opening a pull request (or opening a feature request on github ).一个小的免责声明:这不是这个缺失功能​​的正确解决方案,而是更多的 hack,一个正确的解决方案 imo,将包含编辑ggfortify包的源代码并打开拉取请求(或在 github 上打开功能请求)。

However here's a little "hack" to help you for now by editing the ggplot object:但是,现在通过编辑 ggplot 对象,这里有一个小“技巧”​​可以帮助您:

library(ggfortify)
df <- iris[1:4]
pca_res <- prcomp(df, scale. = TRUE)
p0<-autoplot(pca_res, data = iris, colour = 'Species',loadings=TRUE, loadings.label=1)
p0 # default plot

# check which layers are relevant:
p0$layers # layers 2 (segment) and 3 (text)

# edit ggplot object geom_segment layer:
p0$layers[[2]]$data<-p0$layers[[2]]$data["Sepal.Width",]

# edit ggplot object geom_text layer:
p0$layers[[3]]$data<-p0$layers[[3]]$data["Sepal.Width",]


p0 # new Plot 

This gives you the requested Output of only ''Sepal.Width'' as a loading on your PCA plot:这为您提供了仅“Sepal.Width”的请求输出作为 PCA 图上的负载:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM