0% found this document useful (0 votes)
8 views

Intro Ggplot2 3

Uploaded by

zopauy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Intro Ggplot2 3

Uploaded by

zopauy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Themes from

scratch
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H G G P L O T 2

Rick Scave a
Founder, Scave a Academy
The themes layer
All non-data ink

Visual elements not part of the data

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


The themes layer
All non-data ink

Visual elements not part of the data

Three types

type
text
line
rectangle

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


The themes layer
All non-data ink

Visual elements not part of the data

Three types

type modi ed using


text element_text()
line element_line()
rectangle element_rect()

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


A starting plot...
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


The text elements
text
axis.title
axis.title.x
axis.title.x.top
axis.title.x.bottom
axis.title.y
axis.title.y.left
axis.title.y.right
title
legend.title
plot.title
plot.subtitle
plot.caption
plot.tag
axis.text
axis.text.x
axis.text.x.top
axis.text.x.bottom
axis.text.y
axis.text.y.left
axis.text.y.right
legend.text
strip.text
strip.text.x
strip.text.y

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


The text elements
theme(
text,
axis.title,
axis.title.x,
axis.title.x.top,
axis.title.x.bottom,
axis.title.y,
axis.title.y.left,
axis.title.y.right,
title,
legend.title,
plot.title,
plot.subtitle,
plot.caption,
plot.tag,
axis.text,
axis.text.x,
axis.text.x.top,
axis.text.x.bottom,
axis.text.y,
axis.text.y.left,
axis.text.y.right,
legend.text,
strip.text,
strip.text.x,
strip.text.y)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Adjusting theme elements
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6) +
theme(axis.title = element_text(color = "blue"))

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


A starting plot...
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Line elements
theme(
line,
axis.ticks,
axis.ticks.x,
axis.ticks.x.top,
axis.ticks.x.bottom,
axis.ticks.y,
axis.ticks.y.left,
axis.ticks.y.right,
axis.line,
axis.line.x,
axis.line.x.top,
axis.line.x.bottom,
axis.line.y,
axis.line.y.left,
axis.line.y.right,
panel.grid,
panel.grid.major,
panel.grid.major.x,
panel.grid.major.y,
panel.grid.minor,
panel.grid.minor.x,
panel.grid.minor.y)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


A starting plot...
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Rect elements
theme(
rect,
legend.background,
legend.key,
legend.box.background,
panel.background,
panel.border,
plot.background,
strip.background,
strip.background.x,
strip.background.y)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Hierarchical naming reflects inheritance rules
e.g. Text e.g. Lines

text line
axis.title axis.ticks
axis.title.x axis.ticks.x
axis.title.x.top axis.ticks.x.top
axis.title.x.bottom axis.ticks.x.bottom
axis.title.y axis.ticks.y
axis.title.y.left axis.ticks.y.left,
axis.title.y.right axis.ticks.y.right
axis.line
axis.line.x
axis.line.x.top
axis.line.x.bottom
axis.line.y
axis.line.y.left
axis.line.y.right

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


element_blank()
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6) +
theme(line = element_blank(),
rect = element_blank(),
text = element_blank())

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Let's practice!
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H G G P L O T 2
Theme flexibility
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H G G P L O T 2

Rick Scave a
Founder, Scave a Academy
Ways to use themes
1. From scratch (last video)

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Ways to use themes
1. From scratch (last video)

2. Theme layer object

3. Built-in themes
ggplot2 or ggthemes packages

4. Built-in themes from other packages

5. Update/Set default theme

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Defining theme objects
Useful when you have many plots

Provides consistency in style

Apply a speci c theme everywhere

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Defining theme objects
z <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_jitter(alpha = 0.6) +
scale_x_continuous("Sepal Length (cm)", limits = c(4,8), expand = c(0,0)) +
scale_y_continuous("Sepal Width (cm)", limits = c(1.5,5), expand = c(0,0)) +
scale_color_brewer("Species", palette = "Dark2", labels = c("Setosa", "Versicolor", "Virginica"))

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Defining theme objects
z + theme(text = element_text(family = "serif", size = 14),
rect = element_blank(),
panel.grid = element_blank(),
title = element_text(color = "#8b0000"),
axis.line = element_line(color = "black"))

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Defining theme objects
theme_iris <- theme(text = element_text(family = "serif", size = 14),
rect = element_blank(),
panel.grid = element_blank(),
title = element_text(color = "#8b0000"),
axis.line = element_line(color = "black"))

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Reusing theme objects
z + theme_iris

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Reusing theme objects
m <- ggplot(iris, aes(x = Sepal.Width)) +
geom_histogram(binwidth = 0.1,
center = 0.05)
m

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Reusing theme objects
m +
theme_iris

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Reusing theme objects
m +
theme_iris +
theme(axis.line.x = element_blank())

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Ways to use themes
1. From scratch (last video)

2. Theme layer object

3. Built-in themes
ggplot2 or ggthemes packages

4. Built-in themes from other packages

5. Update/Set default theme

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Using built-in themes
Use theme_*() functions to access built-in themes.

z +
theme_classic()

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Using built-in themes
Use theme_*() functions to access built-in themes.

z +
theme_classic() +
theme(text = element_text(family = "serif"))

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Ways to use themes
1. From scratch (last video)

2. Theme layer object

3. Built-in themes
ggplot2 or ggthemes packages

4. Built-in themes from other packages

5. Update/Set default theme

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


The ggthemes package
Use the ggthemes package for more functions.

library(ggthemes)
z +
theme_tufte()

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Ways to use themes
1. From scratch (last video)

2. Theme layer object

3. Built-in themes
ggplot2 or ggthemes packages

4. Built-in themes from other packages

5. Update/Set default theme

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Updating themes
original <- theme_update(text = element_text(family = "serif", size = 14),
rect = element_blank(),
panel.grid = element_blank(),
title = element_text(color = "#8b0000"),
axis.line = element_line(color = "black"))

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Updating themes
z

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Setting themes
theme_set(original)

# Alternatively
# theme_set(theme_grey())

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Let's practice!
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H G G P L O T 2
Effective
explanatory plots
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H G G P L O T 2

Rick Scave a
Founder, Scave a Academy
Our goal, an effective explanatory plot

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Complete data
dplyr::glimpse(gm2007_full)

Observations: 142
Variables: 3
$ country <fct> "Afghanistan", "Albania", "Algeria", "Angola", "Argentina", "Au...
$ lifeExp <dbl> 43.828, 76.423, 72.301, 42.731, 75.320, 81.235, 79.829, 75.635,...
$ continent <fct> Asia, Europe, Africa, Africa, Americas, Oceania, Europe, Asia, ...

1 We would begin with our complete data set, which contains three variables for 142 countries.

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


First exploratory plots - distributions
ggplot(gm2007_full, aes(lifeExp)) +
geom_histogram()

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


First exploratory plots - distributions
ggplot(gm2007_full_arranged,
aes(index, lifeExp)) +
geom_point()

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


First exploratory plots - distributions
ggplot(gm2007_full_arranged,
aes(index, lifeExp, col = continent)) +
geom_point()

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Our data
dplyr::glimpse(gm2007)

Observations: 20
Variables: 3
$ country <fct> "Swaziland", "Mozambique", "Zambia", "Sierra Leone", "Lesotho...
$ lifeExp <dbl> 39.613, 42.082, 42.384, 42.568, 42.592, 42.731, 43.487, 43.82...
$ continent <fct> Africa, Africa, Africa, Africa, Africa, Africa, Africa, Asia,...
>

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2
Use intuitive and attractive geoms

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Add text labels to your plot

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Use appropriate scales

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Add useful titles and citations

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Remove non-data ink

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Add threshold lines

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Add informative text

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Add embellishments

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2


Let's practice!
I N T R O D U C T I O N T O D ATA V I S U A L I Z AT I O N W I T H G G P L O T 2

You might also like