Coding Examples

No Fear!

February 7, 2025

Let’s get going . . .


Let’s open up the Week 01 module on Posit Cloud…

And work through the examples there.

Example: Make a map!

Example: Make a map!


library(leaflet)
leaflet() %>% 
  addTiles() %>%   # Add default OpenStreetMap map tiles
  addMarkers(lat = 38.90243843683386, lng =  -77.0443814477152, 
             label = "Elliott School of International Affairs")
05:00

Plotting Democracy Over Time

Example: Plotting Democracy Over Time

# Load the packages
library(vdemlite)
library(ggplot2)

# Use vdemlite to extract democracy scores for France and INdia
dem_data <- fetchdem(indicators = "v2x_polyarchy",
                     countries = c("FRA", "IND"))

# And now we can plot the data
ggplot(dem_data, aes(y = v2x_polyarchy, x = year, color=country_name)) +
  geom_line() +
  theme_minimal() +
  xlab("Year") +
  ylab("Electoral Democracy Index") +
  ggtitle("Electoral Democracy, 1970-2022") +
  geom_hline(yintercept = .5, linetype = "dashed", color = "grey") +
   scale_color_manual(name="Country", values=c("#E69F00", "#56B4E9")) +
  ylim(0, 1)
05:00