library(tidyverse)
library(tidyr)
library(colorBlindness)
library(vdemdata)
library(plotly)
library(gganimate)Coding Assignment 2
Basic Data Visualizations
Overview
For this assignment, you are going to evaluate modernization theory as laid out in Seymour Martin Lipset’s classic article entitled “Some Social Requisites of Democracy: Economic Development and Political Legitimacy.” How classic is this article? According to Google Scholar, this piece has been cited more than 11.5 thousand times!
We are going to use data from V-Dem and modern data viz tools to explore Lipset’s hypothesis that economic modernization is highly correlated with democracy. We have already done this to some extent by looking at the relationship between wealth and the polyarchy score. But we are going to broaden things out by looking at other measures of democracy contained in the V-Dem dataset. Specifically, you can choose among the following four measures:
- liberal democracy (
v2x_libdem) - participatory democracy (
v2x_partipdem) - deliberative democracy (
v2x_delibdem) - egalitarian democracy (
v2x_egaldem)
Or you can use some other democracy indicator from the V-Dem database. For measuring modernization we will use GDP per capita (e_gdppc). Use the vdemdata package to retrieve your indicators.
For an extra challenge, you could try merging V-Dem democracy indicators with GDP per capita taken from the World Bank using the wbstats package, but this is not required.
Start by running this code chunk to import all of the packages you will need for this exercise. Then start working through the code and questions below. Feel free to grab relevant code chunks from the modules or the exercises that we have used in class so far.
Step 1: Make a line chart showing country trends (20 pts)
a) Use vdem to retrieve your chosen democracy indicator for five countries from different regions that you expect might be interesting to compare for 1970 onward and store it in an object called line_chart_dta. Use select() to keep country name, year, and your indicator, and filter() to select your countries and years. Use glimpse() or look at the data frame to make sure everything looks right.
line_chart_dta <- vdem |>
select(
country = country_name,
year,
____ = ____
) |>
filter(country %in% c("____", "____", "____", "____", "____") &
year >= ____)
glimpse(____)b) Now choose one of the democracy indicators and visualize it with a line chart using ggplot2 for the countries that you chose. Be sure to specify x =, y = and color = in your aes() call and use geom_line() to create the chart. Add appropriate axis labels, a title and a caption. Now add a colorblind-friendly color map and an attractive theme.
ggplot(____, aes(x = ____, y = ____, color = ____)) +
geom_line(linewidth = ____) +
labs(
x = "____",
y = "____",
title = "____",
caption = "____",
color = "____"
) +
scale_color_viridis_d() + # or try scale_color_brewer()
theme_minimal() # or try another themec) Add a vertical line at an interesting point in history or a horizontal line at a notable point on the y-axis and an annotation explaining the significance of the line.
____ +
geom_vline(xintercept = ____, linetype = "____", color = "____") +
annotate("text", x = ____, y = ____, label = "____")d) In a few sentences below, interpret your chart. Describe the levels and trends in democracy scores. Put your answer right below this line in markdown text (DO NOT write your comments in a code chunk!!).
Step 2: Make a column chart comparing country levels (20 pts)
a) Now use a group_by(), summarize() sequence to take country averages for the data that you retrieved for your line chart in Step 1 and store it as an object called column_chrt_dta.
b) Use ggplot() and geom_col() to visualize one of the democracy measures with a column chart (you can use the same measure as in Step 1 or a different one). Use fct_reorder() to arrange the columns in order of the y-axis values. Make sure to add appropriate axis labels, a title and a caption. Change the fill color and add a theme to spruce it up a bit. Remember that these are averages over the same number of years as the line chart you made in Step 1.
c) Interpret your column chart. Does the evidence in the column chart roughly match what you saw in the line chart above?
Step 3: Make a scatter plot (20 pts)
a) Use vdem to grab democracy indicators along with GDP per capita (e_gdppc) and region codes (e_regionpol_6C) for all countries for one year of your choosing. Use select() to keep the variables you need and filter() to select your year. Be sure to rename variables where needed and use mutate() and case_match() to assign region names.
b) Use the data from part a) to build a scatter plot with ggplot2. Put GDP on the x-axis and one of the measures of democracy on the y-axis and color the points by region. Stretch the x-axis on a log scale and use the scales package to add a prefix and suffix to the x-axis numbers to indicate that they are dollar figures. Add appropriate labels and a viridis color map and add your favorite theme.
c) Next add a trend line with geom_smooth() preferably with a linear model (method = “lm”).
Step 4: Additional Tasks (20 pts)
a) Facet wrap your scatter plot by region.
b) Remove the facet_wrap() call and display the relationship for one region and use geom_text() to label your points.
c) Remove the text labels and make your scatter plot interactive using ggplotly(). Make sure that your tooltip includes the information that you want to display to the user.
Step 5: Interpretation (10 pts)
Interpret your results from your visualizations, including the scatter plot, line chart and column chart. Is there an obvious relationship between development and democracy? Do the data generally support Lipset’s theory?
Step 6: Rendering (10 pts)
Use a Quarto theme to improve the look of the HTML output. Press the render button to create your HTML document. If there are lots of messages or warnings coming out of your code chunks, consider suppressing them with the appropriate chunk options or the execute options in the YAML header. If you get any errors, go back and fix them and try again until the document renders.
Extra Credit (3 pts)
a) Use vdem to download time series data and gganimate to make an animated version of your scatter plot.
b) Interpret the plot. Does it tell an interesting story that the static plot did not?
Render your document to HTML, then zip your entire project folder and submit it to Blackboard. You can work locally or on Posit Cloud — either way, make sure the rendered HTML is included in the folder before zipping.
Before submitting, feel free to delete any of the callout boxes in this document. Doing so will make your final document more legible and signals that you have read the instructions. You are also welcome to choose a different Quarto theme or apply other styling options in the YAML header to personalize your document.