Themes from scratch IN TR OD U C TION TO DATA VISU AL IZATION W - - PowerPoint PPT Presentation

themes from scratch
SMART_READER_LITE
LIVE PREVIEW

Themes from scratch IN TR OD U C TION TO DATA VISU AL IZATION W - - PowerPoint PPT Presentation

Themes from scratch IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT 2 Rick Sca v e a Fo u nder , Sca v e a Academ y The themes la y er All non - data ink Vis u al elements not part of the data INTRODUCTION TO DATA


slide-1
SLIDE 1

Themes from scratch

IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT2

Rick Scavea

Founder, Scavea Academy

slide-2
SLIDE 2

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

The themes layer

All non-data ink Visual elements not part of the data

slide-3
SLIDE 3

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

slide-4
SLIDE 4

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

The themes layer

All non-data ink Visual elements not part of the data Three types type modied using text element_text() line element_line() rectangle element_rect()

slide-5
SLIDE 5

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)

slide-6
SLIDE 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

slide-7
SLIDE 7

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)

slide-8
SLIDE 8

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"))

slide-9
SLIDE 9

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)

slide-10
SLIDE 10

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)

slide-11
SLIDE 11

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)

slide-12
SLIDE 12

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)

slide-13
SLIDE 13

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Hierarchical naming reflects inheritance rules

e.g. Text

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

e.g. Lines

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

slide-14
SLIDE 14

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())

slide-15
SLIDE 15

Let's practice!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT2

slide-16
SLIDE 16

Theme flexibility

IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT2

Rick Scavea

Founder, Scavea Academy

slide-17
SLIDE 17

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Ways to use themes

  • 1. From scratch (last video)
slide-18
SLIDE 18

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
slide-19
SLIDE 19

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Defining theme objects

Useful when you have many plots Provides consistency in style Apply a specic theme everywhere

slide-20
SLIDE 20

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"))

slide-21
SLIDE 21

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"))

slide-22
SLIDE 22

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"))

slide-23
SLIDE 23

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Reusing theme objects

z + theme_iris

slide-24
SLIDE 24

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

slide-25
SLIDE 25

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Reusing theme objects

m + theme_iris

slide-26
SLIDE 26

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Reusing theme objects

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

slide-27
SLIDE 27

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
slide-28
SLIDE 28

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Using built-in themes

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

z + theme_classic()

slide-29
SLIDE 29

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"))

slide-30
SLIDE 30

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
slide-31
SLIDE 31

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

The ggthemes package

Use the ggthemes package for more functions.

library(ggthemes) z + theme_tufte()

slide-32
SLIDE 32

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
slide-33
SLIDE 33

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Updating themes

  • riginal <- 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"))

slide-34
SLIDE 34

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Updating themes

z

slide-35
SLIDE 35

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Setting themes

theme_set(original) # Alternatively # theme_set(theme_grey())

slide-36
SLIDE 36

Let's practice!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT2

slide-37
SLIDE 37

Effective explanatory plots

IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT2

Rick Scavea

Founder, Scavea Academy

slide-38
SLIDE 38

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Our goal, an effective explanatory plot

slide-39
SLIDE 39

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, ...

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

1

slide-40
SLIDE 40

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

First exploratory plots - distributions

ggplot(gm2007_full, aes(lifeExp)) + geom_histogram()

slide-41
SLIDE 41

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

First exploratory plots - distributions

ggplot(gm2007_full_arranged, aes(index, lifeExp)) + geom_point()

slide-42
SLIDE 42

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

First exploratory plots - distributions

ggplot(gm2007_full_arranged, aes(index, lifeExp, col = continent)) + geom_point()

slide-43
SLIDE 43

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,... >

slide-44
SLIDE 44

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

slide-45
SLIDE 45

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Use intuitive and attractive geoms

slide-46
SLIDE 46

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Add text labels to your plot

slide-47
SLIDE 47

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Use appropriate scales

slide-48
SLIDE 48

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Add useful titles and citations

slide-49
SLIDE 49

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Remove non-data ink

slide-50
SLIDE 50

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Add threshold lines

slide-51
SLIDE 51

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Add informative text

slide-52
SLIDE 52

INTRODUCTION TO DATA VISUALIZATION WITH GGPLOT2

Add embellishments

slide-53
SLIDE 53

Let's practice!

IN TR OD U C TION TO DATA VISU AL IZATION W ITH G G P L OT2