Tutorial-10

Author

Dr. Kate Saunders

Creating Infographics

Learning Objectives

  • Practice iterating and polishing your visualisations

  • Practice adding additional additive elements to enhance your data narrative

Motivation

Motivation

Effective communication sometimes requries extending you visualisation by adding narrative elements to guide your audience. This includes elements such as text, highlighting, arrows and labels.

In this tutorial, we’ll practice adding narrative elements to our visualisation, so we can better deliver the key messages to the audience.

Question 1

The following is a basic example from the ggplot2 book from the section on annotations.

That shows how plots can benefit from additional narrative elements.

  1. Using this code to start. Try adding the text layer to the plot.
presidential <- subset(presidential, start > economics$date[1])

ggplot(economics) + 
  geom_rect(
    data = presidential,
    aes(xmin = start, xmax = end, fill = party), 
    ymin = -Inf, ymax = Inf, alpha = 0.2, 
  )  + 
  geom_line(aes(date, unemploy)) 
  1. Fix the colour scale so it appropriately reflects party colours.

  2. Add vertical lines to better distinguish between the different presidencies.

  3. Polish the title, axis labels and legends. Use ?economics to check what units to use.

presidential <- subset(presidential, start > economics$date[1])

ggplot(economics) + 
  geom_rect(
    aes(xmin = start, xmax = end, fill = party), 
    ymin = -Inf, ymax = Inf, alpha = 0.2, 
    data = presidential
  ) + 
  geom_vline(
    aes(xintercept = as.numeric(start)), 
    data = presidential,
    colour = "grey50", alpha = 0.5
  ) + 
  geom_text(
    aes(x = start, y = 2500, label = name), 
    data = presidential, 
    size = 3, vjust = 0, hjust = 0, nudge_x = 50
  ) + 
  geom_line(aes(date, unemploy)) + 
  scale_fill_manual(values = c("blue", "red")) +
  xlab("Date") +
  ylab("Unemployed (in 1000s)") +
  ggtitle("How does unemployment vary by Presidency?")

To go from the starter version to my final polished verion it took me 34 small iterations.

Here are my core changes, which I tried multiple iterations of to get right:

  • Updated the theme layer

  • Moved the legend to improve the data-density of the plot

  • Changed the vertical line display to make them clearer, while still not being visually distracting

Here are some prompts I used to help me.

min_date = min(economics$date)

ggplot(economics) +
  geom_rect(
    data = presidential,
    aes(xmin = start, xmax = end, fill = party),
    ymin = -Inf, ymax = Inf, alpha = 0.2
    ) +
  geom_vline(
    data = presidential,
    aes(xintercept = as.numeric(start)),
    colour = "gray50", alpha = 0.5, linetype = "dashed") +
  geom_text(
    data = presidential,
    aes(x = start, y = 2500, label = name),
    size = 2.5, vjust = 0, hjust = 0, nudge_x = 50
  ) +
  geom_line(aes(date, unemploy)) +
  scale_fill_manual(values = c("blue", "red")) +
  xlab("Date") +
  ylab("Unemployed (in 1000s)") +
  ggtitle("How does unemployment vary by Presidency?") +
  theme_bw() +
  theme(
    legend.position = c(0.2, 0.9),
    legend.direction = "horizontal",
    legend.title = element_blank(),
    legend.background = element_rect(
    fill = "white",
    colour = "grey80",
    linewidth = 0.5
  ),
  legend.text = element_text(size = 8),
  legend.key.size = unit(0.6, "lines"),
  legend.spacing.x = unit(0.3, "lines")
)

Question 2

There are other useful ways to add annotations to your plot such as using the gghighlight package.

if(!require(gghighlight))
  install.packages("gghighlight")

library(gghighlight)

Take a look at the examples here and examples here

  1. Use this code to generate a basic plot to start - Warning: It’s not good.
big_tech_data = read_csv("week10/data/big-tech-stock-price.csv")
ggplot(big_tech_data) +
    geom_line(aes(x= date, y = high, colour = stock_symbol))

  1. Use gghighlight to show stocks with a high > 600. Try looking at ?gghighlight to get started.

p <- ggplot(big_tech_data) +
  geom_line(aes(x= date, y = high, colour = stock_symbol)) + 
  gghighlight(max(high) > 600) ## Highlight top stocks 

p
  1. Try this slight variation as well using facet_wrap()

p + 
  theme_bw() + 
  facet_wrap(~ stock_symbol) ## Create small multiples

Finishing Up

  • Make sure you understand how to position text or a label on your plot by changing the x and y coordinates.
  • Think how you use colour to highlight your messages.

  • Lastly, and most importantly, remember don’t always go adding additional elements if they serve your data narrative.