+ - 0:00:00
Notes for current slide
Notes for next slide

Tips for effective data visualization



Data Science in a Box

1 / 37

Designing effective visualizations

2 / 37

Keep it simple

3 / 37

Use color to draw attention

4 / 37

Tell a story

Credit: Angela Zoss and Eric Monson, Duke DVS

5 / 37

Principles for effective visualizations

6 / 37

Principles for effective visualizations

  • Order matters
  • Put long categories on the y-axis
  • Keep scales consistent
  • Select meaningful colors
  • Use meaningful and nonredundant labels
7 / 37

Data

In September 2019, YouGov survey asked 1,639 GB adults the following question:

In hindsight, do you think Britain was right/wrong to vote to leave EU?

  • Right to leave
  • Wrong to leave
  • Don't know

Source: YouGov Survey Results, retrieved Oct 7, 2019

8 / 37

Order matters

9 / 37

Alphabetical order is rarely ideal

ggplot(brexit, aes(x = opinion)) +
geom_bar()
10 / 37

Order by frequency

fct_infreq: Reorder factors' levels by frequency

ggplot(brexit, aes(x = fct_infreq(opinion))) +
geom_bar()
11 / 37

Clean up labels

ggplot(brexit, aes(x = opinion)) +
geom_bar() +
labs(
x = "Opinion",
y = "Count"
)
12 / 37

Alphabetical order is rarely ideal

ggplot(brexit, aes(x = region)) +
geom_bar()
13 / 37

Use inherent level order

fct_relevel: Reorder factor levels using a custom order

brexit <- brexit %>%
mutate(
region = fct_relevel(
region,
"london", "rest_of_south", "midlands_wales", "north", "scot"
)
)

14 / 37

Clean up labels

fct_recode: Change factor levels by hand

brexit <- brexit %>%
mutate(
region = fct_recode(
region,
London = "london",
`Rest of South` = "rest_of_south",
`Midlands / Wales` = "midlands_wales",
North = "north",
Scotland = "scot"
)
)

15 / 37

Put long categories on the y-axis

16 / 37

Long categories can be hard to read

17 / 37

Move them to the y-axis

ggplot(brexit, aes(y = region)) +
geom_bar()
18 / 37

And reverse the order of levels

fct_rev: Reverse order of factor levels

ggplot(brexit, aes(y = fct_rev(region))) +
geom_bar()
19 / 37

Clean up labels

ggplot(brexit, aes(y = fct_rev(region))) +
geom_bar() +
labs(
x = "Count",
y = "Region"
)
20 / 37

Pick a purpose

21 / 37

Segmented bar plots can be hard to read

ggplot(brexit, aes(y = region, fill = opinion)) +
geom_bar()
22 / 37

Use facets

ggplot(brexit, aes(y = opinion, fill = region)) +
geom_bar() +
facet_wrap(~region, nrow = 1)
23 / 37

Avoid redundancy?

24 / 37

Redundancy can help tell a story

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1)
25 / 37

Be selective with redundancy

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1) +
guides(fill = "none")
26 / 37

Use informative labels

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1) +
guides(fill = "none") +
labs(
title = "Was Britain right/wrong to vote to leave EU?",
x = NULL, y = NULL
)
27 / 37

A bit more info

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1) +
guides(fill = "none") +
labs(
title = "Was Britain right/wrong to vote to leave EU?",
subtitle = "YouGov Survey Results, 2-3 September 2019",
caption = "Source: https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/x0msmggx08/YouGov%20-%20Brexit%20and%202019%20election.pdf",
x = NULL, y = NULL
)
28 / 37

Let's do better

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1) +
guides(fill = "none") +
labs(
title = "Was Britain right/wrong to vote to leave EU?",
subtitle = "YouGov Survey Results, 2-3 September 2019",
caption = "Source: bit.ly/2lCJZVg",
x = NULL, y = NULL
)
29 / 37

Fix up facet labels

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region,
nrow = 1,
labeller = label_wrap_gen(width = 12)
) +
guides(fill = "none") +
labs(
title = "Was Britain right/wrong to vote to leave EU?",
subtitle = "YouGov Survey Results, 2-3 September 2019",
caption = "Source: bit.ly/2lCJZVg",
x = NULL, y = NULL
)
30 / 37

Select meaningful colors

31 / 37

Rainbow colors not always the right choice

32 / 37

Manually choose colors when needed

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1, labeller = label_wrap_gen(width = 12)) +
guides(fill = "none") +
labs(title = "Was Britain right/wrong to vote to leave EU?",
subtitle = "YouGov Survey Results, 2-3 September 2019",
caption = "Source: bit.ly/2lCJZVg",
x = NULL, y = NULL) +
scale_fill_manual(values = c(
"Wrong" = "red",
"Right" = "green",
"Don't know" = "gray"
))
33 / 37

Choosing better colors

34 / 37

Use better colors

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1, labeller = label_wrap_gen(width = 12)) +
guides(fill = "none") +
labs(title = "Was Britain right/wrong to vote to leave EU?",
subtitle = "YouGov Survey Results, 2-3 September 2019",
caption = "Source: bit.ly/2lCJZVg",
x = NULL, y = NULL) +
scale_fill_manual(values = c(
"Wrong" = "#ef8a62",
"Right" = "#67a9cf",
"Don't know" = "gray"
))
35 / 37

Select theme

ggplot(brexit, aes(y = opinion, fill = opinion)) +
geom_bar() +
facet_wrap(~region, nrow = 1, labeller = label_wrap_gen(width = 12)) +
guides(fill = "none") +
labs(title = "Was Britain right/wrong to vote to leave EU?",
subtitle = "YouGov Survey Results, 2-3 September 2019",
caption = "Source: bit.ly/2lCJZVg",
x = NULL, y = NULL) +
scale_fill_manual(values = c("Wrong" = "#ef8a62",
"Right" = "#67a9cf",
"Don't know" = "gray")) +
theme_minimal()
36 / 37

Your turn!

  • RStudio Cloud > AE 07 - Brexit + Telling stories with dataviz > brexit.Rmd.
  • Change the visualisation in three different ways to tell slightly different stories with it each time.
37 / 37

Designing effective visualizations

2 / 37
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow