Questions for Interactive Maps:
The us_cities data set in the geojsonio package contains state capitals and all cities in the United States with populations larger than 40,000. The file located at “https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json” contains spatial polygon boundaries for all 50 states (and the District of Columbia and Puerto Rico). The USArrests data set in the datasets package contains violant crime rates by state.
For this question you should create a map with the following features:
USArrests data set.UrbanPop below the name on a new line.radius = 2) for each city in the us_cities data set where state capitals are colored “red” and all other cities are colored yellowHints:
bringToFront = FALSE in your highlight to avoid masking the city labels when hovering over a statedata("us_cities")
shapeurl <- "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
states <- geojson_read(shapeurl, what = "sp")
data("USArrests")
USArrests$State = row.names(USArrests)
statearrests <- left_join(data.frame(Name = states$name), USArrests, by = c("Name" ="State"))
pal <- colorBin("viridis", domain = statearrests$Murder)
mypopups <- paste("<strong>", statearrests$Name,"</strong>", "<br/>",
"Urban Population:", statearrests$UrbanPop)
us_cities$col <- ifelse(us_cities$capital == 2, "red", "yellow")
arrestsmap <- leaflet(states) %>% addTiles() %>%
addPolygons(
fillColor = pal(statearrests$Murder),
weight = 2,
opacity = 1,
color = "black",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 3,
color = "white",
fillOpacity = 0.7,
bringToFront = FALSE),
popup= lapply(mypopups, HTML)) %>%
addLegend(pal = pal, values = statearrests$Murder,
title = "Murder Rates", position = "bottomleft") %>%
addCircleMarkers(lng= us_cities$long,
lat = us_cities$lat,
label= ~paste(us_cities$name, "population:", us_cities$pop),
color= us_cities$col,
fillOpacity = 1,
radius= 2,
stroke= FALSE)
arrestsmap
Questions for Interactive Plots:
The code below loads a dataset compiled by Mother Jones documenting mass shootings in the United States. For this question you should create an animated plot of your choosing that highlights something you deem to be an important or interesting trend in these data.
Mshoot <- read.csv("https://raw.githubusercontent.com/skuiper/datascience/master/datasets/MassShootings.csv")
#plot_ly(data = Mshoot, type = "bar", mode = "markers",
# x= ~Age, y= ~Fatalities, color= ~Place)
plot_ly(data = Mshoot, type = "scatter", mode = "markers",
x= ~Age, y= ~Fatalities, frame= ~Year, showlegend= F,
hoverinfo = 'text',
text = ~paste0("Age: ", Age, "<br>", "Race: ", Race, "<br>", "Fatalities: ", Fatalities))