
一行R代碼來(lái)實(shí)現(xiàn)繁瑣的可視化
ggfortify 是一個(gè)簡(jiǎn)單易用的R軟件包,它可以?xún)H僅使用一行代碼來(lái)對(duì)許多受歡迎的R軟件包結(jié)果進(jìn)行二維可視化,這讓統(tǒng)計(jì)學(xué)家以及數(shù)據(jù)科學(xué)家省去了許多繁瑣和重復(fù)的過(guò)程,不用對(duì)結(jié)果進(jìn)行任何處理就能以ggplot的風(fēng)格畫(huà)出好看的圖,大大地提高了工作的效率。
ggfortify 已經(jīng)可以在 CRAN 上下載得到,但是由于最近很多的功能都還在快速增加,因此還是推薦大家從 Github 上下載和安裝。
library(devtools) install_github('sinhrks/ggfortify') library(ggfortify)
接下來(lái)我將簡(jiǎn)單介紹一下怎么用ggplot2和ggfortify來(lái)很快地對(duì)PCA、聚類(lèi)以及LFDA的結(jié)果進(jìn)行可視化,然后將簡(jiǎn)單介紹用ggfortify來(lái)對(duì)時(shí)間序列進(jìn)行快速可視化的方法。
PCA (主成分分析)
ggfortify使ggplot2知道怎么詮釋PCA對(duì)象。加載好ggfortify包之后, 你可以對(duì)stats::prcomp和stats::princomp對(duì)象使用ggplot2::autoplot。
library(ggfortify) df <- iris[c(1, 2, 3, 4)] autoplot(prcomp(df))
你還可以選擇數(shù)據(jù)中的一列來(lái)給畫(huà)出的點(diǎn)按類(lèi)別自動(dòng)分顏色。輸入help(autoplot.prcomp)可以了解到更多的其他選擇。
autoplot(prcomp(df), data = iris, colour = 'Species')
比如說(shuō)給定label = TRUE可以給每個(gè)點(diǎn)加上標(biāo)識(shí)(以rownames為標(biāo)準(zhǔn)),也可以調(diào)整標(biāo)識(shí)的大小。
autoplot(prcomp(df), data = iris, colour = 'Species', label = TRUE, label.size = 3)
給定shape = FALSE可以讓所有的點(diǎn)消失,只留下標(biāo)識(shí),這樣可以讓圖更清晰,辨識(shí)度更大。
autoplot(prcomp(df), data = iris, colour = 'Species', shape = FALSE, label.size = 3)
給定loadings = TRUE可以很快地畫(huà)出特征向量。
autoplot(prcomp(df), data = iris, colour = 'Species', loadings = TRUE)
同樣的,你也可以顯示特征向量的標(biāo)識(shí)以及調(diào)整他們的大小,更多選擇請(qǐng)參考幫助文件。
autoplot(prcomp(df), data = iris, colour = 'Species', loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, loadings.label.size = 3)
和PCA類(lèi)似,ggfortify也支持stats::factanal對(duì)象??烧{(diào)的選擇也很廣泛。以下給出了簡(jiǎn)單的例子:
注意當(dāng)你使用factanal來(lái)計(jì)算分?jǐn)?shù)的話,你必須給定scores的值。
d.factanal <- factanal(state.x77, factors = 3, scores = 'regression') autoplot(d.factanal, data = state.x77, colour = 'Income')
autoplot(d.factanal, label = TRUE, label.size = 3, loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3)
K-均值聚類(lèi)
autoplot(kmeans(USArrests, 3), data = USArrests)
autoplot(kmeans(USArrests, 3), data = USArrests, label = TRUE, label.size = 3)
其他聚類(lèi)
ggfortify也支持cluster::clara,cluster::fanny,cluster::pam。
library(cluster) autoplot(clara(iris[-5], 3))
給定frame = TRUE,可以把stats::kmeans和cluster::*中的每個(gè)類(lèi)圈出來(lái)。
autoplot(fanny(iris[-5], 3), frame = TRUE)
你也可以通過(guò)frame.type來(lái)選擇圈的類(lèi)型。更多選擇請(qǐng)參照ggplot2::stat_ellipse里面的frame.type的type關(guān)鍵詞。
autoplot(pam(iris[-5], 3), frame = TRUE, frame.type = 'norm')
更多關(guān)于聚類(lèi)方面的可視化請(qǐng)參考 Github 上的 Vignette 或者 Rpubs 上的例子。
lfda(Fisher局部判別分析)
lfda包支持一系列的 Fisher 局部判別分析方法,包括半監(jiān)督 lfda,非線性 lfda。你也可以使用ggfortify來(lái)對(duì)他們的結(jié)果進(jìn)行可視化。
library(lfda) # Fisher局部判別分析 (LFDA) model <- lfda(iris[-5], iris[, 5], 4, metric="plain") autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
# 非線性核Fisher局部判別分析 (KLFDA) model <- klfda(kmatrixGauss(iris[-5]), iris[, 5], 4, metric="plain") autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
注意對(duì)iris數(shù)據(jù)來(lái)說(shuō),不同的類(lèi)之間的關(guān)系很顯然不是簡(jiǎn)單的線性,這種情況下非線性的klfda 影響可能太強(qiáng)大而影響了可視化的效果,在使用前請(qǐng)充分理解每個(gè)算法的意義以及效果。
# 半監(jiān)督Fisher局部判別分析 (SELF) model <- self(iris[-5], iris[, 5], beta = 0.1, r = 3, metric="plain") autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
時(shí)間序列的可視化
用ggfortify可以使時(shí)間序列的可視化變得極其簡(jiǎn)單。接下來(lái)我將給出一些簡(jiǎn)單的例子。
ts對(duì)象
library(ggfortify) autoplot(AirPassengers)
可以使用ts.colour和ts.linetype來(lái)改變線的顏色和形狀。更多的選擇請(qǐng)參考help(autoplot.ts)。
autoplot(AirPassengers, ts.colour = 'red', ts.linetype = 'dashed')
多變量時(shí)間序列
library(vars) data(Canada) autoplot(Canada)
使用facets = FALSE可以把所有變量畫(huà)在一條軸上。
autoplot(Canada, facets = FALSE)
autoplot也可以理解其他的時(shí)間序列類(lèi)別。可支持的R包有:
zoo::zooreg
xts::xts
tseries::irts
一些例子:
library(xts) autoplot(as.xts(AirPassengers), ts.colour = 'green')
library(timeSeries) autoplot(as.timeSeries(AirPassengers), ts.colour = ('dodgerblue3'))
你也可以通過(guò)ts.geom來(lái)改變幾何形狀,目前支持的有l(wèi)ine,bar和point。
autoplot(AirPassengers, ts.geom = 'bar', fill = 'blue')
autoplot(AirPassengers, ts.geom = 'point', shape = 3)
forecast包
library(forecast) d.arima <- auto.arima(AirPassengers) d.forecast <- forecast(d.arima, level = c(95), h = 50) autoplot(d.forecast)
有很多設(shè)置可供調(diào)整:
autoplot(d.forecast, ts.colour = 'firebrick1', predict.colour = 'red', predict.linetype = 'dashed', conf.int = FALSE)
vars包
library(vars) data(Canada) 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))
autoplot(cpt.meanvar(AirPassengers), cpt.colour = 'blue', cpt.linetype = 'solid')
strucchange包
library(strucchange) autoplot(breakpoints(Nile ~ 1), ts.colour = 'blue', ts.linetype = 'dashed', cpt.colour = 'dodgerblue3', cpt.linetype = 'solid')
dlm包
library(dlm) form <- function(theta){ dlmModPoly(order = 1, dV = exp(theta[1]), dW = exp(theta[2])) } model <- form(dlmMLE(Nile, parm = c(1, 1), form)$par) filtered <- dlmFilter(Nile, model) autoplot(filtered)
autoplot(filtered, ts.linetype = 'dashed', fitted.colour = 'blue')
smoothed <- dlmSmooth(filtered) autoplot(smoothed)
p <- autoplot(filtered) autoplot(smoothed, ts.colour = 'blue', p = p)
KFAS包
library(KFAS) model <- SSModel( Nile ~ SSMtrend(degree=1, Q=matrix(NA)), H=matrix(NA) ) fit <- fitSSM(model=model, inits=c(log(var(Nile)),log(var(Nile))), method="BFGS") smoothed <- KFS(fit$model) autoplot(smoothed)
使用smoothing='none'可以畫(huà)出過(guò)濾后的結(jié)果。
filtered <- KFS(fit$model, filtering="mean", smoothing='none') autoplot(filtered)
trend <- signal(smoothed, states="trend") p <- autoplot(filtered) autoplot(trend, ts.colour = 'blue', p = p)
stats包
可支持的stats包里的對(duì)象有:
stl,decomposed.ts
acf,pacf,ccf
spec.ar,spec.pgram
cpgramautoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = 'blue')
autoplot(acf(AirPassengers, plot = FALSE))
autoplot(acf(AirPassengers, plot = FALSE), conf.int.fill = '#0000FF', conf.int.value = 0.8, conf.int.type = 'ma')
autoplot(spec.ar(AirPassengers, plot = FALSE))
ggcpgram(arima.sim(list(ar = c(0.7, -0.5)), n = 50))
library(forecast) ggtsdiag(auto.arima(AirPassengers))
gglagplot(AirPassengers, lags = 4)
數(shù)據(jù)分析咨詢(xún)請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
SQL Server 中 CONVERT 函數(shù)的日期轉(zhuǎn)換:從基礎(chǔ)用法到實(shí)戰(zhàn)優(yōu)化 在 SQL Server 的數(shù)據(jù)處理中,日期格式轉(zhuǎn)換是高頻需求 —— 無(wú)論 ...
2025-09-18MySQL 大表拆分與關(guān)聯(lián)查詢(xún)效率:打破 “拆分必慢” 的認(rèn)知誤區(qū) 在 MySQL 數(shù)據(jù)庫(kù)管理中,“大表” 始終是性能優(yōu)化繞不開(kāi)的話題。 ...
2025-09-18CDA 數(shù)據(jù)分析師:表結(jié)構(gòu)數(shù)據(jù) “獲取 - 加工 - 使用” 全流程的賦能者 表結(jié)構(gòu)數(shù)據(jù)(如數(shù)據(jù)庫(kù)表、Excel 表、CSV 文件)是企業(yè)數(shù)字 ...
2025-09-18DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動(dòng)態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開(kāi)始提取前,需先判斷 TIF 文件的類(lèi)型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價(jià)值的專(zhuān)業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲(chǔ)的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫(kù)表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實(shí)戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫(kù))處理 Excel 數(shù)據(jù)時(shí),“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗(yàn)與 t 檢驗(yàn):差異、適用場(chǎng)景與實(shí)踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)領(lǐng)域,假設(shè)檢驗(yàn)是驗(yàn)證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專(zhuān)業(yè)操盤(pán)手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲(chǔ)的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計(jì)劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計(jì)劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對(duì)象的 text 與 content:區(qū)別、場(chǎng)景與實(shí)踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請(qǐng)求開(kāi)發(fā)時(shí)(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價(jià)值的核心操盤(pán)手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫(kù)表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請(qǐng)求工具對(duì)比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請(qǐng)求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長(zhǎng)浮點(diǎn)數(shù)據(jù)的科學(xué)計(jì)數(shù)法問(wèn)題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長(zhǎng)浮點(diǎn)數(shù)據(jù)時(shí)的科學(xué)計(jì)數(shù)法問(wèn)題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價(jià)值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營(yíng)問(wèn)題、提升執(zhí)行效率的核心手段,其價(jià)值 ...
2025-09-12用 SQL 驗(yàn)證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實(shí)戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過(guò)程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計(jì)” 與 “用戶(hù)體驗(yàn) ...
2025-09-11塔吉特百貨孕婦營(yíng)銷(xiāo)案例:數(shù)據(jù)驅(qū)動(dòng)下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見(jiàn)頂” 的當(dāng)下,精準(zhǔn)營(yíng)銷(xiāo)成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價(jià)值 在數(shù)據(jù)驅(qū)動(dòng)決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類(lèi)分析:從操作實(shí)踐到業(yè)務(wù)價(jià)值挖掘 在數(shù)據(jù)分析場(chǎng)景中,聚類(lèi)分析作為 “無(wú)監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計(jì)模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價(jià)值導(dǎo)向 統(tǒng)計(jì)模型作為數(shù)據(jù)分析的核心工具,并非簡(jiǎn)單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10