Introduction

http://www.newamericaneconomy.org/wp-content/uploads/2017/02/nae-nh-report.pdf

DotPlot

# Import data
data <- read.csv("data/census2.csv")

# Load packages
library(ggplot2)
library(dplyr)

# Create a new variable for the number of immigrants and the percent of immigrants in total population
data <- 
  data %>%
  mutate(popImmigrant = popTotal - popNative,
         popImmigrant_percent = (popImmigrant / popTotal)*100) 

# Create box plots of city mpg by suv
ggplot(data, aes(x = popImmigrant_percent)) +
  geom_dotplot()

Colum chart

# Percent of immigrants in total population
data %>%
  filter(Year == 2016) %>%
  ggplot(aes(reorder(x = Town, popImmigrant_percent), y = popImmigrant_percent, fill = benchM)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(title = "Lakes Region Towns by Percent of Immigrants in Total Population",
       subtitle = "during 2012-2016",
       x = NULL,
       y = NULL,
       caption = "Data source: American Community Survey, 5 year estiamtes")


# Size of Immigrant population
data %>%
  filter(Year == 2016, !Town %in% c("New Hampshire", "United States")) %>%
  ggplot(aes(reorder(x = Town, popImmigrant), y = popImmigrant, fill = benchM)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(title = "Lakes Region Towns by Immigrant Population",
       subtitle = "during 2012-2016",
       x = NULL,
       y = NULL,
       caption = "Data source: American Community Survey, 5 year estimates")

Geofacet

library(geofacet)
# Create a grid, nh_grid for use of geofacet package
nameTown <- c("Sandwich", "Tamworth", "Freedom", "Hebron", "Ashland", "Holderness", "Center Harbor", "Moultonborough", "Tuftonboro", "Ossipee", "Effingham", "Alexandria", "Bristol", "Bridgewater", "New Hampton", "Meredith", "Laconia", "Gilford", "Wolfeboro", "Danbury", "Hill", "Sanbornton", "Belmont", "Alton", "Andover", "Franklin", "Tilton", "Gilmanton", "Northfield", "Barnstead")
code <- c("Sw", "Tw", "Fd", "Hb", "Al", "Hn", "Ch", "Mb", "Tb", "Op", "Eh", "Ad", "Bt", "Bw", "Nh", "Md", "Ln", "Gf", "Wb", "Db", "Hl", "Sb", "Bm", "At", "Av", "Fk", "Tt", "Gm", "Nf", "Bs")
row <- c(1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,5,5,5,5,6,6)
col <- c(6,7,9,1,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,1,2,4,6,8,2,3,4,7,4,8)
nh_grid <- data.frame(nameTown, code, row, col)

data_geo <- data
colnames(data_geo)[colnames(data_geo) == "Town"] <- "nameTown"

library(dplyr)
library(ggplot2)
library(tidyr)

# Plot multipler variables in a chart using gather function
data_geo %>%
  gather(var, value, c(4:(ncol(data_geo)-4),(ncol(data_geo)-1):ncol(data_geo))) %>%
  filter(var %in% c("popImmigrant"),
         Year == 2016) %>%
  ggplot(aes(x = var, y = value, fill = var)) + 
  geom_col() +
  facet_geo(~ nameTown, grid = nh_grid) +
  labs(title = "Immigration Population in 2012-2016",
       x = NULL,
       y = NULL,
       caption = "Data source: American Community Survey, 5-year estimate") +
  theme(axis.text.x=element_blank(), 
        axis.ticks.x=element_blank(),
        legend.position= c(0.15,0.05))


# Plot multipler variables in a chart using gather function
data_geo %>%
  gather(var, value, c(4:(ncol(data_geo)-4),(ncol(data_geo)-1):ncol(data_geo))) %>%
  filter(var %in% c("popImmigrant_percent"),
         Year == 2016) %>%
  ggplot(aes(x = var, y = value, fill = var)) + 
  geom_col() +
  facet_geo(~ nameTown, grid = nh_grid) +
  labs(title = "Percent of Immigration Population in 2012-2016",
       x = NULL,
       y = NULL,
       caption = "Data source: American Community Survey, 5-year estimate") +
  theme(axis.text.x=element_blank(), 
        axis.ticks.x=element_blank(),
        legend.position= c(0.15,0.05))

How does immigration pattern change during the last decade?

data %>%
  select(Town, popImmigrant_percent, Year, benchM) %>%
  mutate(Year = paste0("Yr",Year)) %>%
  spread(Year, popImmigrant_percent) %>%
  ggplot(aes(reorder(x = Town, Yr2016-Yr2011), y = Yr2016-Yr2011, fill = benchM)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(title = "Change in Percent of Immigrants in Total Population between 2007-11 and 2012-16",
       x = NULL,
       y = "Change in Percent")


data %>%
  filter(!Town %in% c("United States", "New Hampshire")) %>%
  select(Town, popImmigrant, Year) %>%
  mutate(Year = paste0("Yr",Year)) %>%
  spread(Year, popImmigrant) %>%
  ggplot(aes(reorder(x = Town, Yr2016-Yr2011), y = Yr2016-Yr2011, fill = Yr2016-Yr2011 > 0)) +
  geom_col(show.legend = FALSE) +
  coord_flip() +
  labs(title = "Change in Immigrant Population between 2007-11 and 2012-16",
       x = NULL,
       y = "Change in Population")