# What is the average commute time in each state?
# Inspiration: http://overflow.solutions/demographic-data/national-data/state-level-analysis/what-is-the-average-commute-time-in-each-state/

# Asked myself - can I reproduce it?

# Install packages
library(ggplot2)
library(ggthemes)
library(dplyr)
library(choroplethr)
library(choroplethrMaps)
library(plotly)
library(RColorBrewer)
library(devtools)
library(mapproj)
library(maptools)
library(sp)
library(gridExtra)
library(leaflet)
library(rgdal)
library(fiftystater)

# Set directory
setwd("C:/DC/R/Cool datasets")

# Read data 
commute <- read.csv("Mean travel time to work (minutes), workers age 16 years by state.csv", head = TRUE)

# Map by state

states <- readOGR(dsn = "C:/DC/R/Cool datasets/Median income by county and state", 
                  layer = "cb_2016_us_state_500k", 
                  encoding = "UTF-8", verbose = FALSE)

# Merge data
# require(sp)! For spatial dataframe!
class(states)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
class(commute$Time) 
## [1] "numeric"
commute.df <- merge(states, commute, by.x = "NAME", by.y = "State")
class(commute.df)
## [1] "SpatialPolygonsDataFrame"
## attr(,"package")
## [1] "sp"
pal <- colorBin("Greens", c(16, 33), na.color = "#808080",
                alpha = FALSE, reverse = FALSE)

# Create a pop-up
state_popup <- paste0("<strong>State: </strong>", 
                      commute.df$NAME, 
                      "<br><strong>Mean travel time to work (minutes): </strong>", 
                      commute.df$Time)

commute.map <- leaflet(data = commute.df) %>%
  addPolygons(fillColor = ~pal(commute.df$Time), 
              popup = state_popup,
              fillOpacity = 0.9, 
              color = "#BDBDC3", 
              weight = 1) %>%
  addLegend("bottomright",
            pal = pal, values = commute.df$Time,
            title = "Mean travel time to work (minutes)",
            opacity = 1)
commute.map
# Create the same with "fiftystater"

data("fifty_states") # this line is optional due to lazy data loading

# tolower 
commute$statelower <- tolower(commute$State)

# map_id creates the aesthetic mapping to the state name column in your data
commute.map.ggplot <- ggplot(commute, aes(map_id = statelower)) + 
  # map points to the fifty_states shape data
  geom_map(aes(fill = commute$Time), map = fifty_states, colour = "white") + 
  expand_limits(x = fifty_states$long, y = fifty_states$lat) +
  coord_map() +
  scale_x_continuous(breaks = NULL) + 
  scale_y_continuous(breaks = NULL) +
  labs(x = "", y = "") +
  theme(legend.position = "bottom", 
        panel.background = element_blank()) +
  fifty_states_inset_boxes() +
  labs(title = "Mean travel time to work (minutes) by state \n(workers age 16 years)",
       subtitle = "Source: ACS") +
  theme(plot.title = element_text(hjust = 0.5)) + 
  labs(fill = "Mean travel time to work (minutes)") +
  theme(axis.line.x = element_line(size = .5, colour = "black"),
        axis.title = element_text(size = 14),
        legend.direction = "horizontal",
        legend.box = "vertical",
        legend.key.size = unit(0.7, "cm"),
        legend.text = element_text(size = 10),
        text = element_text(family = "OfficinaSanITC-Book"),
        plot.title = element_text(family = "OfficinaSanITC-Book")) 
commute.map.ggplot