Data Visualization With R - Principles and Practice
Data Visualization With R - Principles and Practice
WITH
HELLO!
I am Elijah Appiah from
Ghana.
I am an Economist by
profession.
I love everything data, so I
love R!
You can reach me:
secret behind the smile! [email protected]
3
Lesson Goals
Provide compact introduction to
allow readers learn about
visualization techniques.
Datasets
mtcars: base R
wage1: wooldridge package
diamonds: ggplot2 package
5
Variables
Categorical Numeric
Nominal – names, labels, Discrete – counts
categories with no natural e.g. number of
order cylinders of a vehicle
e.g. gender, countries
Ordinal – categories with Continuous – measured
an order even within an interval
e.g. Likert Scales e.g. height, weight
6
Variables (e.m)
Discrete – represents counts
e.g. number of students, grade levels, gender,
number of blue marbles in a jar, etc.
Continuous – represents measurable
amounts
e.g. height, weight, temperature, distance, etc.
7
GGPLOT2
GRAMMAR OF GRAPHICS PLOTS
8
GGPLOT2 LAYERS
GGPLOT2 LAYERS
Scales onto
which data is
mapped
10
GGPLOT2
The package is
ggplot2
The function is
ggplot()
11
Layer: DATA
ggplot(data = df)
Layer: AESTHETICS
The aesthetic attributes include:
x, y, colour (or color), shape, size, fill, alpha,
etc…
Aesthetics are mapped in the aes() function in the
ggplot() function.
13
Layer: AESTHETICS
ggplot(data = df, mapping = aes())
Aesthetic
attributes
14
Layer: AESTHETICS
ggplot(data = df, aes())
Aesthetic
attributes
15
Layer: AESTHETICS
ggplot(df, aes())
Aesthetic
attributes
16
Layer: AESTHETICS
ggplot(mtcars, aes(x = mpg))
17
Layer: AESTHETICS
ggplot(mtcars, aes(x = mpg, y = hp))
18
Layer: GEOMETRIES
The visual elements of plots are defined by geoms.
It is specified as geom_*().
where * denotes the specific type of plot to create.
A bar plot will be geom_bar()
A histogram will be geom_histogram()
A scatter plot will be geom_point()
Layer: GEOMETRIES
The geometric objects (or geoms) are added (+) to the
ggplot() function.
Example:
ggplot(mtcars, aes(x = mpg)) +
geom_histogram()
Layer: GEOMETRIES
ggplot(mtcars, aes(x = mpg)) + geom_histogram()
21
Layer: GEOMETRIES
ggplot(mtcars, aes(x = mpg, y = hp)) + geom_point()
22
Discrete Continuous
Bar Plot – geom_bar() Histogram – geom_histogram()
geom_boxplot() – boxplots.
geom_pointrange()
33
THREE VARIABLES
geom_contour() – contours.
geom_tile() – tile the plane with rectangles.
geom_raster() – equal sized tiles (fast version of
geom_tile())
36
THANKS!
Any questions?
You can find me at: [email protected]