查看原文
其他

R可视化——基于GGally包绘制平行坐标图

王志山 科研后花园 2023-09-08

平行坐标图(Parallel Coordinates chart)是一种高维数据的可视化方法,常用于一些分类问题及回归问题中,主要用于比较某个数值在不同分组之间的变化差异或随时间的变化趋势等。

GGally包是ggplot2的拓展包之一,我们今天主要基于此包进行平行坐标图的绘制。

加载相关R包

rm(list = ls())
#加载R包library(GGally)library(ggthemes)library(ggprism)library(scagnostics)

绘图参数

#可选参数??ggparcoord#查看该函数下的参数
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, #数据行数 scale="globalminmax",#No scaling groupColumn = "Species",#按照分组显示不同颜色 order = "anyClass",#水平坐标轴排序,可选参数有'skewness', 'allClass', 'anyClass', 'Outlying', 'Skewed', 'Clumpy', 'Sparse', 'Striated', 'Convex', 'Skinny', 'Stringy', 'Monotonic' showPoints = T,#是否显示点 title = "Parallel Coordinates chart",#标题 alphaLines = 0.5) + #线的粗细 theme_pander()+#模板主题设置 theme(plot.title = element_text(size=10))+#标题大小设置 scale_color_prism(palette = "candy_bright")#使用ggrism包的主题颜色p1
p2<-ggparcoord(df, columns = 1:4, #数据行数 groupColumn = "Species",#按照分组显示不同颜色 order = "Outlying",#水平坐标轴排序 showPoints = T,#是否显示点 title = "Parallel Coordinates chart",#标题 scale="uniminmax",#Standardize to Min = 0 and Max = 1 alphaLines = 0.5) + #线的粗细 theme_pander()+#模板主题设置 theme(plot.title = element_text(size=10))+#标题大小设置 scale_color_prism(palette = "neon")#使用ggrism包的主题颜色p2
p3<-ggparcoord(df, columns = 1:4, #数据行数 groupColumn = "Species",#按照分组显示不同颜色 order = "Clumpy",#水平坐标轴排序 showPoints = T,#是否显示点 title = "Parallel Coordinates chart",#标题 scale="std",#Normalize univariately (substract mean & divide by sd) alphaLines = 0.5) + #线的粗细 theme_pander()+#模板主题设置 theme(plot.title = element_text(size=10))+#标题大小设置 scale_color_prism(palette = "autumn_leaves")#使用ggrism包的主题颜色p3
p4<-ggparcoord(df, columns = 1:4, #数据行数 groupColumn = "Species",#按照分组显示不同颜色 order = "Sparse",#水平坐标轴排序 showPoints = T,#是否显示点 title = "Parallel Coordinates chart",#标题 scale="center",#Standardize and center variables alphaLines = 0.5) + #线的粗细 theme_pander()+#模板主题设置 theme(plot.title = element_text(size=10))+#标题大小设置 scale_color_prism(palette = "sunny_garden")#使用ggrism包的主题颜色p4
#拼图cowplot::plot_grid(p1,p2,p3,p4,ncol=2)


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")


3、分面显示

p1+facet_wrap(~Species)p2+facet_wrap(~Species)p3+facet_wrap(~Species)p4+facet_wrap(~Species)


我就知道你“在看”

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存