Objective

The main objective of this assignment is to create a map (choropleth type) using Plotly functions in R. It will executes and analyze data from US Government Tax Collections especially alcoholic beverages products for the year 2015.

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
url <- "https://www2.census.gov/govs/statetax/15staxcd.txt"
file <- "statetax.txt"
download.file(url, destfile = file)

# Reference Link: https://www.census.gov/govs/statetax/data_file_layout.html

data <- read.table(file, header=TRUE, sep=",")
taxes <- data[3,c(2:8, 10:52)]

# Dataframe creation
tax_state <- data.frame(State = names(taxes), 
                        Taxes = t(taxes))
names(tax_state)<-c("State", "Taxes")

# Hovertext creation
tax_state$hover <- with(tax_state, paste(State, '<br>', "Taxes:", Taxes))

# Aesthetics and colors
bord <- list(color = toRGB("red"))

# Map
map <- list(
  scope = 'usa',
  projection = list(type = 'albers usa'),
  showlakes = TRUE,
  lakecolor = toRGB('white')
)

plot_ly(z = tax_state$Taxes, text = tax_state$hover, locations = tax_state$State, 
        type = 'choropleth', locationmode = 'USA-states', 
        color = tax_state$Taxes, colors = 'Blues', marker = list(line = bord)) %>%
  layout(title = 'Sales Tax per State for Alcoholic Beverages (2015) <br>(US Census Bureau)', 
         geo = map)
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.