RMarkdown

RMarkdown is a simple formatting syntax for authoring documents that interleave text, images, and code. All assignments will be formatted using RMarkdown.

Distinguish between the RMarkdown source file and the HTML output file. Press the Knit button in RStudio to generate the latter from the former.

This document demos the UN Votes example from DSBox.

Interleaving Graphics and Code

RMarkdown documents can interleave text markup, images, programming code and output.

Images

UN Logo
UN Logo

Code

This code loads the libraries required for the demo.

library(tidyverse)
library(unvotes)
library(scales)
library(lubridate)

The warning and message arguments suppress the library function’s output in the knitted (i.e., rendered) document.

Code Output

This code displays the raw data in the UN Votes dataset.

un_votes %>% 
  distinct(country) %>% 
  count()
## # A tibble: 1 × 1
##       n
##   <int>
## 1   200

Data from: Erik Voeten “Data and Analyses of Voting in the UN General Assembly”, Routledge Handbook of International Organization.

Inline Code

A simpler, through perhaps more rare form of code is inline code, which inserts code output directly in a text. For example, here is the number of unique countries computing inline rather than in a code chunk: 200. Note that this code is not a chunk that can be run in editor; the document must be knit to see the output.

Plots

You can also embed plots. For example, here is the plot inspired by this DSBox example.

We start, by building a specialized dataset from the raw UN votes dataset that gives us the voting records of the United Kingdom and the United States.

un_uk_us <- un_votes %>%
  mutate(
    country = case_when(
      country == "United Kingdom" ~ "UK",
      country == "United States" ~ "US",
      TRUE ~ country
    )
  ) %>%
  inner_join(un_roll_calls, by = "rcid") %>%
  inner_join(un_roll_call_issues, by = "rcid") %>%
  mutate(
    issue = fct_recode(issue,  "Nuclear weapons and material" = "Nuclear weapons and nuclear material")
  ) %>%
  filter(country %in% c("UK", "US")) %>%
  mutate(year = year(date)) %>%
  group_by(country, year, issue) %>%
  summarize(percent_yes = mean(vote == "yes"))
un_uk_us
## # A tibble: 808 × 4
## # Groups:   country, year [146]
##    country  year issue                        percent_yes
##    <chr>   <dbl> <fct>                              <dbl>
##  1 UK       1946 Colonialism                        0.8  
##  2 UK       1946 Economic development               0.7  
##  3 UK       1946 Human rights                       0    
##  4 UK       1947 Colonialism                        0.333
##  5 UK       1947 Economic development               0.5  
##  6 UK       1947 Palestinian conflict               0.571
##  7 UK       1948 Colonialism                        0.417
##  8 UK       1948 Arms control and disarmament       0    
##  9 UK       1948 Economic development               0.375
## 10 UK       1948 Human rights                       0.167
## # … with 798 more rows

And now, we create a plot comparing these voting records.

un_uk_us %>%
  ggplot(aes(x = year, y = percent_yes, color = country)) +
  geom_point(alpha = 0.4) +
  geom_smooth(method = "loess", se = FALSE) +
  facet_wrap(~issue) +
  labs(
    title = "Percentage of 'Yes' votes in the UN General Assembly", #<<
    subtitle = "1946 to 2015", #<<
    y = "% Yes", x = "Year", color = "Country" #<<
  )

As you can see, the UK and US generally have similar voting records, but, interestingly, the records diverged in the early 1980’s on votes related to the Palestinian conflict.