Skip to content

Instantly share code, notes, and snippets.

@josep2
Created May 18, 2016 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josep2/1591070dba4552ff95913cb1067728de to your computer and use it in GitHub Desktop.
Save josep2/1591070dba4552ff95913cb1067728de to your computer and use it in GitHub Desktop.
## For data sources I used API's from other scripts not included here.
library(dplyr)
library(ggplot2)
library(ggstance)
library(ggalt)
library(scales)
library(grid)
library(RColorBrewer)
library(extrafont)
my_subtitle = "Data: Homeland Infrastructure Foundation"
my_caption = "@jowanza"
## Line chart for over time
timeframe <- filter(base_mobile_data, !is.na(YEARBLT))
timeframes <- group_by(timeframe,YEARBLT) %>%
summarise(total = n()) %>%
arrange(desc(total))
ggplot(timeframes, aes(YEARBLT, total)) +
geom_line(color = "purple", size = 1, alpha=.75)+
my_theme()+
labs(y = "# Of Mobile Home Parks", x="Year Built", title = "Mobile Home Parks Built Over Time (Limited Data) ",
caption=my_caption, subtitle = my_subtitle)
ggsave("blog_vis1.png", dpi=300, width=10, height=9)
### States
rank_states_count <- group_by(base_mobile_data,STATE) %>%
summarise(total = n()) %>%
arrange(desc(total))
rank_states <- top_n(rank_states_count, 10, total )
ggplot(rank_states, aes(x = total, y = reorder(STATE, total))) +
geom_barh(stat="identity", alpha=.5) +
geom_stateface(aes(y=STATE, x = 200, label=as.character(STATE)), color="#003399", size=8, alpha=1) +
geom_text(aes(x = total - 250, y = STATE, label = total), color="white",
size=3.5, family="Hack")+
my_theme() +
labs (y= "State", x = "# Mobile Home Parks", title="Number of Mobile Home Parks By State", subtitle = my_subtitle, caption = my_caption)+
scale_x_continuous(breaks=seq(0,6000, 1000))
ggsave("blog_vis2.png", dpi=300, width=10, height=9)
## number of Units per mobile home park
units_data = filter(base_mobile_data, UNITS > 0)
unit_mean = mean(units_data$UNITS)
unit_median = median(units_data$UNITS)
ggplot(units_data, aes(x=UNITS))+
geom_histogram(binwidth = 5, alpha=.5, fill= "dodger blue")+
my_theme()+
labs(x= "Units", y = 'Count', caption = my_caption, title="Distribution of Units by Mobile Home Park", subtitle = my_subtitle )+
scale_x_continuous(breaks= seq(0,1600, 200))+
geom_vline(xintercept = unit_mean, color = "orange", alpha=.5)+
geom_vline(xintercept = unit_median, color = "green", alpha=.5)+
annotate("text", label = "mean:\n123", x = unit_mean + 40, y = 250, size = 3, colour = "black", family= "Hack")+
annotate("text", label = "median:\n61", x = 5, y = 250, size = 3, colour = "black", family="Hack")
ggsave("blog_vis3.png", dpi=300, width=10, height=9)
## Zips
zip_count <- group_by(base_mobile_data,ZIP) %>%
summarise(total = n()) %>%
arrange(desc(total))
zip_data = inner_join(zip_count, base_mobile_data, by = "ZIP")
zip_data = distinct(zip_data, ZIP) %>%
select(ZIP, total, STATE)
zip_data = inner_join(zip_data, income, by="ZIP")
zip_data$percap = zip_data$total/zip_data$Pop
zip_data = filter(zip_data, Pop > 1000)
zip_data = top_n(zip_data, 10, percap)
ggplot(zip_data, aes(x = percap, y = reorder(ZIP, percap))) +
geom_barh(stat="identity", alpha=.5)+
geom_stateface(aes(y=reorder(ZIP, percap), x = .0025, label=as.character(STATE)), color="#003399", size=8, alpha=1) +
my_theme() +
labs (y= "Zip code", x = "# Mobile Home Parks", title="Top 10 Per Capita Mobile Homes (population > 1000)", subtitle = my_subtitle, caption = my_caption)+
scale_x_continuous(labels=scales::percent)
ggsave("blog_vis4.png", dpi=300, width=10, height=9)
## Counties
county_count <- group_by(base_mobile_data, COUNTY) %>%
summarise(total = n()) %>%
arrange(desc(total))
county_data = inner_join(county_count, base_mobile_data, by = "COUNTY")
county_data = distinct(county_data, COUNTY) %>%
select(COUNTY, total, STATE)
county_data = top_n(county_data, 10, total)
ggplot(county_data, aes(x = total, y = reorder(COUNTY, total))) +
geom_barh(stat="identity", alpha=.5)+
geom_stateface(aes(y=reorder(COUNTY, total), x = 20, label=as.character(STATE)), color="#003399", size=8, alpha=1) +
geom_text(aes(x = 35 , y = reorder(COUNTY, total), label = STATE), color="white",
size=3.5, family="Hack")+
geom_text(aes(x = total - 20 , y = reorder(COUNTY, total), label = total), color="blue",
size=3.5, family="Hack")+
my_theme() +
labs (y= "County", x = "# Mobile Home Parks", title="Top 10 Mobile Home Parks By County", subtitle = my_subtitle, caption = my_caption)
ggsave("blog_vis5.png", dpi=300, width=10, height=9)
## Cities
city_count <- group_by(base_mobile_data, CITY) %>%
summarise(total = n()) %>%
arrange(desc(total))
city_data = inner_join(city_count, base_mobile_data, by = "CITY")
city_data = distinct(city_data, CITY) %>%
select(CITY, total, STATE)
city_data = top_n(city_data, 10, total)
ggplot(city_data, aes(x = total, y = reorder(CITY, total))) +
geom_barh(stat="identity", alpha=.5)+
geom_stateface(aes(y=reorder(CITY, total), x = 10, label=as.character(STATE)), color="#003399", size=8, alpha=1) +
geom_text(aes(x = 20 , y = reorder(CITY, total), label = STATE), color="white",
size=3.5, family="Hack")+
geom_text(aes(x = total - 10 , y = reorder(CITY, total), label = total), color="blue",
size=3.5, family="Hack")+
my_theme() +
labs (y= "City", x = "# Mobile Home Parks", title="Top 10 Mobile Home Parks By City", subtitle = my_subtitle, caption = my_caption)
ggsave("blog_vis6.png", dpi=300, width=10, height=9)
## County Median Income by # Of Mobile Home Parks (Interactive)
incomedata <- inner_join(zip_count, income, by="ZIP")
ggplot(incomedata, aes(x=Median, y=total, size=5))+
geom_jitter(alpha=.5, color="purple")+
stat_smooth(size=1, color = "black", alpha=.5)+
my_theme()+
labs (y= "# Mobile Homes", x = "Median Income", title="# Of Moile Homes By Zip Code Median Income", subtitle = my_subtitle, caption = my_caption)+
scale_x_continuous(labels=scales::dollar)
ggsave("blog_vis.png", dpi=300, width=10, height=9)
## Themes
my_theme <- function() {
# Generate the colors for the chart procedurally with RColorBrewer
palette <- brewer.pal("Greys", n=9)
color.background = palette[2]
color.grid.major = palette[3]
color.axis.text = palette[6]
color.axis.title = palette[7]
color.title = palette[9]
# Begin construction of chart
theme_bw(base_size=9) +
# Set the entire chart region to a light gray color
theme(panel.background=element_rect(fill=color.background, color=color.background)) +
theme(plot.background=element_rect(fill=color.background, color=color.background)) +
theme(panel.border=element_rect(color=color.background)) +
# Format the grid
theme(panel.grid.major=element_line(color=color.grid.major,size=.25)) +
theme(panel.grid.minor=element_blank()) +
theme(axis.ticks=element_blank()) +
# Format the legend, but hide by default
theme(legend.position="none") +
theme(legend.background = element_rect(fill=color.background)) +
theme(legend.text = element_text(size=7,color=color.axis.title)) +
# Set title and axis labels, and format these and tick marks
theme(plot.title=element_text(color=color.title, size=15, vjust=1.25,face="bold", family="Hack")) +
theme(axis.text.x=element_text(size=9,color=color.axis.text,face="bold")) +
theme(axis.text.y=element_text(size=7,color=color.axis.text,face="bold")) +
theme(axis.title.x=element_text(size=10,color=color.axis.title, vjust=0, face="bold", family= "Hack")) +
theme(axis.title.y=element_text(size=10,color=color.axis.title, vjust=1.25, face="bold", family = "Hack")) +
theme(strip.text.y = element_text(size = 8, colour = "black", face="bold", family = "Hack")) +
theme(strip.background = element_rect(fill=color.background, color=color.background, size=1)) +
theme(plot.subtitle = element_text(color=color.title, size=10, vjust=1.25,face="italic", family="Hack"))+
theme(plot.caption = element_text(color=color.title, size=7, vjust=1.25,face="bold", family="Hack"))+
# Plot margins
theme(plot.margin = unit(c(0.35, 0.2, 0.3, 0.35), "cm"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment