---
title: "RMarkdown Example: UN Voting"
author: "kvlinden"
date: "Spring, 2022"
output: 
  html_document
---

```{r include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
```


## 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](https://rstudio-education.github.io/dsbox/).


## Interleaving Graphics and Code

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

### Images

![UN Logo](images/un.png){width="300px"}

### Code

This code loads the libraries required for the demo.

```{r warning = FALSE, message = FALSE}
library(tidyverse)
library(unvotes)
library(scales)
library(lubridate)
```

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

### Code Output

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

```{r}
un_votes %>% 
  distinct(country) %>% 
  count()
```

Data from: Erik Voeten [&ldquo;Data and Analyses of Voting in the UN General Assembly&rdquo;](https://papers.ssrn.com/sol3/papers.cfm?abstract_id=2111149), 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: `r count(distinct(un_votes, country))`. 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](https://datasciencebox.org/hello-world.html).

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.

```{r}
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
```

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

```{r}
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&rsquo;s on votes related to the Palestinian conflict.
