Data Visualization Using Ggplot2 and Its Extensions - 5 July 2021 - Final
Data Visualization Using Ggplot2 and Its Extensions - 5 July 2021 - Final
extensions
1 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Find us at...
@rladiestunis
@rladiestunis
Kaouthar Driss
Mouna Belaid
2 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Outline
1) Data presentation using the peopleanalyticsdata package
2) One-variable visualization
Boxplot Viz
Barplot Viz
3) Two-variables visualization
3 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Outline
4) Correlation Matrix for three or more variable Viz
Augmenting one- and two-variable visualization with color, and other aesthetic mappings
4 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
How to follow this workshop
Workshop materials are available at the following link
Prerequisites:
Basic proficiency in R
Some basic knowledge about different R data types & structures (character, numeric, data frame, etc.)
Possibility to communicate with Teacher Assistants for relevant questions about our training session
5 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Learning goals 🚀
Get a clear understanding of the ggplot2 R package and some of its extensions: ggprism, ggstatsplot, gganimate, GGally and
patchwork
Design effective data visualization by choosing the appropriate ggplot2 geoms when selecting the data
6 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
What is the grammar of graphics?
Google defines a grammar as “the whole system and structure of a language or of languages in general, usually taken as
consisting of syntax and morphology (including inflections) and sometimes also phonology and semantics.”1
Applied to visualizations, a grammar of graphics is a grammar used to describe and create a wide range of statistical graphics.3
The layered grammar of graphics approach is implemented in ggplot2, a widely used graphics library for R. All graphics in
this library are built using a layered approach, building layers up to create the final graphic.
The ggplot2 is an abbreviation of grammar of graphics plot. The ggplot2 is created by Hadley Wickham.
Hadley Wickham wrote a very interesting book ggplot2: elegant graphics for data analysis that shows you how to use ggplot2 to
create graphics that help you understand your data.
[1] Google
[2] Wickham, Hadley. (2010) “A Layered Grammar of Graphics”. Journal of Computational and Graphical Statistics, 19(1).
[3] Wilkinson, Leland. (2005). The Grammar of Graphics. (UChicago authentication required)
7 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
The components of graphics using ggplot2
- Data: is what we want to visualize. Data should be in a data.frame format to be used in ggplot2.
- Coordinate system (coord): A coord describes how data coordinates are mapped to the plane of the graphics. We normally use
Cartesian coordinates (default), but a number of others are available, including polar coordinates and map projections.
- Geoms: The geoms are the geometric objects that are drawn to represent the data such as points, lines, areas, polygons, etc.
- Aesthetics: Aesthetics are visual properties of geoms such as x and y positions, colors, shapes, transparency, etc.
- Scales: Scales map values in the data space to values in the aesthetic space whether it is color, size, or shape.
- Statistical transformation (stats): The stats summarize data in many useful ways. Examples binning and counting to create a
histogram and regression line for regression analysis.
8 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Prior installation
install.packages(c('peopleanalyticsdata',
'ggplot2',
'summarytools',
'dplyr',
'ggprism',
'GGally',
'magick',
'patchwork',
'ggstatsplot',
'gganimate'))
9 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Overview of the peopleanalyticsdata R package
Data sets for statistical inference modeling related to People Analytics.
Contains eleven data sets from the book 'Handbook of
Regression Modeling in People Analytics' by Keith McNulty (2020).
library(peopleanalyticsdata)
data(package = 'peopleanalyticsdata')
10 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
DT::datatable(sociological_data,
fillContainer = FALSE, options = list(autoWidth = TRUE, pageLength = 4, scrollX =
Southern
1 66417 50 157 Unskilled F 5
Asia
Southern
2 55124 50 156 Unskilled F 5
Asia
Southern
3 55124 50 155 Unskilled F 5
Asia
Southern
4 54238 50 154 Unskilled F 5
Asia
11 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Data Frame Summary
Dimensions: 2618 x 9
Duplicates: 16
Freqs (% of
No Variable Stats / Values Graph Missing
Valid)
annual_income_ppp Mean (sd) : 76040.1 (20981.7)
min < med < max:
2863 < 82140 < 361 distinct 10
1
[integer] 119564
IQR (CV) : 27459 (0.3) values (0.4%)
average_wk_hrs Mean (sd) : 44.2 (5.9)
min < med < max:
30 < 43 < 55
IQR (CV) : 11 26 distinct 34
2
[integer] (0.1) values (1.3%)
education_months Mean (sd) : 179.3 (39.7)
min < med < max:
40 < 184 < 280
IQR (CV) 173 distinct 19
3
[integer] : 50 (0.2) values (0.7%)
2021-07-07
12 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Boxplot
A boxplot aims to study the distribution. It can also show the distribution within multiple groups, along with the median, range,
and outliers.
The dark line inside the box represents the median. The top of box is 75% percentile and bottom of box is 25% percentile.
The end points of the lines (a.k.a whiskers) is at a distance of 1.5*IQR, where IQR or Inter Quartile Range is the distance
between 25th and 75th percentiles. The points outside the whiskers are marked as dots and are normally considered as extreme
points.
geom_boxplot()
Requires to specify the data data().
Requires the x and y aesthetic inside aes() while plotting one numeric and one categorical data visualization.
Can change appearance of the boxes with color, fill, alpha arguments.
13 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data)
14 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data) +
aes(x = gender,
y = annual_income_ppp)
15 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data) +
aes(x = gender,
y = annual_income_ppp) +
geom_boxplot()
16 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data) +
aes(x = gender,
y = annual_income_ppp) +
geom_boxplot() +
scale_x_discrete(labels = c('F' = 'Female',
17 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data) +
aes(x = gender,
y = annual_income_ppp,
fill = gender) +
geom_boxplot() +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name ='Gender',
labels = c('Female','Male
values = c('#FF1493' , '#
18 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data) +
aes(x = gender,
y = annual_income_ppp,
fill = gender) +
geom_boxplot() +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name ='Gender',
labels = c('Female','Male
values = c('#FF1493' , '#
ggtitle(label = 'Annual income by gender')
xlab(label = 'Gender') +
ylab(label = 'Annual income in USD')
19 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot
ggplot(data = sociological_data) +
aes(x = gender,
y = annual_income_ppp,
fill = gender) +
geom_boxplot() +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name ='Gender',
labels = c('Female','Male
values = c('#FF1493' , '#
ggtitle(label = 'Annual income by gender')
xlab(label = 'Gender') +
ylab(label = 'Annual income in USD') +
theme_bw()
20 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot - How to add
statistical details
To add statistical details to out boxplot, we will use ggstatsplot is an extension of ggplot2 package for creating graphics with
details from statistical tests included in the information-rich plots themselves.
In a typical exploratory data analysis workflow, data visualization and statistical modeling are two different phases: visualization
informs modeling, and modeling in its turn can suggest a different visualization method, and so on and so forth.
The central idea of ggstatsplot is simple: combine these two phases into one in the form of graphics with statistical details,
which makes data exploration simpler and faster.
21 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Boxplot - How to add
statistical details
This function creates either a violin plot, a box plot, or a mix of two for between-group or between-condition comparisons with results
from statistical tests in the subtitle.
library(ggstatsplot)
ggbetweenstats(
data = sociological_data,
x = gender,
y = annual_income_ppp,
title = "Annual income by gender"
)
22 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Barplot
A barplot displays the relationship between a numeric and a categorical variable.
A type of graph in which different amounts that are being compared are shown using rectangles that have different
lengths but the same width.
geom_bar()
Requires to specify data data().
Provides both x and y inside aes() where, x is either character or factor and y is numeric.
Specifies width of bars with the bins or binwidth argument
Sets stat=identity to make a bar chart create bars instead of a histogram.
Can change appearance of the bars with color, fill, alpha arguments.
23 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Barplot
ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs) +
geom_bar(stat = 'identity', binwidth = 0.5)
24 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Barplot
ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs) +
geom_bar(stat = 'identity', binwidth = 0.5)
scale_x_discrete(labels = c('F' = 'Female',
25 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Barplot
ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs,
fill = gender) +
geom_bar(stat='identity', binwidth = 0.5) +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name = 'Gender',
labels = c('Female','Male
values = c('#FF1493','#41
26 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Barplot
ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs,
fill = gender) +
geom_bar(stat='identity', binwidth = 0.5) +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name = 'Gender',
labels = c('Female','Male
values = c('#FF1493','#41
ggtitle(label = 'Average of working hours b
xlab(label = 'Gender') +
ylab(label = 'Average working hours')
27 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Barplot
ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs,
fill = gender) +
geom_bar(stat='identity', binwidth = 0.5) +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name = 'Gender',
labels = c('Female','Male
values = c('#FF1493','#41
ggtitle(label = 'Average of working hours b
xlab(label = 'Gender') +
ylab(label = 'Average working hours') +
scale_y_continuous(expand = c(0,0)) +
theme_bw()
28 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Barplot
ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs,
fill = gender) +
geom_bar(stat='identity', binwidth = 0.5) +
scale_x_discrete(labels = c('F' = 'Female',
scale_fill_manual(name = 'Gender',
labels = c('Female','Male
values = c('#FF1493','#41
ggtitle(label = 'Average of working hours b
xlab(label = 'Gender') +
ylab(label = 'Average working hours') +
scale_y_continuous(expand = c(0,0)) +
theme_bw() +
coord_flip()
29 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Density Histogram
we attempt to visualize the underlying probability distribution of the data by drawing an appropriate continuous curve.
geom_histogram()
A histogram corresponds to a set of filled rectangles whose heights correspond to the counts and whose widths
correspond to the width of the variable bins.
In a density plot,
geom_density()
Dedicated to change the appearance of the curve with size, col, lty arguments.
30 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Density Histogram
ggplot(data = sociological_data) +
aes(x = education_months,
y = ..density..) +
geom_histogram()
31 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Density Histogram
ggplot(data = sociological_data) +
aes(x = education_months,
y = ..density..) +
geom_histogram() +
geom_density(size = 1,
col = 'black',
lty = 2)
32 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Density Histogram
ggplot(data = sociological_data) +
aes(x = education_months,
y = ..density..) +
geom_histogram() +
geom_density(size = 1,
col = 'black',
lty = 2) +
xlab(label = 'Education (months)') +
ylab(label = 'Density')
33 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
One-variable visualization: Density Histogram
ggplot(data = sociological_data) +
aes(x = education_months,
y = ..density..) +
geom_histogram(aes(color = gender,
fill = gender),
alpha = 0.4,
position = 'identity') +
geom_density(aes(color = gender),
size = 1,
lty = 2) +
xlab(label = 'Education (months)') +
ylab(label = 'Density') +
scale_fill_manual(values = c('#FF1493', '#4
scale_color_manual(values = c('#FF1493', '#
34 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Scatterplot
A scatterplot aims to understand the nature of relationship between two dependant variables.
geom_point()
Requires to specify data data().
Requires the x and y aesthetic inside aes().
geom_smooth()
Draws a smoothing line (based on loess) by default, can be tweaked to draw the line of best fit by setting method='lm'.
35 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months) +
geom_point()
36 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months) +
geom_point(shape = 18,
color = '#88398a')
37 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months) +
geom_point(shape = 18,
color = '#88398a') +
xlab(label = 'Annual income in USD') +
ylab(label = 'Education (months)')
38 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months) +
geom_point(shape = 18,
color = '#88398a') +
xlab(label = 'Annual income in USD') +
ylab(label = 'Education (months)') +
geom_smooth(method = lm)
39 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months) +
geom_point(shape = 18,
color = '#88398a') +
xlab(label = 'Annual income in USD') +
ylab(label = 'Education (months)') +
geom_smooth(method = lm,
linetype = 'dashed',
color = 'darkred',
fill = 'blue')
40 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months,
color = job_type) +
geom_point(shape = 18) +
geom_smooth(method = lm,
linetype = 'dashed') +
xlab(label = 'Annual income in USD') +
ylab(label = 'Education (months)') +
scale_color_manual(name = 'Job type',
values = c('#E69F00','#5
theme(legend.position = "bottom")
41 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
library(ggpmisc)
library(dplyr)
sociological_data_S <- sociological_data
filter(job_type %i
p + theme(legend.position = "bottom",
text = element_text(size = 10))
stat_poly_eq(formula = y ~ x,
data = sociological_data
color='#E69F00',
rr.digits = 3,
aes(annual_income_ppp,
education_months,
label = paste('Skill
..eq.l
'~ital
..rr.l
sep =
eq.with.lhs = 'italic(ed
eq.x.rhs = '~italic(inc)
parse = TRUE,
label.y.npc = .95,
label.x.npc = .01)
42 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Two-variable visualization : Scatterplot
library(ggpmisc)
library(dplyr)
sociological_data_U <- sociological_data
filter(job_type %i
p + theme(legend.position = "bottom",
text = element_text(size = 10))
stat_poly_eq(formula = y ~ x,
data = sociological_data
color='#56B4E9',
rr.digits = 3,
aes(annual_income_ppp,
education_months,
label = paste('Unski
..eq.l
'~ital
..rr.l
sep =
eq.with.lhs = 'italic(ed
eq.x.rhs = '~italic(inc)
parse = TRUE,
label.y.npc = .90,
label.x.npc = .01)
43 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Correlation matrix
A correlation matrix is simply a table that displays the correlation coefficients for different numerix variables. The
matrix depicts the correlation between all the possible pairs of values in a table. It is a powerful tool to summarize a
large dataset and to identify and visualize patterns in the given data.
corr()
Calculates the correlation between two variables.
44 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Three-variable visualization : Correlation matrix
DT::datatable(round(cor(sociological_data[,-c(4,5,6)]),3),
fillContainer = FALSE, options = list(autoWidth = TRUE, scrollX = TRUE))
45 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Three-variable visualization : Correlation matrix
library(GGally)
ggpairs(sociological_data, columns = 1:3)
46 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Three-variable visualization : Correlation matrix
library(GGally)
ggpairs(sociological_data, columns = 1:3) +
theme_bw()
47 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Three-variable visualization : Correlation matrix
library(GGally)
ggpairs(sociological_data, columns = 1:3,
lower = list(continuous = wrap('smooth',
method = 'lm',
color='grey')),
diag = list(continuous = wrap('barDiag')),
upper = list(continuous = wrap('cor', size = 5))) +
theme_bw()
48 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Three-variable visualization : correlation matrix
library(GGally)
ggpairs(sociological_data,
columns = 1:3,
ggplot2::aes(colour = gender))
49 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Graph appearance enhancement : changing the
theme
library(ggprism)
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = average_wk_hrs) +
geom_point(aes(color = factor(family_size))
xlab(label = 'Indivdual annual income (PPP
ylab(label = 'Average number of hours per w
scale_color_manual(name = 'Family size',
values = c('#1B9E77', '#
'#7570B3', '#
'#66A61E', '#
'#A6761D', '#
'#4267B2')) +
theme_prism(base_size = 12)
50 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Graph appearance enhancement : making
animation
library(gganimate)
51 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
How to make your plot looks professional
library(ggprism)
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months,
color = job_type) +
geom_point() +
ggtitle('Education and income by job type')
xlab(label = 'Annual income in USD') +
ylab(label = 'Total months spend in educati
labs(caption = 'Source : Sociological surve
scale_color_manual(name ='Job type',
labels = c('Skilled','Un
values = c('#E69F00','#5
theme_prism(base_size = 12) +
theme(legend.position = "bottom")
52 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
How to make your plot looks professional
library(magick)
library(ggprism)
ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months,
color = job_type) +
geom_point() +
ggtitle('Education and income by job type')
xlab(label = 'Annual income in USD') +
ylab(label = 'Total months spend in educati
labs(caption = 'Source : Sociological surve
scale_color_manual(name ='Job type',
labels = c('Skilled','Un
values = c('#E69F00','#5
theme_prism(base_size = 12) +
theme(legend.position = "bottom")
grid::grid.raster(image_read('figs/RStudio-Lo
x = 0.1,
y = 0.005,
just = c('left', 'bottom'),
width = unit(1, 'inches'))
53 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
How to lay out multiple plots
We will use patchwork package an extension of ggplot2 that combine two or more graphs together.
54 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
How to lay out multiple plots
library(patchwork)
p1 <- ggplot(data = sociological_data) +
aes(x = annual_income_ppp,
y = education_months) +
geom_point(shape = 18,
color = '#88398a') +
xlab(label = 'Annual income in USD') +
ylab(label = 'Education (months)')
p2 <- ggplot(data = sociological_data) +
aes(x = gender,
y = average_wk_hrs,
fill = gender) +
geom_bar(stat='identity', binwidth = 0.
scale_x_discrete(labels = c('F' = 'Fema
scale_fill_manual(name = 'Gender',
labels = c('Female','Male
values = c('#FF1493','#41
ggtitle(label = 'Average of working hou
xlab(label = 'Gender') +
ylab(label = 'Average working hours') +
scale_y_continuous(expand = c(0,0)) +
theme_bw()
p1 / p2
55 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Graph export options
ggsave('figs/figure.jpeg', width = 8, height = 6, units = 'in', dpi = 600, limitsize = TRUE
56 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/
Thanks!
@rladiestunis
@rladiestunus
57 / 57
https://rladiestunis.github.io/useR-2021-ggplot2-tutorial/