一種選擇是使用名爲ggbiplot的軟件包,該軟件包可以在圖形方面提供更多的靈活性。我在下面提供了一些代碼來說明它的功能。此外,在進行PCA時擴大變量非常重要,我不確定你是否做過,但只是想仔細檢查。
data("mtcars")
mtcars
# using cylinder as grouping variable, and weight and hp for example
cyl <- mtcars$cyl
cyl <- as.factor(cyl)
xvars <- mtcars[, c(4,7)]
# Need to scale variables for PCA due to the fact that variables with large variance will impact PCA
xvars.scaled <- scale(xvars)
cars.pca <- prcomp(xvars.scaled)
# Create base plot
g.plot <- ggbiplot(cars.pca, obs.scale=1, var.scale=1,
groups=cyl, ellipse=TRUE)
# Add in color
g.plot <- g.plot+ scale_color_discrete(name='')
# add in legend
g.plot <- g.plot+ theme(legend.direction='horizontal',
legend.position='bottom')
g.plot
,其結果是:
更詳盡的例子和教程,也可以通過以下鏈接找到:http://www.r-bloggers.com/computing-and-visualizing-pca-in-r/
我希望這有助於!