Skip to content

Instantly share code, notes, and snippets.

@davidski
Created February 22, 2015 18:50
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 davidski/387db83d119a48860c12 to your computer and use it in GitHub Desktop.
Save davidski/387db83d119a48860c12 to your computer and use it in GitHub Desktop.
Poll residential power rates for Top 50 Metro regions
library(dplyr)
library(tidyr)
library(magrittr)
library(httr)
library(ggplot2)
library(ggvis)
#Inspired by original utility rate API post at
#http://www.numbrcrunch.com/blog/using-the-httr-package-to-retrieve-data-from-apis-in-r
census_url <- "http://www.census.gov/popest/data/metro/totals/2013/files/CBSA-EST2013-alldata.csv"
s <- read.csv(url(census_url), stringsAsFactors=F)
top_50_cities <- s %>% filter(LSAD=="Metropolitan Division") %>% arrange(desc(POPESTIMATE2013)) %>% head(50) %>% select(NAME, pop=POPESTIMATE2013) %>%
separate(NAME, c("city", "state"), sep=", ", remove=F)
#key <- "Insert your key here"
# Combining both the Google Maps API and the Data.gov API to get utility rates by city - preferred
google_url <- "https://maps.googleapis.com/maps/api/geocode/json"
gov_url <- "http://api.data.gov/nrel/utility_rates/v3.json"
geoCode <- function(address,verbose=FALSE) {
r <- GET(google_url, query = list(address = address))
stop_for_status(r)
result1 <- content(r)
if (!identical(result1$status, "OK")) {
warning("Please input a valid US address.", call. = FALSE)
return(c(NA,NA,NA,NA,NA,NA))
}
s <- GET(gov_url, query = list(api_key = key, lat = result1$results[[1]]$geometry$location$lat, lon = result1$results[[1]]$geometry$location$lng))
stop_for_status(s)
result2 <- content(s)
if (result2$outputs$utility_name == "no data") {
warning("Please input a valid US address.", call. = FALSE)
return(c(NA,NA,NA,NA,NA,NA))
}
first <- result1$results[[1]]
second <- result2$outputs
list(
lat = first$geometry$location$lat,
lon = first$geometry$location$lng,
type = first$geometry$location_type,
address = first$formatted_address,
utility = second$utility_info[[1]]$utility_name,
residential_rate_kwh = second$residential
)
}
geoCode(top_50_cities$NAME[1])
rates <- lapply(top_50_cities$NAME, geoCode)
rates <- data.frame(matrix(unlist(rates), ncol=6, byrow=T))
top_50_cities$rate <- rates$X6
top_50_cities$utility <- rates$X5
top_50_cities %>% mutate(rate=as.numeric(levels(rate))[rate]*100) %>%
mjs_plot(x=NAME, y=rate, width=500, height=500) %>%
mjs_bar()
plot <- top_50_cities %>% mutate(rate=as.numeric(levels(rate))[rate]*100) %>%
arrange(desc(rate))
plot$NAME <- factor(plot$NAME, levels = plot[order(plot$rate), "NAME"])
#levels(plot$NAME) <- levels(plot$NAME)[order(plot$rate)]
ggplot(plot, aes(NAME, rate)) + geom_bar(stat="identity", fill="steelblue") + coord_flip() +
ylab("Electrical rate (cents/kwh)") +
xlab("Metro Region") +
ggtitle("Electrical Rate for Top 50 Metro Regions") +
geom_text(aes(NAME, rate, label=rate), hjust=0) +
theme_bw()
plot %>%
ggvis(~NAME, ~rate) %>%
layer_bars() %>%
add_axis("x", properties=axis_props(
labels=list(angle=45, align="left")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment