Mini project 2: Refugees in the United States

Due by 11:59 PM on Friday, December 7, 2018


The United States has resettled more than 600,000 refugees from 60 different countries since 2006.

In this mini project, you will use R, ggplot, and Illustrator, Inkscape, or Gravit Designer to explore where these refugees have come from.


Here’s what you need to do:

  1. Create a new RStudio project and place it on your computer somewhere. Open that new folder in Windows File Explorer or macOS Finder (however you navigate around the files on your computer), and create two subfolders there named data and output.

  2. Download the Department of Homeland Security’s annual count of people granted refugee status between 2006-2015:

    DHS refugees, 2006-2015

    Place this in the data subfolder you created in step 1.As always, you’ll probably need to right click on this link and choose “Save link as…”, since your browser will want to display it as text. This data was originally uploaded by the Department of Homeland Security to Kaggle, and is provided with a public domain license.

  3. Create a new R Markdown file and save it in your project. In RStudio go to File > New File > R Markdown…, choose the default options, and delete all the placeholder text in the new file except for the metadata at the top, which is between --- and ---.

  4. Verify that your project folder is structured like this:

  5. Clean the data using the code I’ve given you below.

  6. Summarize the data somehow. There is data for 60 countries over 10 years, so you’ll probably need to aggregate or reshape the data somehow (unless you do a 60-country sparkline). I’ve included some examples down below.

  7. Create an appropriate time-based visualization based on the data. I’ve shown a few different ways to summarize the data so that it’s plottable down below. Don’t just calculate overall averages or totals per country—the visualization needs to deal with change over time. Do as much polishing and refining in R—make adjustments to the colors, scales, labels, grid lines, and even fonts, etc.

  8. Save the figure as a PDF.Use ggsave(plot_name, filename = "output/blah.pdf", width = XX, height = XX)

  9. Refine and polish the saved PDF in Illustrator or Inkscape or Gravit Designer, adding annotations, changing colors, and otherwise enhancing it.

  10. Export the polished image as a PDF and a PNG file.

  11. Write a memo (no word limit) explaining your process. I’m specifically looking for the following:
    • What story are you telling with your graphic?
    • How did you apply the principles of CRAP?
    • How did you apply Alberto Cairo’s five qualities of great visualizations?
  12. Upload the following outputs to Learning Suite:
    • A PDF or Word file of your memo with your final code, intermediate graphic (the one you create in R), and final graphic (the one you enhance) in it.Remember to use ![Caption](path/to/figure/here) to place external images in Markdown.

    • A standalone PNG version of your graphic.You’ll export this from Illustrator or Inkscape

    • A standalone PDF version of your graphic.You’ll export this from Illustrator or Inkscape

You will be graded based on how you use R and ggplot, how well you apply the principles of CRAP and The Truthful Art, and how appropriate the graph is for the data and the story you’re telling. I will use this rubric to give comments the final product—the actual grade will be a check or check plus. Example rubric for redesign 2

For this assignment, I am less concerned with the code (that’s why I gave most of it to you), and more concerned with the design. Choose good colors based on palettes listed in the reference list. Choose good, clean fonts. Use the heck out of theme(). Add informative design elements in Illustrator/Inkscape/Gravit Designer. Make it look beautiful and CRAPpy.

The assignment is due by 11:59 PM on Friday, December 7.

Please seek out help when you need it! You know enough R (and have enough examples of code from class and your readings) to be able to do this. Your project has to be turned in individually, and your visualization should be your own (i.e. if you work with others, don’t all turn in the same graph), but you should work with others! Meet with me for help too—I’m here to help!

You can do this, and you’ll feel like a true dataviz witch/wizard when you’re done.

Data cleaning code

The data isn’t perfectly clean and tidy, but it’s real world data, so this is normal. Because the emphasis for this assignment is on design, not code, I’ve provided code to help you clean up the data.

These are the main issues with the data:

refugees_raw <- read_csv("data/refugee_status.csv", na = c("-", "X", "D")) 
non_countries <- c("Africa", "Asia", "Europe", "North America", "Oceania", 
                   "South America", "Unknown", "Other", "Total")

refugees_clean <- refugees_raw %>%
  # Make this column name easier to work with
  rename(origin_country = `Continent/Country of Nationality`) %>%
  # Get rid of non-countries
  filter(!(origin_country %in% non_countries)) %>%
  # Convert country names to ISO3 codes
  mutate(iso3 = countrycode(origin_country, "", "iso3c",
                            custom_match = c("Korea, North" = "PRK"))) %>%
  # Convert ISO3 codes to country names, regions, and continents
  mutate(origin_country = countrycode(iso3, "iso3c", ""),
         origin_region = countrycode(iso3, "iso3c", "region"),
         origin_continent = countrycode(iso3, "iso3c", "continent")) %>%
  # Make this data tidy
  gather(year, number, -origin_country, -iso3, -origin_region, -origin_continent) %>%
  # Make sure the year column is numeric + make an actual date column for years
  mutate(year = as.numeric(year),
         year_date = ymd(paste0(year, "-01-01")))

Data to possibly use in your plot

Here are some possible summaries of the data you might use…

Country totals over time

This is just the refugees_clean data frame I gave you. You’ll want to filter it and select specific countries, though—you won’t really be able to plot 60 countries all at once unless you use sparklines.

origin_country iso3 origin_region origin_continent year number year_date
Afghanistan AFG Southern Asia Asia 2006 651 2006-01-01
Angola AGO Middle Africa Africa 2006 13 2006-01-01
Armenia ARM Western Asia Asia 2006 87 2006-01-01
Azerbaijan AZE Western Asia Asia 2006 77 2006-01-01
Belarus BLR Eastern Europe Europe 2006 350 2006-01-01
Bhutan BTN Southern Asia Asia 2006 3 2006-01-01

Cumulative country totals over time

Note the cumsum() function—it calculates the cumulative sum of a column.

refugees_countries_cumulative <- refugees_clean %>%
  arrange(year_date) %>%
  group_by(origin_country) %>%
  mutate(cumulative_total = cumsum(number))
origin_country iso3 origin_region origin_continent year number year_date cumulative_total
Afghanistan AFG Southern Asia Asia 2006 651 2006-01-01 651
Afghanistan AFG Southern Asia Asia 2007 441 2007-01-01 1092
Afghanistan AFG Southern Asia Asia 2008 576 2008-01-01 1668
Afghanistan AFG Southern Asia Asia 2009 349 2009-01-01 2017
Afghanistan AFG Southern Asia Asia 2010 515 2010-01-01 2532
Afghanistan AFG Southern Asia Asia 2011 428 2011-01-01 2960

Continent totals over time

Note the na.rm = TRUE argument in sum(). This makes R ignore any missing data when calculating the total. Without it, if R finds a missing value in the column, it will mark the final sum as NA too, which we don’t want.

refugees_continents <- refugees_clean %>%
  group_by(origin_continent, year_date) %>%
  summarize(total = sum(number, na.rm = TRUE))
origin_continent year_date total
Africa 2006-01-01 18116
Africa 2007-01-01 17473
Africa 2008-01-01 8931
Africa 2009-01-01 9664
Africa 2010-01-01 13303
Africa 2011-01-01 7677

Cumulative continent totals over time

Note that there are two group_by() functions here. First we get the total number of refugees per continent per year, then we group by continent only to get the cumulative sum of refugees across continents.

refugees_continents_cumulative <- refugees_clean %>%
  group_by(origin_continent, year_date) %>%
  summarize(total = sum(number, na.rm = TRUE)) %>%
  arrange(year_date) %>%
  group_by(origin_continent) %>%
  mutate(cumulative_total = cumsum(total))
origin_continent year_date total cumulative_total
Africa 2006-01-01 18116 18116
Africa 2007-01-01 17473 35589
Africa 2008-01-01 8931 44520
Africa 2009-01-01 9664 54184
Africa 2010-01-01 13303 67487
Africa 2011-01-01 7677 75164

Visualization ideas

You can redesign one of these ugly, less-than-helpful graphs, or create a brand new visualization (like a map!).

Or be super brave and make a map!