Skip to content

Instantly share code, notes, and snippets.

@ggada
Created May 24, 2017 04:42
Show Gist options
  • Save ggada/39c56af71206f5a03abc0fd69037de86 to your computer and use it in GitHub Desktop.
Save ggada/39c56af71206f5a03abc0fd69037de86 to your computer and use it in GitHub Desktop.
R code for visualization report - INFX 561
library(devtools)
install_github("mikeasilva/blsAPI")
install.packages("data.table")
install.packages("wbstats")
library(wbstats)
library(data.table)
browseVignettes("data.table")
myDT <- data.table(
wb(indicator = c("SP.POP.TOTL"), mrv = 1)
)
energy <- data.table(wb(indicator = c("EG.ELC.RNEW.ZS"), mrv = 1))
getwd()
setwd("C:/Users/gaura/Documents/GitHub/vis-des/trump-ex-credit/")
wealth <- readxl::read_excel("wealth.xlsx")
library(ggplot2)
ggplot(wealth, mapping = aes(start, worth)) + geom_bar(stat="identity") + geom_text(aes(label = wealth$name, size = wealth$term), check_overlap = TRUE, angle = 90)
ggplot(wealth, mapping = aes(start, worth)) + geom_bar(stat="identity") +
geom_text(aes(label = wealth$name), angle = 90, nudge_y = -2000) +
scale_y_continuous(limits = c(-2000, 4000)) +
coord_flip(180)
library(data.table)
cw.area <- fread('https://download.bls.gov/pub/time.series/cw/cw.area')
cw.data.0.Current <- fread('https://download.bls.gov/pub/time.series/cw/cw.data.0.Current')
cw.data.10.OtherWest <- fread('https://download.bls.gov/pub/time.series/cw/cw.data.10.OtherWest')
cw.data.1.AllItems <- fread('https://download.bls.gov/pub/time.series/cw/cw.data.1.AllItems')
cw.data.12.USHousing <- fread('https://download.bls.gov/pub/time.series/cw/cw.data.12.USHousing')
cw.series <- fread('https://download.bls.gov/pub/time.series/cw/cw.series')
library(dplyr)
cw.joined <- cw.series %>%
left_join(cw.data.0.Current, by = "series_id") %>%
left_join(cw.area, c("area_code" = "V1"))
cw.joined %>%
filter(grepl("Seattle", V2)) %>%
filter(grepl("Rent", series_title)) %>%
View()
cw.joined[,grepl()]
# ===
cities <- read.csv("GitHub/vis-des/final-report-draft/Metro_MedianRentalPrice_Studio.csv")
wages <- readxl::read_excel("GitHub/vis-des/final-report-draft/aMSA_M2016_dl.xlsx")
rent_wages <- cities %>%
inner_join(wages)
class(cities$AREA)
wages$AREA <- as.integer(wages$AREA)
rent_wages
occ_rw <- filter(rent_wages, OCC_CODE %in% c('11-3021',
'15-1134',
'15-1132',
'25-0000',
'27-1024',
'27-3042'))
occ_rw$A_MEDIAN <- as.integer(occ_rw$A_MEDIAN)
occ_rw$monthly_median_wage <- occ_rw$A_MEDIAN/12
occ_rw$RegionName <- as.character(occ_rw$RegionName)
d1 <- cbind(occ_rw$RegionName, occ_rw$mthly_rent, occ_rw$OCC_TITLE, occ_rw$TOT_EMP, occ_rw$monthly_median_wage, occ_rw$monthly_food, occ_rw$median_sq_ft)
colnames(d1) <- c("city", "mthly_rent", "occ_title", "total_employment", "median_wage", "monthly_food", "median_sq_ft")
write.csv(d1, "GitHub/vis-des/final-report-draft/out.csv")
data <- readxl::read_excel("GitHub/vis-des/final-report-draft/data.xlsx")
data <- read.csv("GitHub/vis-des/final-report-draft/out.csv")
require(ggplot2)
data <- data %>%
arrange(occ_title, desc(yr_to_own))
cc <- scales::seq_gradient_pal("blue", "red", "Lab")(seq(0,1,length.out=5))
ggplot(data=data) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + facet_grid( occ_title ~ .)+ theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) + coord_flip() + aes(fill = as.factor(median_wage))
filter(data, grepl("Seattle", city))
ggplot(data=(filter(data, grepl("Manager", occ_title)))) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own, fill = median_wage)) + coord_flip()
mgr <- filter(data, grepl("Manager", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(10000,15000,1000)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(5)
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab("City") + ylab("Years to own a 1000 sq. ft. apartment") + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Web", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(4000,9000,1000)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab("City") + ylab("Years to own a 1000 sq. ft. apartment") + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Education", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(3500,5000,500)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab("City") + ylab("Years to own a 1000 sq. ft. apartment") + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Graphic", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(3500,5500,500)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab("City") + ylab("Years to own a 1000 sq. ft. apartment") + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Graphic", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(3500,5500,500)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab("City") + ylab("Years to own a 1000 sq. ft. apartment") + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Technical", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(4500,7500,500)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab(NULL) + ylab(NULL) + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Software", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(6500,12000,1000)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab(NULL) + ylab(NULL) + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
#---
mgr <- filter(data, grepl("Web", occ_title))
ct <- cut(mgr$median_wage, breaks = c(seq(4500,8500,1000)), dig.lab = 5)
col <- colorRampPalette(c("#b3e0a6", "#24693d"))(nlevels(ct))
ggplot(data=mgr) + geom_bar(stat="identity", aes(x=reorder(city, -yr_to_own), y=yr_to_own)) + coord_flip() + aes(fill = ct) + scale_fill_manual(values = col) + theme_bw() + xlab(NULL) + ylab(NULL) + guides(fill=guide_legend(title="Median monthly wage")) + labs(title = unique(mgr$occ_title)) + scale_y_continuous(breaks = seq(0,25,1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment