Case Study: Democracy in Guatemala
Explore how to convert wide data into tidy format using pivot_longer in R. Learn to create a time series line graph with ggplot2 to track democracy scores in Guatemala over 40 years. Understand setting proper variable types and aesthetics for clear data visualization.
We'll cover the following...
Let’s explore another example of how to convert a data frame that isn’t in tidy format (wide format) to a data frame that’s in tidy format (long/narrow format). We’ll do this by using the pivot_longer() function from the tidyr package again.
Furthermore, we’ll use functions from the ggplot2 and dplyr packages to produce a time series plot showing how the democracy scores have changed over the 40 years from 1952 to 1992 for Guatemala. Recall that we saw time series plots on creating line graphs using geom_line().
Let’s use the dem_score data frame we imported before, but focus on only the data corresponding to Guatemala.
Let’s lay out the grammar of graphics.
First, we know we need to set data = ...