(R)使用PCA(ggbiplot)可视化具有大量变量的数据集

时间:2023-01-19 16:54:54

My dataset has 100 samples and 17000 variables. I would use PCA and visualize data. But the problem is that the plot is not good. How I can control the number of arrows in ggbiplot or biplot, in fact select the most contributed variables? Some sample codes are as below:

我的数据集有100个样本和17000个变量。我会使用PCA并可视化数据。但问题是情节不好。我如何控制ggbiplot或biplot中的箭头数量,实际上选择贡献最多的变量?一些示例代码如下:

data <- matrix(rnorm(1700000), nrow=100, ncol=17000)
colnames(data) <- paste("X", 1:ncol(data), sep="")
pca <- prcomp(data, scale=T, center=T)

biplot(pca)
print(ggbiplot(pca, obs.scale = 1, var.scale = 1, 
               groups = c(rep('a',30), rep('b',70))))

(R)使用PCA(ggbiplot)可视化具有大量变量的数据集

1 个解决方案

#1


2  

I assumed you got a recent version of ggbiplot from github (19 Jun 2015 https://github.com/vqv/ggbiplot). In this one, I don't think there's a clean way to reduce the number of arrows. You'd have to modify the original function by subsetting the df.v in two plotting calls:

我假设你从github获得了最新版本的ggbiplot(2015年6月19日https://github.com/vqv/ggbiplot)。在这一个中,我认为没有一种减少箭头数量的干净方法。您必须通过在两个绘图调用中对df.v进行子集化来修改原始函数:

around line 89:

89号线附近:

g <- g + geom_segment(data = df.v[1:5,], # SUBSET HERE
aes(x = 0, y = 0, xend = xvar, yend = yvar), arrow = arrow(length = unit(1/2, "picas")), color = muted("red"))

and around line 127:

在第127行附近:

g <- g + geom_text(data = df.v[1:5,], # SUBSET HERE
aes(label = varname, x = xvar, y = yvar, angle = angle, hjust = hjust), color ="darkred", size = varname.size)

(R)使用PCA(ggbiplot)可视化具有大量变量的数据集

#1


2  

I assumed you got a recent version of ggbiplot from github (19 Jun 2015 https://github.com/vqv/ggbiplot). In this one, I don't think there's a clean way to reduce the number of arrows. You'd have to modify the original function by subsetting the df.v in two plotting calls:

我假设你从github获得了最新版本的ggbiplot(2015年6月19日https://github.com/vqv/ggbiplot)。在这一个中,我认为没有一种减少箭头数量的干净方法。您必须通过在两个绘图调用中对df.v进行子集化来修改原始函数:

around line 89:

89号线附近:

g <- g + geom_segment(data = df.v[1:5,], # SUBSET HERE
aes(x = 0, y = 0, xend = xvar, yend = yvar), arrow = arrow(length = unit(1/2, "picas")), color = muted("red"))

and around line 127:

在第127行附近:

g <- g + geom_text(data = df.v[1:5,], # SUBSET HERE
aes(label = varname, x = xvar, y = yvar, angle = angle, hjust = hjust), color ="darkred", size = varname.size)

(R)使用PCA(ggbiplot)可视化具有大量变量的数据集