R可視化——基于GGally包繪制平行坐標(biāo)圖

???? \color{red}{平行坐標(biāo)圖}(Parallel Coordinates chart)是一種高維數(shù)據(jù)的可視化方法,常用于一些分類問(wèn)題及回歸問(wèn)題中,主要用于比較某個(gè)數(shù)值在不同分組之間的變化差異或隨時(shí)間的變化趨勢(shì)等。
???? \color{red}{GGally包}是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
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容