R语言 缩放变量平行坐标图

R语言 缩放变量平行坐标图

为了分析和可视化高维数据,我们可以使用平行坐标图。绘制一个由n条平行线组成的背景,通常是垂直和均匀间隔的,以显示n维空间中的一组点。n维空间中的一个点由一条顶点位于平行轴上的折线表示;该点的第 i个坐标与顶点在第i个轴上 的位置相对应。

R编程语言中的缩放变量平行坐标图

这种表示方法类似于时间序列的可视化,只是它用于没有自然顺序的数据,因为轴与时间点没有关联。因此,几个轴的布局可能会引起人们的兴趣。

使用的模块

  • GGally: 它对ggplot2进行了扩展,增加了几个函数,以降低将几何图形与转换后的数据相结合的复杂性。它可以通过以下命令安装。
install.packages("GGally")
  • hrbrthemes: 它是一个额外的’ggplot2’主题的汇编,用于轴和绘图。
install.packages("hrbrthemes")

为了绘制平行坐标图,我们将使用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 = “” )

参数

  • data: 数据集
  • columns: 变量向量(名称或指数),作为图中的坐标轴
  • groupColumn: 按单个变量分组(颜色)。
  • scale: 用于缩放变量的方法(详见)。
  • scaleSummary: 如果scale==”center”,用于对每个变量进行单变量中心化的汇总统计。
  • centerObsID: 如果规模==”centerObs”,案例图的行号,应以其为中心进行单变量统计。
  • missing: 用于处理缺失值的方法(见细节)。
  • order: 用于对坐标轴进行排序的方法(详见)。
  • showPoints: 逻辑运算符,表示是否应该绘制点。

例子1:不使用比例尺

这里我们将看到不使用缩放变量。为此,我们将不使用 缩放 属性。

# Libraries
library(GGally)
library(viridis)    # provide the color palette
library(hrbrthemes) # provides themes for axis and plot
 
# default data in R
data <- iris
 
# glimpse of the data
head(data)
 
# plotting the Parallel Coordinates
ggparcoord(data, # data
           columns = 1:3, # plotting first 3 columns
           alphaLines = .4, # transparency of the color
           groupColumn = 5, order = "anyClass",
           showPoints = TRUE) +
  theme(
    plot.title = element_text(size=10)
  )

输出

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

缩放变量 R中的平行坐标图

例2:使用MinMax缩放

这里我们将使用mixmax缩放变量, scale = “globalminmax “。

# Libraries
library(GGally)
library(viridis)    # provide the color palette
library(hrbrthemes) # provides themes for axis and plot
 
# default data in R
data <- iris
 
# glimpse of the data
head(data)
 
# plotting the Parallel Coordinates
ggparcoord(data, # data
           columns = 1:3, # plotting first 3 columns
           alphaLines = .4, # transparency of the color
           groupColumn = 5, order = "anyClass",
           scale = "globalminmax",
           showPoints = TRUE) +
  theme(
    plot.title = element_text(size=10)
  )

输出

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

缩放变量 R中的平行坐标图

例子3:用标准化进行缩放

在这里,我们将使用 scale=”std “的标准化缩放变量。

# Libraries
library(GGally)
library(viridis)    # provide the color palette
library(hrbrthemes) # provides themes for axis and plot
 
# default data in R
data <- iris
 
# glimpse of the data
head(data)
 
# plotting the Parallel Coordinates
ggparcoord(data, # data
           columns = 1:3, # plotting first 3 columns
           alphaLines = .4, # transparency of the color
           groupColumn = 5, order = "anyClass",
           scale = "std",
           showPoints = TRUE) +
  theme(
    plot.title = element_text(size=10)
  )

输出

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

缩放变量 R中的平行坐标图

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程