my_continent <- function(dat){dat %>% unique() %>% filter(!state_abbr %in% c("AK","HI","PR"))}
# Load States
state <- my_continent(us_states()) %>% mutate(statefp = parse_number(statefp))
# Load Builds
builds <- permits %>%
rename(state_abbr = "StateAbbr") %>%
rename(statefp = "state") %>%
rename(countyfp = "county") %>%
filter(variable == "Single Family") %>%
filter(year %in% c(2003, 2004, 2005, 2006, 2007, 2008))
builds <- my_continent(builds)
my_create <- function(state_str){
df <- us_counties() %>% unique() %>%
filter(state_abbr == state_str) %>%
mutate(countyfp = parse_number(countyfp))
df
}
my_join <- function(dat1, dat2){
df <- inner_join(dat1, dat2, by = "countyfp") %>%
select(countyfp, countyname, year, value, geometry) %>%
group_by(countyfp, year) %>%
mutate(total = sum(value)) %>%
mutate(total = as.numeric(total))
df
}
USA <- inner_join(state, builds) %>%
select(state_abbr, year, value, geometry) %>%
group_by(state_abbr, year) %>%
mutate(total = sum(value)) %>%
mutate(total = as.numeric(total))
CA <- my_join(my_create("CA"), builds)
ID <- my_join(my_create("ID"), builds)
You have been asked to support a story for the local paper (that has a web presence) that looks back on the housing collapse and the early effects of residential construction. You have data on residential building permits from 1980 through 2010 for each county in the United States. Your colleague that is writing the article would like a few maps and graphics that highlight the single family building permit patterns across your state as well as the patterns in the US.
Remember the big story is the collapse of new building permits at the initial stages of the mortgage crisis. Make sure your graphics highlight the collapse in a clear and honest manner.
my_plot <- function(dat){
ggplot(data = dat) +
geom_sf(aes(fill = total), color = "black") +
facet_wrap(~ year) +
theme_bw() +
scale_fill_gradient(low = "orange", high = "red", space = "Lab",
na.value = "grey50", guide = "colourbar", aesthetics = "fill") +
theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank(),
axis.title.y=element_blank(), axis.text.y=element_blank(), axis.ticks.y=element_blank())
}
my_plot(USA)
my_plot(ID)
my_plot(CA)
My plots show the housing market between the years 2006 to 2009.
I plotted for USA, ID and CA. I used functions to minimize the code.