5.9 Time: Line charts & events

5.9.1 Data & Packages & functions

  • Line and path plots typically used for time series data (see Appendix Line vs. path plots)
  • Line plots (geom_line()): join the points from left to right
    • Have time on the x-axis, showing how a single variable has changed over time
  • Path plots (geom_path()): join them in the order that they appear in the dataset (in other words, a line plot is a path plot of the data sorted by x value)
  • Below we’ll also use gtrends() from the gtrendsR package to obtain search frequencies.
  • And we’ll use pivot_wider() from the tidyr package.
  • ggnewscale: Can be used to reset a scale if we want to generate several legends
    • And we’ll use scale modification to show proper legends in line plots

5.9.2 Graph

  • Here we’ll reproduce Figure 5.15 (but with ggplot2) (Bauer et al. 2020)
  • Questions:
    • What does the graph show? What are the underlying variables (and data)?
    • How many scales/mappings does it use? Could we reduce them?
    • What do you like, what do you dislike about the figure? What is good, what is bad?
    • What kind of information could we add to the graph (if any)?
    • How would you approach a replication of the graph?

Lines (trends) and events

Figure 5.15: Lines (trends) and events

5.9.3 Lab: Data & code

  • Learning objectives
    • How to plot dates
    • How to make line plots
    • How to create manual legends for various elements
    • How to visualize events

We’ll start by preparing the data.

  1. We download data from google on Google Searches.
## Warning: funs() is soft deprecated as of dplyr 0.8.0
## Please use a list of either functions or lambdas: 
##   # Simple named list: 
##   list(mean = mean, median = median)
##   # Auto named with `tibble::lst()`: 
##   tibble::lst(mean, median)
##   # Using lambdas
##   list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
## This warning is displayed once per session.
2018-03-01 2 4
2018-03-02 1 3
2018-03-03 0 1
2018-03-04 0 1
2018-03-05 1 4
2018-03-06 1 5

  1. We plot the data an add our own annotations in Figure 5.16. Let’s go through the code together.
Lines (trends) and events

Figure 5.16: Lines (trends) and events

5.9.4 Exercise

  1. Use the code from above and investigate Google searches for two other topics (e.g. “COVID” and “Hydroxychloroquine”). Choose a sensible time period for your search. And choose a sensible geographic area (e.g., geo = "US").
  2. Convert the data into longformat etc. (following the steps above) so that you can visualize it as a lineplot in ggplot.
  3. Add events to your lineplots (e.g., one could take one of Trump’s tweets as an event).
  4. Try to visualize a legend (it’s challenging!).


Bauer, Paul C, Frederic Gerdon, Florian Keusch, and Frauke Kreuter. 2020. “The Impact of the GDPR Policy on Data Sharing/Privacy Attitudes.” Preliminary Draft, 1–22.