The facets layer IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 - - PowerPoint PPT Presentation

the facets layer
SMART_READER_LITE
LIVE PREVIEW

The facets layer IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 - - PowerPoint PPT Presentation

The facets layer IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2 Rick Scavetta Founder, Scavetta Academy Facets Straight-forward yet useful Concept of Small Multiples Popularized by Edward Tufte Visualization of Quantitative Information,


slide-1
SLIDE 1

The facets layer

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

Rick Scavetta

Founder, Scavetta Academy

slide-2
SLIDE 2

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Facets

Straight-forward yet useful Concept of Small Multiples Popularized by Edward Tufte Visualization of Quantitative Information, 1983

slide-3
SLIDE 3

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

slide-4
SLIDE 4

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

slide-5
SLIDE 5

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

iris.wide

p <- ggplot(iris.wide, aes(x = L y = W ccolo geom_jitter(alpha = 0.7) + scale_color_brewer(palette = " coord_fixed() p

slide-6
SLIDE 6

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

iris.wide & facet_grid()

p <- ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) + geom_jitter(alpha = 0.7) + scale_color_brewer(palette = "Set1") + coord_fixed() p + facet_grid(cols = vars(Species))

slide-7
SLIDE 7

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Formula notation

p <- ggplot(iris.wide, aes(x = Length, y = Width, color = Part)) + geom_jitter(alpha = 0.7) + scale_color_brewer(palette = "Set1") + coord_fixed() p + facet_grid(. ~ Species)

slide-8
SLIDE 8

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

iris.wide2

ggplot(iris.wide2, aes(x = Part, y = setosa, color = Measure)) + geom_jitter() ggplot(iris.wide2, aes(x = Part, y = versicolor, color = Measure)) + geom_jitter() ggplot(iris.wide2, aes(x = Part, y = virginica, color = Measure)) + geom_jitter()

slide-9
SLIDE 9

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

iris.tidy

ggplot(iris.tidy, aes(x = Measure, y = Value, color = Part)) + geom_jitter() + facet_grid(cols = vars(Species))

slide-10
SLIDE 10

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

iris.tidy faceting done

wrong:

ggplot(iris.tidy, aes(x = Me y = Va color geom_jitter() + facet_grid(rows = vars(Spe

slide-11
SLIDE 11

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Other options

Split according to rows and columns

slide-12
SLIDE 12

Let's practice!

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

slide-13
SLIDE 13

Facet labels and

  • rder

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

Rick Scavetta

Founder, Scavetta Academy

slide-14
SLIDE 14

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

A new dataframe

# Plot p <- ggplot(msleep2, aes(bod bra geom_point(alpha = 0.6, sh coord_fixed() p

slide-15
SLIDE 15

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

A new dataframe, with facets

p + facet_grid(rows = vars(vor

slide-16
SLIDE 16

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

A new dataframe, with facets

p + facet_grid(rows = vars(vor

slide-17
SLIDE 17

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Poor labels and order

p + facet_grid(rows = vars(vor

Two typical problems with facets: Poorly labeled (e.g. non descriptive) Wrong or inappropriate

  • rder
slide-18
SLIDE 18

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Poor labels and order

p + facet_grid(rows = vars(vor

Solutions: Easy: Add labels in ggplot Better: Relabel and rearrange factor variables in your dataframe

slide-19
SLIDE 19

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

The labeller argument

# Default is to label the va p + facet_grid(rows = vars(vor labeller = labe

slide-20
SLIDE 20

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using label_both adds the variable name

# Print variable name also p + facet_grid(rows = vars(vor labeller = la

slide-21
SLIDE 21

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Two variables on one side

p + facet_grid(rows = vars(vor con

slide-22
SLIDE 22

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using label_context avoids ambiguity

p + facet_grid(rows = vars(vor con labeller = la

slide-23
SLIDE 23

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Use rows and columns when appropriate

p + facet_grid(rows = vars(vor cols = vars(con labeller = labe

slide-24
SLIDE 24

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Use rows and columns when appropriate

p + facet_grid(rows = vars(vor cols = vars(con

slide-25
SLIDE 25

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Use rows and columns when appropriate

slide-26
SLIDE 26

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Relabeling and reordering factors

msleep2$conservation <- fct_recode(msleep2$conservation, Domesticated = "domestic `Least concern` = "lc", `Near threatened` = "nt" Vulnerable = "vu", Endangered = "en") msleep2$vore = fct_recode(msleep2$vore, Carnivore = "carni", Herbivore = "herbi", Insectivore = "insecti", Omnivore = "omni")

slide-27
SLIDE 27

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Reinitialize plot with new labels

# Plot p <- ggplot(msleep2, aes(bod bra geom_point(alpha = 0.6, sh coord_fixed() p + facet_grid(rows = vars(vor cols = vars(con

slide-28
SLIDE 28

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Reinitialize plot with new labels

slide-29
SLIDE 29

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Changing the order of levels

# Change order of levels: msleep2$conservation = fct_relevel(msleep2$conservation, c("Domesticated", "Least concern", "Near threatened", "Vulnerable", "Endangered"))

slide-30
SLIDE 30

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Reinitialize plot with new order

slide-31
SLIDE 31

Let's practice!

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

slide-32
SLIDE 32

Facet plotting spaces

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

Rick Scavetta

Founder, Scavetta Academy

slide-33
SLIDE 33

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Facets and variable plotting spaces

Reasons to not use consistent plotting spaces: Variable type Subsets contains Continuous Wildly different ranges Categorical Different groups

slide-34
SLIDE 34

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space...

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh coord_fixed() + facet_grid(rows = vars(vor cols = vars(con

slide-35
SLIDE 35

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

... but not with xed scales

ggplot(msleep2, aes(bodywt_log, brainwt_log)) + geom_point(alpha = 0.6, shape = 16) + coord_fixed() + facet_grid(rows = vars(vore), cols = vars(conservation), scales = "free_x") Error: coord_fixed doesn't support free scales

slide-36
SLIDE 36

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free_

slide-37
SLIDE 37

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free_

slide-38
SLIDE 38

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free"

slide-39
SLIDE 39

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(x = body y = name geom_point() + facet_grid(rows = vars(vor

slide-40
SLIDE 40

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(x = body y = name geom_point() + # Free the y scales and sp facet_grid(rows = vars(vor scales = "free_

slide-41
SLIDE 41

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(x = body y = name geom_point() + # Free the y scales and sp facet_grid(rows = vars(vor scales = "free_ space = "free_y

slide-42
SLIDE 42

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Final adjustments

msleep2 <- msleep2 %>% # Arrange from lo to hi weight arrange(-bodywt_log) %>% # Redefine factor levels in or mutate(name = as_factor(name)) # New order is reflected in y ax ggplot(msleep2, aes(x = bodywt_l y = name)) + geom_point() + # Free the y scales and space facet_grid(rows = vars(vore), scales = "free_y", space = "free_y")

slide-43
SLIDE 43

Let's practice!

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

slide-44
SLIDE 44

Facet wrap & margins

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2

Rick Scavetta

Founder, Scavetta Academy

slide-45
SLIDE 45

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Adjusting the plotting space

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free"

slide-46
SLIDE 46

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using facet_wrap()

Use cases:

  • 1. When you want both x and y axes to be free on every individual

plot i.e. Not just per row or column as per facet_grid()

slide-47
SLIDE 47

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using facet_wrap() - Scenario 1

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_wrap(vars(vore, cons scales = "free"

slide-48
SLIDE 48

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using facet_wrap()

Use cases:

  • 1. When you want both x and y axes to be free on every individual

plot i.e. Not just per row or column as per facet_grid()

  • 2. When your categorical (factor) variable has many groups (levels)

i.e. too many sub plots for column or row-wise faceting A more typical scenario

slide-49
SLIDE 49

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using facet_wrap() - Scenario 2

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_wrap(vars(order))

slide-50
SLIDE 50

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using margin plots

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free"

slide-51
SLIDE 51

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using margin plots

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free" margins = TRUE)

slide-52
SLIDE 52

INTERMEDIATE DATA VISUALIZATION WITH GGPLOT2

Using margin plots

ggplot(msleep2, aes(bodywt_l brainwt_ geom_point(alpha = 0.6, sh facet_grid(rows = vars(vor cols = vars(con scales = "free" margins = "cons

slide-53
SLIDE 53

Let's practice!

IN TERMEDIATE DATA VIS UALIZ ATION W ITH GGP LOT2