7.25 主成分图

借助 autoplotly(Tang 2018) 可将函数 stats::prcomp 生成的结果转化为交互图形

pca <- prcomp(iris[c(1, 2, 3, 4)])
plot(pca)

library(autoplotly)
p <- autoplotly(pca,
  data = iris, colour = "Species",
  label = TRUE, label.size = 3, frame = TRUE
)
## Warning: `select_()` was deprecated in dplyr 0.7.0.
## Please use `select()` instead.
## Warning: `group_by_()` was deprecated in dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
p + labs(
  y = "Second Principal Components",
  x = "First Principal Components",
  title = "Principal Components Analysis"
)
p %>% plotly::layout(annotations = list(
  text = "Example Text",
  font = list(
    family = "Courier New, monospace",
    size = 18,
    color = "black"
  ),
  x = 0,
  y = 0,
  showarrow = TRUE
))

ggfortify (Tang, Horikoshi, and Li 2016) 包将主成分分析图转化为静态图形

library(ggfortify)
autoplot(pca, data = iris, colour = 'Species')
主成分分析

图 7.32: 主成分分析

参考文献

Tang, Yuan. 2018. “Autoplotly: An r Package for Automatic Generation of Interactive Visualizations for Statistical Results.” Journal of Open Source Software 3. https://doi.org/10.21105/joss.00657.
Tang, Yuan, Masaaki Horikoshi, and Wenxuan Li. 2016. ggfortify: Unified Interface to Visualize Statistical Results of Popular r Packages.” The R Journal 8 (2): 474–85. https://doi.org/10.32614/RJ-2016-060.