This post will demonstrate how to create a choropleth map using the leaflet R package in our RStudio application.
1. Launch the RStudio app and go to the packages tab. Activate the Raster and Leaflet packages by checking the checkboxes beside them.
2. Once the packages are loaded onto the window console, we load in a shapefile into the R environment by writing this statement:
bauchi <- shapefile(“path to file”)
Press the Enter key to run the command.
3. Check in the ‘Environment’ tab to inspect the shapefile that was added.
4. The command lines for creating the choropleth map is below along with the explanation.
//This defines a color gradient for the vector object defined in the domain
pal<- colorNumeric(palette = “OrRd”. domain = bauchi$total)
//bind Popup display to the vector data
n_popup <- paste0(“name:”.bauchi$NAME_2, “population:”, bauchi$total)
leaflet(data = bauchi) %>% initialize leaflet package
addTiles() %>% Add the base tiles
addPolygons(fillColor:~pal(total), color:”000″, weight: 2,
//This will add a legend to the map
addLegend(“bottomright”, pal=pal, values = ~total,
title = “Legend”, opacity = 1)
5. Go to the Viewer page to see the results.
Thank you for your following.