简体   繁体   English

用两个变量标记 R 中的 biplot?

[英]biplot in R labeling by two variables?

Hello I have a multivariate dataset你好,我有一个多元数据集

I performed a PCA over my scaled data and used fviz_pca_biplot() function for displaying a biplot.我对缩放数据执行了 PCA,并使用fviz_pca_biplot()函数来显示双标图。

this is a example of what I did:这是我所做的一个例子:

(example with mtcars) (以 mtcars 为例)

colnames(mtcars)

"mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb" “mpg”“cyl”“disp”“hp”“drat”“wt”“qsec”“vs”“am”“gear”“carb”

ds <- mtcars %>%
  scale()
PCA_analysis <- princomp(ds)
fviz_pca_biplot(PCA_analysis, label = "all", habillage =  mtcars$gear) 

pca_mtcars

The issue is that I want to display the labeling not only by "gear" variable but by "am" variable of mtcars dataset, in a way that the point colors are relative to "gear" variable and the shape of the points are relative of the "am" variable问题是我想不仅通过“gear”变量而且通过 mtcars 数据集的“am”变量来显示标签,点颜色与“gear”变量相关,点的形状与“am”变量

No easy way to do it, but you can try only plotting the text:没有简单的方法可以做到,但您可以尝试只绘制文本:

g = fviz_pca_biplot(PCA_analysis, label = "all", 
habillage =  mtcars$gear,geom="text",show.legend=FALSE) 

Then you add the data manually to the ggplot object and complete by calling geom_point() :然后将数据手动添加到 ggplot 对象并通过调用 geom_point() 完成:

g$data$am = factor(mtcars$am)
g$data$gear = factor(mtcars$gear)
g + geom_point(aes(color = gear,shape = am))

在此处输入图片说明

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

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