%% Note: fig-width option not working as of Quarto 1.4, try again in 1.5
flowchart LR
Client-->|Request|id1[(API)]
id1[(API)]-->|Response|Client
id1[(API)]-->Server
Server-->id1[(API)]
August 24, 2026
Thoughts? 😎 💭
unvotes)unvotes)%% Note: fig-width option not working as of Quarto 1.4, try again in 1.5
flowchart LR
Client-->|Request|id1[(API)]
id1[(API)]-->|Response|Client
id1[(API)]-->Server
Server-->id1[(API)]
vdemdatawbstats or WDIunvotesvdemdata Packagedplyr functions like filter() and select()vdem and what does it do?filter(), select(), mutate()filter() is used to select observations based on their valuesselect() is used to select variablesmutate() is used to create new variables or modifying existing onesfilter()== instead of >=<= and see what happens= versus === is used to assign values to variables, just like <-== is used to test if two values are equal to each otherfilter(year == 1990) will give you just the observations for 1990>= and <=>= is used to test if a value is greater than or equal to another value<= is used to test if a value is less than or equal to another valuefilter(year >= 1990) will give you the observations for 1990 and laterfilter(year <= 1990) will give you the observations for 1990 and earlierselect()v2x_libdem instead of v2x_polyarchymutate()polyarchypolyarchy squared?democracy <- vdem |> # download the V-Dem dataset
filter(year == 2015) |> # keep only observations from 2015
select( # select (and rename) these variables
country = country_name, # name before the = sign is new name
vdem_ctry_id = country_id, # name after the = sign is old name
year,
polyarchy = v2x_polyarchy
) |>
mutate(
polyarchy_dbl = polyarchy * 2 # create variable 2X polyarchy
)
glimpse(democracy) + addition- subtraction* multiplication/ division^ exponentiation (also **)vdemdata Example# Load packages
library(vdemdata) # to download V-Dem data
library(dplyr)
# Download the data
democracy <- vdem |> # download the V-Dem dataset
filter(year == 2015) |> # filter year, keep 2015
select( # select (and rename) these variables
country = country_name, # the name before the = sign is the new name
vdem_ctry_id = country_id, # the name after the = sign is the old name
year,
polyarchy = v2x_polyarchy,
gdp_pc = e_gdppc,
region = e_regionpol_6C
) |>
mutate(
region = case_match(region, # replace the values in region with country names
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
)
# View the data
glimpse(democracy)Use filter() to select years…
# Download the data
democracy <- vdem |>
filter(year == 2015) |> # keep 2015
select(
country = country_name,
vdem_ctry_id = country_id,
year,
polyarchy = v2x_polyarchy,
gdp_pc = e_gdppc,
region = e_regionpol_6C
) |>
mutate(
region = case_match(region,
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
)Use select() to choose variables…
# Download the data
democracy <- vdem |>
filter(year == 2015) |>
select( # select (and rename) these variables
country = country_name, # the name before the = sign is the new name
vdem_ctry_id = country_id, # the name after the = sign is the old name
year,
polyarchy = v2x_polyarchy,
gdp_pc = e_gdppc,
region = e_regionpol_6C
) |>
mutate(
region = case_match(region,
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
)Use mutate with case_match() to Recode Region….
# Download the data
democracy <- vdem |>
filter(year == 2015) |>
select(
country = country_name,
vdem_ctry_id = country_id,
year,
polyarchy = v2x_polyarchy,
gdp_pc = e_gdppc,
region = e_regionpol_6C
) |>
mutate(
region = case_match(region, # replace the values in region with country names
1 ~ "Eastern Europe",
2 ~ "Latin America",
3 ~ "Middle East",
4 ~ "Africa",
5 ~ "The West",
6 ~ "Asia")
# number on the left of the ~ is the V-Dem region code
# we are changing the number to the country name on the right
# of the equals sign
)library(ggplot2)
ggplot(democracy, aes(x = gdp_pc, y = polyarchy)) +
geom_point(aes(color = region)) +
geom_smooth(method = "lm", linewidth = 1) +
scale_x_log10(labels = scales::label_number(prefix = "$", suffix = "k")) +
labs(
x= "GDP per Capita",
y = "Polyarchy Score",
title = "Wealth and democracy in 2015",
caption = "Source: V-Dem Institute",
color = "Region"
) +
scale_color_viridis_d(option = "inferno", end = .8)