???? (Parallel Coordinates chart)是一種高維數(shù)據(jù)的可視化方法,常用于一些分類問(wèn)題及回歸問(wèn)題中,主要用于比較某個(gè)數(shù)值在不同分組之間的變化差異或隨時(shí)間的變化趨勢(shì)等。
???? 是ggplot2的拓展包之一,我們今天主要基于此包進(jìn)行平行坐標(biāo)圖的繪制。
加載相關(guān)R包
rm(list = ls())
#加載R包
library(GGally)
library(ggthemes)
library(ggprism)
library(scagnostics)
繪圖參數(shù)
#可選參數(shù)
??ggparcoord#查看該函數(shù)下的參數(shù)
ggparcoord(data,
columns = 1:ncol(data),
groupColumn = NULL,
scale = "std",
scaleSummary = "mean",
centerObsID = 1,
missing = "exclude",
order = columns,
showPoints = FALSE,
splineFactor = FALSE,
alphaLines = 1,
boxplot = FALSE,
shadeBox = NULL,
mapping = NULL,
title = "")
繪圖
1、基本繪圖
p1<-ggparcoord(df,
columns = 1:4, #數(shù)據(jù)行數(shù)
scale="globalminmax",#No scaling
groupColumn = "Species",#按照分組顯示不同顏色
order = "anyClass",#水平坐標(biāo)軸排序,可選參數(shù)有'skewness', 'allClass', 'anyClass', 'Outlying', 'Skewed', 'Clumpy', 'Sparse', 'Striated', 'Convex', 'Skinny', 'Stringy', 'Monotonic'
showPoints = T,#是否顯示點(diǎn)
title = "Parallel Coordinates chart",#標(biāo)題
alphaLines = 0.5) + #線的粗細(xì)
theme_pander()+#模板主題設(shè)置
theme(plot.title = element_text(size=10))+#標(biāo)題大小設(shè)置
scale_color_prism(palette = "candy_bright")#使用ggrism包的主題顏色
p1
p2<-ggparcoord(df,
columns = 1:4, #數(shù)據(jù)行數(shù)
groupColumn = "Species",#按照分組顯示不同顏色
order = "Outlying",#水平坐標(biāo)軸排序
showPoints = T,#是否顯示點(diǎn)
title = "Parallel Coordinates chart",#標(biāo)題
scale="uniminmax",#Standardize to Min = 0 and Max = 1
alphaLines = 0.5) + #線的粗細(xì)
theme_pander()+#模板主題設(shè)置
theme(plot.title = element_text(size=10))+#標(biāo)題大小設(shè)置
scale_color_prism(palette = "neon")#使用ggrism包的主題顏色
p2
p3<-ggparcoord(df,
columns = 1:4, #數(shù)據(jù)行數(shù)
groupColumn = "Species",#按照分組顯示不同顏色
order = "Clumpy",#水平坐標(biāo)軸排序
showPoints = T,#是否顯示點(diǎn)
title = "Parallel Coordinates chart",#標(biāo)題
scale="std",#Normalize univariately (substract mean & divide by sd)
alphaLines = 0.5) + #線的粗細(xì)
theme_pander()+#模板主題設(shè)置
theme(plot.title = element_text(size=10))+#標(biāo)題大小設(shè)置
scale_color_prism(palette = "autumn_leaves")#使用ggrism包的主題顏色
p3
p4<-ggparcoord(df,
columns = 1:4, #數(shù)據(jù)行數(shù)
groupColumn = "Species",#按照分組顯示不同顏色
order = "Sparse",#水平坐標(biāo)軸排序
showPoints = T,#是否顯示點(diǎn)
title = "Parallel Coordinates chart",#標(biāo)題
scale="center",#Standardize and center variables
alphaLines = 0.5) + #線的粗細(xì)
theme_pander()+#模板主題設(shè)置
theme(plot.title = element_text(size=10))+#標(biāo)題大小設(shè)置
scale_color_prism(palette = "sunny_garden")#使用ggrism包的主題顏色
p4
#拼圖
cowplot::plot_grid(p1,p2,p3,p4,ncol=2)

image.png
2、顯示箱線圖
ggparcoord(df,
columns = 1:4,
scale="globalminmax",
groupColumn = "Species",
showPoints = T,
title = "Parallel Coordinates chart",
alphaLines = 0.5,
boxplot = T) +
theme_map()+
theme(plot.title = element_text(size=10))+
scale_color_prism(palette = "candy_bright")

image.png
3、分面顯示
p1+facet_wrap(~Species)#使用facet_wrap()函數(shù)實(shí)現(xiàn)圖形分面
p2+facet_wrap(~Species)
p3+facet_wrap(~Species)
p4+facet_wrap(~Species)

image.png

image.png

image.png

image.png