#' Visualization tool for number of complaints by state.
#'
#' Using a heatmap, it displays the various frequencies of complaints by state on the US map.
#'
#' The function can only take the specific dataframes generated by the other functions in this package that contains the state information. Moreover, the data can only contain two columns, whereas the first column is the number of states and the second column is the number of complaints.
#'
#' @param df Dataframe (two columns)
#' @return Graph of the United States with the ascribed labels
#' @author Jae Woong Ham
#' @import httr
#' @import rjson
#' @import ggplot2
#' @import maps
#' @import sp
#' @import maptools
#' @import stringr
#' @import magrittr
#' @import rvest
#' @import dplyr
#' @export
#' @examples
#' heatmap_state_function()
heatmap_state_function<- function(df=sampledf){
sampledf <- data.frame("x" = c("AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT","NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"), "y" = c(5,12,7,9,25,32,10,2,8,35,5,12,7,9,25,32,10,2,8,35,5,12,7,9,25,32,10,2,8,35,5,12,7,9,25,32,10,2,8,35,5,12,7,9,25,32,10,2,8,35))
df<-as.data.frame(df)
states <- map_data("state")
geo_data <- data.frame(region=df[,1],count=df[,2])
geo_data$region <- tolower(state.name[match(geo_data$region, state.abb)])
geo_data <-na.omit(geo_data)
geo_data_final <-geo_data[!geo_data$region %in% c("hawaii","alaska"),]
geo_data_merged <- merge(states, geo_data_final, by="region")
geonames <- data.frame(region=tolower(state.name), long=state.center$x, lat=state.center$y)
geonames <- merge(geonames, geo_data_final, by="region")
qplot(long, lat, data=geo_data_merged, geom="polygon", fill=as.numeric(as.character(count)), group=group)
ggplot(geo_data_merged, aes(long, lat)) + geom_polygon(aes(group=group, fill=as.numeric(as.character(count)))) + geom_text(data=geonames, aes(long, lat, label=as.numeric(as.character(count))))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.