library(pacman)
p_load("tidyverse", "broom", "coefplot", "cowplot",
                 "gapminder", "GGally", "ggrepel", "ggridges", "gridExtra",
                 "here", "interplot", "margins", "maps", "mapproj",
                 "mapdata", "MASS", "quantreg", "rlang", "scales",
                 "survey", "srvyr", "viridis", "viridisLite", "devtools", "socviz", "statebins")
library(readxl)
Degree_Percent <- read_excel("GeoFRED_Bachelor's_Degree_or_Higher_by_State_Percent.xls", 
    skip = 1)
View(Degree_Percent)
library(maps)
us_states <- map_data("state")
head(us_states)
##        long      lat group order  region subregion
## 1 -87.46201 30.38968     1     1 alabama      <NA>
## 2 -87.48493 30.37249     1     2 alabama      <NA>
## 3 -87.52503 30.37249     1     3 alabama      <NA>
## 4 -87.53076 30.33239     1     4 alabama      <NA>
## 5 -87.57087 30.32665     1     5 alabama      <NA>
## 6 -87.58806 30.32665     1     6 alabama      <NA>
p <- ggplot(data = us_states,
            mapping = aes(x = long, y = lat,
                          group = group))
p + geom_polygon(fill = "white", color = "black")

p <- ggplot(data = us_states,
            aes(x = long, y = lat,
                group = group, fill = region))
p + geom_polygon(color = "gray90", size = 0.1) + guides(fill = FALSE)

p <- ggplot(data = us_states,
            mapping = aes(x = long, y = lat,
                          group = group, fill = region))
p + geom_polygon(color = "gray90", size = 0.1) +
    coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
    guides(fill = FALSE)

Degree_Percent$region <- tolower(Degree_Percent$`Region Name`)
us_states_Degree_Percent<- left_join(us_states, Degree_Percent, by = "region")
p <- ggplot(data = us_states_Degree_Percent,
            aes(x = long, y = lat,
                group = group, fill = region))
p + geom_polygon(color = "gray90", size = 0.1) +
    coord_map(projection = "albers", lat0 = 39, lat1 = 45) 

p0 <- ggplot(data = us_states_Degree_Percent,
             mapping = aes(x = long, y = lat,
                           group = group, fill = region))

p1 <- p0 + geom_polygon(color = "gray90", size = 0.1) +
    coord_map(projection = "albers", lat0 = 39, lat1 = 45) 

p1 

names(us_states_Degree_Percent)[names(us_states_Degree_Percent) == "2018"] <- "value"
names(us_states_Degree_Percent)[names(us_states_Degree_Percent) == "Region Name"] <- "State"
#View(us_states_Degree_Percent)

library(statebins)
statebins_continuous(state_data = us_states_Degree_Percent , state_col = "State",
                     text_color = "white", value_col = "value",
                     brewer_pal="Reds", font_size = 3,
                     legend_title="us_states_Degree_Percent")
## Warning in validate_states(state_data, state_col, merge.x): Removing
## duplicate state rows
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.

library(statebins)
statebins(state_data = us_states_Degree_Percent, state_col = "State",
                     text_color = "white", value_col = "value",
                     brewer_pal="Blues", font_size = 3,
                     legend_title="2018 Bachelor's Degree or Higher by State (percent)")
## Warning in validate_states(state_data, state_col, merge.x): Removing
## duplicate state rows
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.

statebins(state_data = us_states_Degree_Percent,          
          state_col = "State", value_col = "value",
          text_color = "white", breaks = 7,
          labels = c("<=27.10","<=29.50","<=31.50","34","<38.70","<60.40","No Data Available"),
          brewer_pal="Reds", font_size = 3, legend_title="2018 Bachelor's Degree or Higher by State (percent)")
## Warning in validate_states(state_data, state_col, merge.x): Removing
## duplicate state rows
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.

library(readxl)
GeoFRED_New_Private_Housing_Units_Authorized_by_Building_Permits_by_State_Change_Units <- Private_Housing <- read_excel("GeoFRED_New_Private_Housing_Units_Authorized_by_Building_Permits_by_State_Change_Units.xls", 
    skip = 1)

View(Private_Housing)
Private_Housing$region <- tolower(Private_Housing$`Region Name`)
us_states_Private_Housing<- left_join(us_states, Private_Housing, by = "region")
names(us_states_Private_Housing)[names(us_states_Private_Housing) == "2019 October"] <- "value"
names(us_states_Private_Housing)[names(us_states_Private_Housing) == "Region Name"] <- "State"
View(us_states_Private_Housing)


library(statebins)
statebins_continuous(state_data = us_states_Private_Housing , state_col = "State",
                     text_color = "white", value_col = "value",
                     brewer_pal="Reds", font_size = 3,
                     legend_title="Percent Private Housing")
## Warning in validate_states(state_data, state_col, merge.x): Removing
## duplicate state rows
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.

statebins(state_data = us_states_Private_Housing,          
          state_col = "State", value_col = "value",
          text_color = "white", breaks = 5,
          labels = c("<=2000","<=1500","<1000","<=500", "<=0"),
          brewer_pal="Blues", font_size = 3, legend_title="Percent Private Housing")
## Warning in validate_states(state_data, state_col, merge.x): Removing
## duplicate state rows
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.

library(statebins)
statebins(state_data = us_states_Private_Housing, state_col = "State",
                     text_color = "white", value_col = "value",
                     brewer_pal="Reds", font_size = 3,
                     legend_title="Percent Private Housing")
## Warning in validate_states(state_data, state_col, merge.x): Removing
## duplicate state rows
## Warning: `show_guide` has been deprecated. Please use `show.legend`
## instead.