今天介绍一个tidydr
包,还是来自于y叔,这个包就做一件事,可视化各种降维结果,比如大家耳熟能详的pca、pcoa、umap、tsne等等。
install.packages("tidydr")
devtools::install_github("YuLab-SMU/tidydr")
library(tidydr)
# 查看支持的方法
available_methods()
## The `dr()` function works for the following methods that
## require data matrix (or data frame) as input:
## + stats::prcomp()
## + Rtsne::Rtsne()
## + uwot::umap()
## + uwot::tumap()
## + uwot::lvish()
## The `dr()` function works for the following methods that
## require distance matrix (or distance object) as input:
## + stats::cmdscale()
## + MASS::sammon()
## + vegan::metaMDS()
## + ape::pcoa()
## + smacof::mds()
## + vegan::wcmdscale()
## + ecodist::pco()
## + labdsv::pco()
## + ade4::dudi.pco()
可以看到支持原始数据的方法有5种,支持距离矩阵的方法有9种!基本上常见的方法全都包括了!
使用非常简单!2步即可完成,方法是通用的。
# 首先选择方法
x <- dr(data = iris[,1:4], fun = prcomp)
# 然后用ggplot2画图,就是这么简单!
library(ggplot2)
## metadata as a vector
ggplot(x, aes(Dim1, Dim2), metadata=iris$Species) +
geom_point(aes(color=.group))+theme_minimal()
plot of chunk unnamed-chunk-3
为了满足大家的审美,专门支持了小坐标轴箭头!
autoplot(x, aes(color=Species), metadata = iris[, 5, drop=FALSE]) +
theme_dr()
plot of chunk unnamed-chunk-4
快来尝试一下吧~