ggfortify包学习
library(ggfortify)
library(tidyverse)
#PCA (主成分分析)
df <- iris[c(1, 2, 3, 4)]
prcomp(df) %>%
#autoplot()
#选择数据中的一列来给画出的点按类别自动分颜色
#autoplot(data = iris, colour = 'Species')
#给定label = TRUE 可以给每个点加上标识(以rownames为标准),也可以调整标识的大小
#autoplot( data = iris, colour = 'Species', label = TRUE, label.size = 3)
#给定 shape = FALSE 可以让所有的点消失,只留下标识,这样可以让图更清晰,辨识度更大
# autoplot(data = iris, colour = 'Species', shape = FALSE, label.size = 3)
# 给定 loadings = TRUE 可以很快的画出特征向量。
# autoplot(data = iris, colour = 'Species', loadings = TRUE)
# 可以显示特征向量的标识以及调整他们的大小
autoplot(data = iris, colour = 'Species',
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3)
# 因素分析
factanal(state.x77, factors = 3, scores = 'regression') %>%
#autoplot(data = state.x77, colour = 'Income')
autoplot(label = TRUE, label.size = 3,
loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3)
#K-均值
kmeans(USArrests, 3) %>%
# autoplot(data = USArrests)
autoplot(data = USArrests, label = TRUE, label.size = 3)
#cluster(集群)
library(cluster)
clara(iris[-5],3) %>%
# autoplot()
#给定 frame = TRUE,可以把stats::kmeans 和 cluster::* 的每个集群圈出来。
# autoplot(frame=3)
# 可以通过 frame.type 来选择圈的类型
autoplot(frame = TRUE, frame.type = 'norm')
# lfda(Fisher局部判别分析)
library(lfda)
# Fisher局部判别分析 (LFDA)
lfda(iris[-5], iris[, 5], 4, metric="plain") %>%
# autoplot(data = iris, frame = TRUE, frame.colour = 'Species')
# 半监督Fisher局部判别分析 (SELF)
autoplot(data = iris, frame = TRUE, frame.colour = 'Species')
#时间序列的可视化
AirPassengers %>%
#autoplot()
# 使用 ts.colour 和 ts.linetype来改变线的颜色和形状
autoplot(ts.colour = 'red', ts.linetype = 'dashed')
#多变量时间序列
library(vars)
Canada %>%
# autoplot()
# facets = FALSE 可以把所有变量画在一条轴上
autoplot(facets = FALSE)
autoplot(AirPassengers, ts.colour = ('dodgerblue3'))
# 通过 ts.geom 来改变几何形状,目前支持的有 line, bar and point
autoplot(AirPassengers, ts.geom = 'bar', fill = 'blue')
autoplot(AirPassengers, ts.geom = 'point', shape = 3)
# forecast包
library(forecast)
d.arima <- auto.arima(AirPassengers)
forecast(d.arima, level = c(95), h = 50) %>%
autoplot()
# vars包
library(vars)
d.vselect <- VARselect(Canada, lag.max = 5, type = 'const')$selection[1]
d.var <- VAR(Canada, p = d.vselect, type = 'const')
autoplot(predict(d.var, n.ahead = 50), ts.colour = 'dodgerblue4',
predict.colour = 'blue', predict.linetype = 'dashed')
# changepoint包
library(changepoint)
autoplot(cpt.meanvar(AirPassengers))
library(strucchange)
autoplot(breakpoints(Nile ~ 1))
# https://terrytangyuan.github.io/2015/11/24/ggfortify-intro/
# Draw subplots
res <- lm(Volume ~ Girth, data = trees)
autoplot(res, ncol = 4)+
theme_classic()
autoplot(res, which = 1:6, ncol = 2, label.size = 3)
m <- lm(Petal.Width ~ Petal.Length, data = iris)
# Change the color by groups (species)
autoplot(m, which = 1:6, label.size = 3, data = iris,
colour = 'Species')
# Compute a generalized linear model
m <- glm(Murder ~ Assault + UrbanPop + Rape,
family = gaussian, data = USArrests)
# Create the plot
# Change the theme and colour
autoplot(m, which = 1:6, ncol = 2, label.size = 3,
colour = "steelblue") + theme_bw()
# Draw multiple instances
library(purrr)
res <- purrr::map(c(3, 4, 5), ~ kmeans(iris[-5], .))
autoplot(res, data = iris[-5], ncol = 3)
library(survival)
res <- list(a = survfit(Surv(time, status) ~ 1, data = lung),
b = survfit(Surv(time, status) ~ sex, data = lung))
autoplot(res)
# Plotting matrix
df <- mtcars[, c("mpg", "disp", "hp", "drat", "wt")] %>%
as.matrix()
# Heatmap
autoplot(scale(df))
df2 <- df[, c("wt", "mpg")]
colnames(df2) <- c("V1", "V2")
# Scatter plot
autoplot(df2, geom = 'point') +
labs(x = "mpg", y = "wt")
library(tidyverse)
#PCA (主成分分析)
df <- iris[c(1, 2, 3, 4)]
prcomp(df) %>%
#autoplot()
#选择数据中的一列来给画出的点按类别自动分颜色
#autoplot(data = iris, colour = 'Species')
#给定label = TRUE 可以给每个点加上标识(以rownames为标准),也可以调整标识的大小
#autoplot( data = iris, colour = 'Species', label = TRUE, label.size = 3)
#给定 shape = FALSE 可以让所有的点消失,只留下标识,这样可以让图更清晰,辨识度更大
# autoplot(data = iris, colour = 'Species', shape = FALSE, label.size = 3)
# 给定 loadings = TRUE 可以很快的画出特征向量。
# autoplot(data = iris, colour = 'Species', loadings = TRUE)
# 可以显示特征向量的标识以及调整他们的大小
autoplot(data = iris, colour = 'Species',
loadings = TRUE, loadings.colour = 'blue',
loadings.label = TRUE, loadings.label.size = 3)
# 因素分析
factanal(state.x77, factors = 3, scores = 'regression') %>%
#autoplot(data = state.x77, colour = 'Income')
autoplot(label = TRUE, label.size = 3,
loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3)
#K-均值
kmeans(USArrests, 3) %>%
# autoplot(data = USArrests)
autoplot(data = USArrests, label = TRUE, label.size = 3)
#cluster(集群)
library(cluster)
clara(iris[-5],3) %>%
# autoplot()
#给定 frame = TRUE,可以把stats::kmeans 和 cluster::* 的每个集群圈出来。
# autoplot(frame=3)
# 可以通过 frame.type 来选择圈的类型
autoplot(frame = TRUE, frame.type = 'norm')
# lfda(Fisher局部判别分析)
library(lfda)
# Fisher局部判别分析 (LFDA)
lfda(iris[-5], iris[, 5], 4, metric="plain") %>%
# autoplot(data = iris, frame = TRUE, frame.colour = 'Species')
# 半监督Fisher局部判别分析 (SELF)
autoplot(data = iris, frame = TRUE, frame.colour = 'Species')
#时间序列的可视化
AirPassengers %>%
#autoplot()
# 使用 ts.colour 和 ts.linetype来改变线的颜色和形状
autoplot(ts.colour = 'red', ts.linetype = 'dashed')
#多变量时间序列
library(vars)
Canada %>%
# autoplot()
# facets = FALSE 可以把所有变量画在一条轴上
autoplot(facets = FALSE)
autoplot(AirPassengers, ts.colour = ('dodgerblue3'))
# 通过 ts.geom 来改变几何形状,目前支持的有 line, bar and point
autoplot(AirPassengers, ts.geom = 'bar', fill = 'blue')
autoplot(AirPassengers, ts.geom = 'point', shape = 3)
# forecast包
library(forecast)
d.arima <- auto.arima(AirPassengers)
forecast(d.arima, level = c(95), h = 50) %>%
autoplot()
# vars包
library(vars)
d.vselect <- VARselect(Canada, lag.max = 5, type = 'const')$selection[1]
d.var <- VAR(Canada, p = d.vselect, type = 'const')
autoplot(predict(d.var, n.ahead = 50), ts.colour = 'dodgerblue4',
predict.colour = 'blue', predict.linetype = 'dashed')
# changepoint包
library(changepoint)
autoplot(cpt.meanvar(AirPassengers))
library(strucchange)
autoplot(breakpoints(Nile ~ 1))
# https://terrytangyuan.github.io/2015/11/24/ggfortify-intro/
# Draw subplots
res <- lm(Volume ~ Girth, data = trees)
autoplot(res, ncol = 4)+
theme_classic()
autoplot(res, which = 1:6, ncol = 2, label.size = 3)
m <- lm(Petal.Width ~ Petal.Length, data = iris)
# Change the color by groups (species)
autoplot(m, which = 1:6, label.size = 3, data = iris,
colour = 'Species')
# Compute a generalized linear model
m <- glm(Murder ~ Assault + UrbanPop + Rape,
family = gaussian, data = USArrests)
# Create the plot
# Change the theme and colour
autoplot(m, which = 1:6, ncol = 2, label.size = 3,
colour = "steelblue") + theme_bw()
# Draw multiple instances
library(purrr)
res <- purrr::map(c(3, 4, 5), ~ kmeans(iris[-5], .))
autoplot(res, data = iris[-5], ncol = 3)
library(survival)
res <- list(a = survfit(Surv(time, status) ~ 1, data = lung),
b = survfit(Surv(time, status) ~ sex, data = lung))
autoplot(res)
# Plotting matrix
df <- mtcars[, c("mpg", "disp", "hp", "drat", "wt")] %>%
as.matrix()
# Heatmap
autoplot(scale(df))
df2 <- df[, c("wt", "mpg")]
colnames(df2) <- c("V1", "V2")
# Scatter plot
autoplot(df2, geom = 'point') +
labs(x = "mpg", y = "wt")
评论
发表评论