leafletLeaflet is an interactive mapping package
Later in this course we will use leaflet to create interactive maps.
The leaflet() function creates a blank, interactive map canvas in your R Studio Viewer pane.
You can add elements to the map canvas using the generic addControl() function. Some of the more common functions:
addProviderTiles()addCircleMarker()addMarker()addPolylines()addPolygons()Once you add a layer, the map automatically zooms to the extent of the layer.
fitBounds(lng1, lat1, lng2, lat2) where
setView(lng = , lat = , zoom = )At the top of your ga_schools script, make sure that
tidyverse,sf,leafletare all added.Re-import the schools dataset with all variables
(comment out
select(school, long, lat, total_enroll))
Create leaflet map with a baselayer
Define the extent of the map explicitly
Center the map on Atlanta
Add Georgia schools to the map and let the map define the view by the extent of the point layer.
To style your map:
addControl() function to define the colors, size, opacity, etc.
??addControl in the Console.For example, these are some of the most commonly used style choices for a circle marker:
addCircleMarkers(
stroke refers to the outline of the circle or polygonscolor refers to the outline colorweight refers to the thickness of the outlineopacity refers to the transparency of the outline, from 0( transparent) to 1(opaque)fill refers to the inside of the circle or polygonfillColor refers to the inside colorYou can define colors with the hex code, or by calling one of the color names included in base R.
addPolygons(
Define size of circles by the enrollment.
Color points by title 1 school status.
leaflet() |>
addProviderTiles(provider = "CartoDB.Positron") |>
addCircleMarkers(data = ga_schools_shp,
radius = ~ total_enroll/300,
color = ~ case_when(title1_school_status == "2-Title I targeted assistance school" ~ "#5ab4ac",
title1_school_status == "5-Title I schoolwide school" ~ "#3288bd",
title1_school_status == "6-Not a Title I school" ~ "#fc8d59",
TRUE ~ "#e5e5e5"))
popup parameter defines the text that displays when you click on an object in your leaflet map.
paste() function.popup = paste("School: ", ga_schools_shp$school, "<br> Enrollment: ", ga_schools_shp$total_enroll)
Notice
leaflet(ga_schools_shp) function instead of to addCircleMarkers() and then you don’t need to use dataframe$ to call a variable. But this only works if your map only has 1 layer.<br> is the break html tag - it starts a new lineAdd popup with school name
leaflet() |>
addProviderTiles(provider = "CartoDB.Positron") |>
addCircleMarkers(data = ga_schools_shp,
radius = ~ total_enroll/300,
popup = paste("School: ",
ga_schools_shp$school,
"<br> Enrollment: ",
ga_schools_shp$total_enroll),
color = ~ case_when(title1_school_status == "2-Title I targeted assistance school" ~ "#5ab4ac",
title1_school_status == "5-Title I schoolwide school" ~ "#3288bd",
title1_school_status == "6-Not a Title I school" ~ "#fc8d59",
TRUE ~ "#e5e5e5"))Add the school district polygon layer to the map
ga_sd_pop) to your map
addPolygons()scales packageCreate a new script to make a map of your choosing using spatial data and census variables from tidycensus.
Some suggestions: