Your First Data Viz

Author

Insert Your Name

Published

August 26, 2024

Democracy over time

How has the level of democracy changed over time in specific countries? In this short project, we will use data from the Varieties of Democracy Project and data visualization tools to address this question.

Step 1: Update the YAML

The YAML is the section of the document where you set various settings. In this document, it is in lines 1-7.

  • Go up to the YAML and update your name in the author line.

Step 2: Load up packages

First, we need to “load” the packages we will use for this exercise. Here, we will use `ggplot2, which is a package for data visualization, and vdemlite, which is a package that loads up data from the Varieties of Democracy Dataset (this package is created by GWU’s Professor Emmanuel Teitelbaum).

We can “run” the code chunk in a number of different ways.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(vdemlite)

Step 3: Set up the data

fetchdem() is the main function in vdemlite that downloads the V-Dem data and loads it into R. Within fetchdem(), you can specify one or more V-Dem indexes or indicators along with the years and countries you are interested in. The function will download the data and return a tibble (dataframe) with the specified indexes and indicators.

Let’s grab the electoral democracy (“polyarchy”) score for Brazil, China, and South Africa between 1970 and 2020.

# Grab the polyarchy scores for the BRICS between 2000 and 2020
# save as a dataframe called "brics_dem"
brics_dem <- fetchdem(indicators = "v2x_polyarchy",
                         start_year = 1970, end_year = 2020,
                         countries = c("BRA", "CHN", "ZAF"))

Step 4: Check out the data

You can examine the dataset you created in tabular form in a few ways. You can examine the data using glimpse()

glimpse(brics_dem)
Rows: 153
Columns: 4
$ country_name    <chr> "South Africa", "South Africa", "South Africa", "South…
$ country_text_id <chr> "ZAF", "ZAF", "ZAF", "ZAF", "ZAF", "ZAF", "ZAF", "ZAF"…
$ year            <dbl> 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, …
$ v2x_polyarchy   <dbl> 0.165, 0.166, 0.166, 0.166, 0.165, 0.164, 0.164, 0.162…

Step 5: Visualize!

ggplot(brics_dem, aes(x = year, y = v2x_polyarchy, color = country_name)) +
  geom_line() +
  labs(title = "Democracy in Brazil, China, and South Africa",
       x = "Year",
       y = "Polyarchy Score") +
  scale_color_viridis_d(name = "Country", option = "inferno", end = .8) +
  theme_minimal()

Step 6: Your turn to try

First, choose your own countries and create a new dataset.

Update the code below with new country codes, which will create a new dataset object that I have set to be called myVdem.

The three country codes you use need to match. A full list of “alpha-3” country codes can be found here (https://www.iban.com/country-codes).

# Choose your own countries!  
# Update the code below
myVdem <- fetchdem(indicators = "v2x_polyarchy",
                         start_year = 1970, end_year = 2020,
                         countries = c("USA", "CHL", "ITA"))

Now, create your plot by running the code chunk below. Make sure you update the title.

ggplot(myVdem, aes(x = year, y = v2x_polyarchy, color = country_name)) +
  geom_line() +
  labs(title = "Democracy in ...",
       x = "Year",
       y = "Polyarchy Score") +
  scale_color_viridis_d(name = "Country", option = "inferno", end = .8) +
  theme_minimal()

Step 7: Write a short paragraph describing the patterns you see below

[Do NOT write your paragraph in a code chunk.The benefit of Quarto is that you can integrate code AND your writing in the same document.]

Step 8: Render the document

“Render” your document by clicking the Render button at the top of the window. This will generate an html document that combines the text with the executed code.

The html file should open in a new window.

You will also see and be able to open the document in the Files pane (in the lower right).

When we do assignments, we will always want to Render the document so that we can see your work.

Bonus

If you are finished, try the following:

  • Create a new code chunk below (using the button the top right or copy and pasting)
  • Select countries you want to examine
  • Change the date range to run from 1900 to 2020
  • Create a second code chunk to create another plot and insert the code to create the plot
  • What trends do you see? Write up a paragraph. Do NOT write your paragraph in a code chunk. The benefit of Quarto is that you can integrate code AND your writing in the same document.