Parallel coordinates in R

Package

MASS

Author

Brian Ripley

Sample data set

Consider the well-known iris data set and divide it between the numerical and the categorical variable.

df <- iris[, 1:4]
groups <- iris[, 5]

Parallel coordinate plot with parcoord

Basic parallel coordiates plot

The parcoord function from MASS package can be used to create a parallel coordinates plot in base R based on the matplot function. You can input a matrix or a data frame.

# install.packages("MASS")
library(MASS)

parcoord(df)

Paralell coordinates plot in R with parcoord

Minimum and maximum values

If you want to show the minimum and maximum values for each axis variable set var.label = TRUE.

# install.packages("MASS")
library(MASS)

parcoord(df, var.label = TRUE)

parcoord function in R

Lines customization

parcoord function colors

Color by group

The col argument can be used to change the color of the lines. You can pass a single color to change the default black color or specify a color for each row (line) of the data frame. In the following example we are transforming the categorical variable into numeric, so each group will have a different color.

# install.packages("MASS")
library(MASS)

# Categorical variable as colors
cols <- as.numeric(groups)

parcoord(df, col = cols)

Paralell coordinates plot color lines by group

Custom colors

If you prefer setting custom colors you can create a vector with as many colors as groups as shown in the example below.

# install.packages("MASS")
library(MASS)

# Colors
cols <- c("#3399FF", "#FFC44C", "#FF661A")
cols <- cols[factor(groups)]

parcoord(df, col = cols)

Highlight lines paralell coordinates plot in R

Highlighting a group

Note that the previous approach can be used to highlight specific groups. For instance, in the following example we are highlighting one of the groups with blue lines against gray lines.

# install.packages("MASS")
library(MASS)

# Colors
cols <- c("lightgray", "lightgray", 4)
cols <- cols[factor(groups)]

parcoord(df, col = cols)
ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon
Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also