ggforify 这个包很值得我写一个文档来介绍他。因为很方便,他是ggplot2包的一个增强版。开始介绍。

生存分析

library(ggfortify)
library(survival)
autoplot(aareg(Surv(time, status) ~ age + sex + ph.ecog, data = lung, nmin = 1))

时间序列图

autoplot(stats::acf(AirPassengers, plot = FALSE)) # 自相关图

autoplot(stats::pacf(AirPassengers, plot = FALSE)) # 偏自相关图

autoplot(stats::ccf(AirPassengers, AirPassengers, plot = FALSE)) # ccf 图

密度图

autoplot(stats::density(stats::rnorm(1:50)))

autoplot(stats::density(stats::rnorm(1:50)), fill = 'blue')

实现序列预测图

d.arima <- forecast::auto.arima(AirPassengers)
autoplot(forecast::forecast(d.arima, h = 10))

autoplot(forecast::forecast(d.arima, level = c(85), h = 10))

autoplot(forecast::forecast(d.arima, h = 5), conf.int = FALSE, is.date = FALSE)

autoplot(forecast::forecast(stats::HoltWinters(UKgas), h = 10))

autoplot(forecast::forecast(forecast::ets(UKgas), h = 5))

聚类图

autoplot(stats::kmeans(iris[-5], 3), data = iris)

autoplot(cluster::clara(iris[-5], 3), label = TRUE)

autoplot(cluster::fanny(iris[-5], 3))

autoplot(cluster::fanny(iris[-5], 3), frame = TRUE)

autoplot(cluster::pam(iris[-5], 3), data = iris, colour = 'Species')

autoplot(cluster::pam(iris[-5], 3), data = iris, frame = TRUE, frame.type = 't')

热力图

autoplot(matrix(rnorm(20), nc = 5))
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.

autoplot(matrix(rnorm(20), nc = 5), fill = 'red')
## Scale for 'y' is already present. Adding another scale for 'y', which
## will replace the existing scale.

autoplot(matrix(rnorm(20), nc = 2), geom = 'point')

主成分分析可视化

autoplot(stats::prcomp(iris[-5]))

autoplot(stats::prcomp(iris[-5]), data = iris)

autoplot(stats::prcomp(iris[-5]), data = iris, colour = 'Species')

autoplot(stats::prcomp(iris[-5]), data = iris, frame = TRUE,
frame.colour = 'Species')

autoplot(stats::prcomp(iris[-5]), data = iris, frame = TRUE,
frame.type = 't', frame.colour = 'Species')

生存分析可视化

library(survival)
library(ggfortify)
autoplot(survfit(Surv(time, status) ~ sex, data = lung))

autoplot(survfit(Surv(time, status) ~ sex, data = lung), facets = TRUE)    

总结

autoplot 是一大类函数,你可以将多种对象传递进去,然后生成相应图形