Objective

The objective of this presentation is to generate a choropleth map using the “Plotly” library in R. The map generated will display State Government Tax Collections for “Alcoholic Beverages Sales Tax (T10)” in the USA from the year 2015. The background information and supporting dataset can be obtained from the United States Census Bureau website: https://www.census.gov/govs/statetax/

Data Analysis

# Loading Libraries
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.6.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# Loading Data
myurl <- "https://www2.census.gov/govs/statetax/15staxcd.txt"
mydestfile <- "15staxcd.txt"
download.file(myurl, destfile = mydestfile)

# Creating Data Frame
mydata <- read.table(mydestfile, header=TRUE, sep=",")
alcbev_taxes <- mydata[3,c(2:8, 10:52)]
state_tax <- data.frame(State = names(alcbev_taxes), Taxes = t(alcbev_taxes))
names(state_tax)<-c("State", "Taxes")

# Creating Hover Text on Map
state_tax$hover <- with(state_tax, paste(State, '<br>', "Taxes:", Taxes))

# Loading Map Options
borders <- list(color = toRGB("red"))
map_options <- list(scope = 'usa', projection = list(type = 'albers usa'),
                    showlakes = TRUE, lakecolor = toRGB('white'))

# Creating Choropleth Map
plot_ly(z = state_tax$Taxes, text = state_tax$hover, locations = state_tax$State,
        type = 'choropleth', locationmode = 'USA-states', color = state_tax$Taxes,
        colors = 'Blues', marker = list(line = borders)) %>%
  layout(title = 'Alcoholic Beverages Sales Tax (T10) per State 2015 <br>(US Census Bureau)',
         geo = map_options)