Generalized Pairs Plot in R. Visualize Your Data with Correlation... | by Ahmed Yahya Khaled | Towards Data Science

Your innovative idea may come from a detail exploration of your data. Most of the cases that data will contain both continuous and categorical variables. You will need to find explainable pattern from this. What if it can be displayed methodologically in a single diagram and you can do it with simple and short lines of coding?
R is an open-sourced programming language for statistical computing and graphics supported by the R Foundation for Statistical Computing. It is easy to learn and comfortable to work with its widely used integrated development environment- RStudio.
First of all we need to install required packages with the below codes.
install.packages("tidyverse")install.packages("GGally")install.packages("ISLR")
The ‘tidyverse’ package is for data wrangling & data visualization. The widely used ‘ggplot2’ package is encapsulated in the ‘tidyverse’. ‘GGally’ reduces the complexity of combining geometric objects with transformed data by adding several functions to ‘ggplot2’. We will use Mid-Atlantic Wage Data for this exercise which contains wage data for a group of 3000 male workers in the Mid-Atlantic region of US. ‘ISLR’ package is required to load this dataset.
The packages need to be installed once but every time you run your script you need to load the packages with the library function.
library(tidyverse)library(GGally)library(ISLR)
We’ll load the dataset with the data function and explore the dataset with the glimpse and head functions.
data(Wage)glimpse(Wage)head(Wage)
Let’s select only “age”, “education”, “jobclass” & “wage” columns for our work.
Wage2 <- Wage %>%   subset(select = c("age", "education", "jobclass", "wage"))glimpse(Wage2)head(Wage2)
We can see “age” & “wage” variables(columns) are continuous(<int>, <dbl>)and “education” & “jobclass” variables(columns) are categorical(<fact>).
Our objective of this exercise is to see how “wage” varies with respect to “age”, “education” and “jobclass”. Let’s try different plotting options with ggplot2 package’s ggplot & respective geom functions.
Scatter Plot
# P1 ggplot(Wage2) +  geom_point(aes(x = age, y = wage,                  color = education), size = 4) +  labs(x = 'AGE', y = "WAGE") +  ggtitle("wage ~ age, education ") +  theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))# P2ggplot(Wage2) +  geom_point(aes(x = age, y = wage,                  color = jobclass), size = 4) +  labs(x = 'AGE', y = "WAGE") +  ggtitle("wage ~ age, jobclass ") +  theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))
Histogram
# P3ggplot(Wage2) +  geom_histogram(aes(x = wage,                  fill = education), color = 'lightblue') +  labs(x = 'WAGE') +  ggtitle("wage ~ education ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))# P4ggplot(Wage2) +  geom_histogram(aes(x = wage,                      fill = jobclass), color = 'lightblue') +  labs(x = 'WAGE') +  ggtitle("wage ~ jobclass ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))
Density Plot
# P5ggplot(Wage2) +  geom_density(aes(x = wage,                      fill = education)) +  labs(x = 'WAGE') +  ggtitle("wage ~ education ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))# P6ggplot(Wage2) +  geom_density(aes(x = wage,                    fill = jobclass)) +  labs(x = 'WAGE') +  ggtitle("wage ~ jobclass ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))
Bar Plot
# P7ggplot(Wage2) +  geom_bar(aes(x = education,                    fill = jobclass)) +  labs(x = 'WAGE') +  ggtitle("wage ~ education ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))# P8ggplot(Wage2) +  geom_bar(aes(x = jobclass,                fill = education)) +  labs(x = 'WAGE') +  ggtitle("wage ~ jobclass ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))
Box Plot
# P9ggplot(Wage2) +  geom_boxplot(aes(x = education, y = wage,               fill = education)) +  labs(x = 'EDUCATION') +  ggtitle("wage ~ education ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))# P10ggplot(Wage2) +  geom_boxplot(aes(x = jobclass, y = wage,               fill = jobclass)) +  labs(x = 'JOBCLASS') +  ggtitle("wage ~ jobclass ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))# P11ggplot(Wage2) +  geom_boxplot(aes(x = jobclass, y = wage,                   fill = education)) +  labs(x = 'JOBCLASS') +  ggtitle("wage ~ jobclass, education ") +    theme_bw() +  theme(axis.text.x = element_text(face = 'bold', size = 10),        axis.text.y = element_text(face = 'bold', size = 10))
Correlation Matrix Plot with “ggpairs” of “GGally”
So far we have checked different plotting options- Scatter plot, Histogram, Density plot, Bar plot & Box plot to find relative distributions. Now its time to see the Generalized Pairs Plot in R.
We have already loaded the “GGally” package. The function ggpairs will do the magic and bring all these plots in a single page !
# P12ggpairs(Wage2) + theme_bw()
Color featuring by “education”
# P13ggpairs(Wage2, aes(color = education)) + theme_bw()
Color featuring by “jobclass”
# P14ggpairs(Wage2, aes(color = jobclass)) + theme_bw()
Hope you find this hands-on article useful. I would recommend you to apply these easy-coding plots in your own-field data and draw meaningful and interesting insights for your business innovation.
Best wishes for your data journey...