Open In App

Parallel chart with the MASS library in R

Last Updated : 22 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

To analyze and visualize high-dimensional data, one can use Parallel Coordinates. A background is drawn consisting of n parallel lines, often vertical and evenly spaced, to display a set of points in an n-dimensional space. A point in n-dimensional space is represented by a polyline with vertices on parallel axes; the ith coordinate of the point corresponds to the position of the vertex on the ith axis.

Parallel chart with the MASS library in R Programming Language

This representation is similar to time series visualization, except that it is used with data that does not have a natural order because the axes do not correlate to points in time. As a result, several axis layouts may be of interest.

Parallel Coordinates with MASS Library

The parcoord() function in the MASS package creates a parallel coordinates chart automatically. A data frame with solely numeric variables can be used as the input dataset. Each variable will be utilized to construct one of the chart’s vertical axes.

R




# Libraries
library(MASS)
 
# default data in R
data <- iris
 
head(data)
 
# plotting the graphs
parcoord(iris[, c(1:4)] , # choosing first 4 parameters
          
         # selecting the color palette based on the plot
         col = colors()[as.numeric(iris$Species)*8]
         )


Output:

  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

Customizing the Color Palette

Basically, there are not any built-in methods or attributes in this package for color customization. We will use colorRampPalette() methods color range between two colors specified points

R




# Libraries
library(MASS)
 
# choosing the graph color
library(RColorBrewer)
 
# default data in R
data <- iris
 
head(data)
 
# define a color palette
palette <- brewer.pal(5, "Set1")
 
# plotting the graphs
parcoord(iris[, c(1:4)] , # choosing first 4 parameters
          
         # selecting the color palette based on the plot
         col = palette[as.numeric(iris$Species)]
         )


Output:

  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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads