Create a web page presentation using R Markdown that features a plot created with Plotly. To Host a webpage on RPubs. The webpage must contain the date that you created the document, and it must contain a plot created with Plotly.
First libraries are loaded:
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)
##
## 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
Then data is loaded:
myurl <- "https://www2.census.gov/govs/statetax/15staxcd.txt"
mydestfile <- "15staxcd.txt"
download.file(myurl, destfile = mydestfile)
Then the DataFrame is created:
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")
state_tax$hover <- with(state_tax, paste(State, '<br>', "Taxes:", Taxes))
Then some code is generated for the settings that will be called as arguments when generating the choropleth map.
borders <- list(color = toRGB("red"))
map_options <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = TRUE,
lakecolor = toRGB('white')
)
Finally de map is presented:
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 per State, 2015 <br>(US Census Bureau)',
geo = map_options)