This simple script creates an outline map of U.S. states along with a popup of corresponding state name when clicked.
The result produced by this code is then published to RPubs.
#load needed libraries
library(leaflet)
library(maps)
set.seed(2016-04-25)
df = data.frame(
lat = runif(100, min = 39.25, max = 39.35),
lng = runif(100, min = -76.65, max = -76.55),
size = runif(100, 5, 20),
color = sample(colors(), 100)
)
#use the map dataset from the maps library
mapStates = map("state", fill = TRUE, plot = FALSE)
state_popup <- mapStates$names
#create the interactive map with a pop for each state in the U.S.
my_map <- leaflet(data = mapStates) %>%
addTiles() %>%
addPolygons(color = rainbow(10, alpha = NULL)
, stroke = TRUE, weight = 0.5, smoothFactor = 0.5
, highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE)
,popup = state_popup)
my_map