如何将PCA载荷添加到ggplot?

问题描述 投票:0回答:1

我已经使用

PCAloadings <- data.frame(Variables = rownames(pca_res_t$rotation), pca_res_t$rotation)
创建了 PCALoadings,但似乎无法实际将载荷添加到我的 ggplot 上。我将发布我使用的确切代码,然后也是可重现的代码。我一直在尝试使用 geom_segment。

这就是我用来制作 ggplot 的东西:

ggplot(data=sep_total_raw_data, aes(x=pc1t, y=pc2t))+
  geom_point(alpha=0.8, size=1,aes(colour=Data_Type, shape=Data_Type))+
  xlab("PC1 (53.83%)")+
  ylab("PC2 (26.3%)")+
  guides(colour=guide_legend(title="Data_Type"))+
  geom_mark_hull(concavity=5, expand=0, radius=0, aes(fill=Data_Type))+
  theme(panel.grid = element_blank(), panel.border = element_rect(fill = "transparent"))+
  geom_text(aes(label=Code, fontface="bold", colour=Data_Type))

这是我尝试过的 geom_segment 东西:

 + geom_segment(data = PCALoadings, aes(x=0,xend=xvar, y=0, yend=yvar), arrow = arrow(length = unit(0.025,"npc"),type = "open"),lwd=1)

但它无法识别 xvar 或 yvar,我已经尝试过了:

+ geom_segment(data = PCALoadings, aes(x=0,xend=(pc1t*2), y=0, yend=(pc2t*2)), arrow = arrow(length = unit(1/2,"picas")), colour="black")

但它说

Error in `geom_segment()`:
! Problem while computing aesthetics.
ℹ Error occurred in the 4th layer.
Caused by error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (5)
✖ Fix the following mappings: `xend` and `yend`
Run `rlang::last_trace()` to see where the error occurred.

这是可重现的代码:

ggplot(data=iris, aes(x=Petal.Length, y=Petal.Width))+
  geom_point(alpha=0.8, size=1,aes(colour=Species, shape=Species))+
  xlab("Petal Length")+
  ylab("Petal Width")+
  guides(colour=guide_legend(title="Species"))+
  geom_mark_hull(concavity=5, expand=0, radius=0, aes(fill=Species))+
  theme(panel.grid = element_blank(), panel.border = element_rect(fill = "transparent"))+geom_text(aes(label=Species, fontface="bold", colour=Species))

感谢大家的帮助!

r ggplot2 pca geom-segment
1个回答
0
投票

所以,如果我理解你的问题,你会想构建一个包含 PCA 的个体和变量(即载荷)的双标图。

您应该尝试 GABB 包,以促进这种表示。

请参阅下面的示例,其中参数 biplot = TRUE

library(GABB)
library(FactoMiner)


## Example of GABB package pipeline with the base data.set "mtcars" 
my.data <- mtcars

## Data preparation for RDA and PCA : tranformation and scaling of numeric/quantitative variables
prep_data(data = my.data, quantitative_columns = c(1:7), transform_data_method = "log", scale_data = T)

## Create PCA
library(FactoMineR)
my.pca <- FactoMineR::PCA(X = data_quant) 


## Create, display and save graphic output of individual and variable PCA

#Basic output with minimum required parameters
PCA_RDA_graphics(complete.data.set = initial_data_with_quant_transformed, PCA.object = my.pca, factor.names = c("vs", "am", "gear", "carb"))

#Advanced outputs (image below)
PCA_RDA_graphics(complete.data.set = initial_data_with_quant_transformed, PCA.object = my.pca, 
                 factor.names = c("vs", "am", "gear", "carb"), Biplot.PCA = TRUE,col.arrow.var.PCA = "grey",
                 Barycenter = TRUE, Segments = TRUE, Ellipse.IC = TRUE,
                 Barycenter.Ellipse.Fac1 = "vs", Barycenter.Ellipse.Fac2 = "am",
                 factor.colors = "vs", factor.shapes = "am",
                 Barycenter.factor.col = "vs", Barycenter.factor.shape = "am")

© www.soinside.com 2019 - 2024. All rights reserved.