---
title: "Lab 5.1 - Nobel Laureates"
author: "Author Goes Here"
date: "Semester Goes Here"
output:
  pdf_document:
    latex_engine: xelatex
---

```{r load-packages, message=FALSE}
library(tidyverse) 
```

This analysis explores the data used by P. Aldhous in [this Buzzfeed article](https://www.buzzfeednews.com/article/peteraldhous/immigration-and-science). The article claims that one key factor in the US's leadership in science and technology is immigration because while most living Nobel laureates in the sciences are based in the US, many of them were born in other countries.

## The Dataset

🚧
Get the `nobel` dataset from the class dataset repository, save it in your shared `data` folder, and load it into this notebook.

🚧
Because there is no formal webpage for this dataset, study the dataset and give a short summary here on what it contains. Use inline code to report how many observations (rows) and how many variables (columns) are in the dataset. What does each observation represent? Please include this data dictionary (you don't need to modify it):

- `id`: ID number
- `firstname`: First name of laureate
- `surname`: Surname 
- `year`: Year prize won
- `category`: Category of prize
- `affiliation`: Affiliation of laureate
- `city`: City of laureate in prize year
- `country`: Country of laureate in prize year
- `born_date`: Birth date of laureate
- `died_date`: Death date of laureate
- `gender`: Gender of laureate
- `born_city`: City where laureate was born
- `born_country`: Country where laureate was born
- `born_country_code`: Code of country where laureate was born
- `died_city`: City where laureate died
- `died_country`: Country where laureate died
- `died_country_code`: Code of country where laureate died
- `overall_motivation`: Overall motivation for recognition
- `share`: Number of other winners award is shared with
- `motivation`: Motivation for recognition

In a few cases the name of the city/country changed after laureate was given (e.g. in 1975 Bosnia and Herzegovina was part of the Socialist Federal Republic of Yugoslavia). In these cases the variables below reflect a different name than their counterparts without the suffix `_original`.

- `born_country_original`: Original country where laureate was born
- `born_city_original`: Original city where laureate was born
- `died_country_original`: Original country where laureate died
- `died_city_original`: Original city where laureate died
- `city_original`: Original city where laureate lived at the time of winning the award
- `country_original`: Original country where laureate lived at the time of winning the award

## Cleansing the Data

🚧
Create a new data frame called `nobel_living` that includes only:

- laureates for whom country is available (i.e., it isn't `NA` -- remember to use the `is.na()` function)
- laureates who are people as opposed to organizations (organizations are denoted with `"org"` as their gender)
- laureates who are still alive (their died_date is `NA`)

Confirm that once you have filtered for these characteristics you are left with a data frame with 228 observations.

## Determining Where Nobel Laureates Lived

The Buzzfeed article claims that most living Nobel laureates were based in the US when they won their prizes. First, we'll create a new variable to identify whether the laureate was in the US when they won their prize.

We include a `mutate()` function that uses a functional variant of the classic "if" statement, called `if_else()`, to create this variable. The arguments to this new function, to be covered in more detail later in the course, are:

- the condition for which we're testing (e.g., is the country the USA?)
- the value to use if the condition is true (e.g., if `country` is equal to `"USA"`, it gives us `"USA"`)
- the value to use otherwise (e.g., if the country isn't `"USA"`, we get `"Other"`).

```{r eval=FALSE}
# Remove this example chunk after you have completed the exercise below.
  mutate(
    country_us = if_else(country == "USA", "USA", "Other")
  )
```

🚧
Add a code chunk that creates a data frame called `nobel_living_science` by combining the two transformations above into a pipeline: starting with the `nobel_living` data frame you made earlier, use `mutate()` with the `if_else` discussed above to create the a `country_us` variable; and then use `filter()` to limit the results to include only categories with values `%in%` "Physics", "Medicine", "Chemistry", "Economics".

🚧
Create a faceted bar plot, with horizontal bars, visualizing the relationship between the category of prize and whether the laureate was in the US when they won the Nobel prize. Interpret your visualization, and say a few words about whether the Buzzfeed headline is supported by the data.

- Your visualization should be faceted by category.
- For each facet you should have two bars, one for winners in the US and one for Other. 
![prizes by current country](images/nobel1.png) 

## Determining Where Nobel Laureates Were Born

🚧
Go back to the code chunk that created `nobel_living_science` and add a new variable called `born_country_us` that has the value `"USA"` if the laureate is born in the US, and `"Other"` otherwise. Do this by modifying your earlier code chunk; you won't add anything new here.

🚧
Remake your visualization and add a second variable: whether the laureate was born in the US or not. Your final visualization should contain a facet for each category, within each facet a bar for whether they won the award in the US or not, and within each bar whether they were born in the US or not. (Don't over-think this: you can do this by just adding another aesthetic mapping!) Based on your visualization, do the data appear to support Buzzfeed's claim? Explain your reasoning in 1-2 sentences.

![prizes by country of origin](images/nobel2.png)

The data show that very few Nobel prize winners who won in other countries emigrated there from the US. Conversely, however, the data show that many US prize winners were born in other countries, at least for fields other than Economics in which the majority of winners were not US-born.

## Determining Where Immigrant Nobel Laureates Were Born

🚧
Make a table for where immigrant Nobelists were born, using a single pipeline:

- filter for living STEM laureates who won their prize in the US, but were born outside of the US,
- then create a frequency table for their birth country, `born_country`,
- then sort the result in descending order of number of Nobelists for each country.

![country of origin of immigrant winners](images/nobel3.png)

## Recreating the Buzzfeed Visualizations [OPTIONAL]

The plots in the Buzzfeed article are called waffle plots. You can find the code used for making these plots in Buzzfeed's GitHub repo (yes, they have one!) [here](https://buzzfeednews.github.io/2017-01-immigration-and-science/). You're not expected to recreate them as part of your assignment, but you're welcome to do so for fun!
