Trying to make a map with leaflet, pls help!

This is my first time asking for help so please let me know if I did something wrong. I want to create a map of evictions by county in Ohio. I am using data from the Princeton Eviction lab that I downloaded. I really have no idea how to use it. The geojson file has several features. It has a bunch of variables for different years. For instance, er-01 means the eviction rate in 2001. er-16 means the eviction rate in 2016. The data is from 2000 until 2016. I would like to plot this eviction rate variable for each year. Please help! The following code is something I tried, but I am not sure how to add a legend. Is there an easier way to do this? How can I plot for just one variable and do it for every year?

```{r}
library(leaflet)
library(geojsonio)
library(utils)
library(tidyverse)

download.file("https://raw.githubusercontent.com/amalabdi/milestone_8/master/counties.geojson", "counties.geojson")
geojson <- readLines("counties.geojson", warn = FALSE) %>%
  paste(collapse = "\n") %>%
  fromJSON(simplifyVector = FALSE)

# Default styles for all features
geojson$style = list(
  weight = 1,
  color = "#555555",
  opacity = 1,
  fillOpacity = 0.8
)

# Er-03 is the the eviction rate in 2003
evictionrate <- sapply(geojson$features, function(feat) {
  feat$properties$`er-03`
})

# Add a properties$style list to each feature
geojson$features <- lapply(geojson$features, function(feat) {
  feat$properties$style <- list(
    fillColor = feat$properties$`er-03`
  )
  feat
})

# Add the now-styled GeoJSON object to the map
leaflet() %>% addGeoJSON(geojson) 
2 Likes

I haven't done much with geojson but maybe this will help. https://rstudio.github.io/leaflet/json.html

I couldn't get the fromJSON to work, but it works with the RJSONIO library.

1 Like

My baseline strategy for spatial data in R is to get it into sf objects as fast as possible.

You can read geoJSON links directly with sf::read_sf()

library(sf)

sf <- read_sf(
  "https://raw.githubusercontent.com/amalabdi/milestone_8/master/counties.geojson")

Picking out a good color column and a pretty palette:

library(leaflet)

pal_val <- sf$er.03
pal <- colorNumeric("viridis", pal_val)

Then just stack it all together, with some nice map tiles for flair.

leaflet(sf) %>%
  addProviderTiles(providers$Stamen.Toner) %>%
  addPolygons(color = ~pal(pal_val),
              fillColor = ~pal(pal_val)) %>% 
  addLegend(pal = pal, values = pal_val)

Rplot

5 Likes

Thank you so much!! I have been stuck for so long. Is there any way to use addCircleMarkers to display circles whose sizes correspond to the eviction rate (aka er-03)? I am not sure where to begin with calculating centroids so it plots in the middle of each county or how to pull the latitude and longitude for addCircleMarkers.

Again sf is the way.

Now that you have an sf object with the polygons for each of the 88 counties in Ohio. You use sf::st_centroid() to convert those polygons into center points.

sf_centers <- sf %>%
dplyr::mutate(geometry = st_centroid(geometry))

And in leaflet land size is controlled by the radius argument.

leaflet(sf_centers) %>%
addProviderTiles(providers$Stamen.Toner) %>%
addCircleMarkers(radius = ~er.03)

If you want to adjust the sizing of the circle-markers you would adjust the er.03 value with some mathematical operation (addition, multiplication or exponentiation) instead of the pal idea you have to use for coloring.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.