# 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,
         popImmigrantHL = ifelse(popImmigrant_percent >= mean(popImmigrant_percent), 
                              "equal or above ave", "below ave")) 

# Count number of towns of above and below average percent of immigrant population 
data %>%
  count(popImmigrantHL)
## # A tibble: 2 x 2
##   popImmigrantHL         n
##   <chr>              <int>
## 1 below ave             37
## 2 equal or above ave    27

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")

Compare towns of above and below average percent of immigrant population

# Compare median income
data %>%
  filter(Year == 2016) %>%
  ggplot(aes(x = medianIncome, fill = popImmigrantHL)) + 
  geom_density(alpha = 0.3)


# Compare education
data %>%
  filter(Year == 2016) %>%
  ggplot(aes(x = popBA_percent, fill = popImmigrantHL)) + 
  geom_density(alpha = 0.3)


# Compare poverty rate
data %>%
  filter(Year == 2016) %>%
  ggplot(aes(x = popPov_percent, fill = popImmigrantHL)) + 
  geom_density(alpha = 0.3)