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)
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.
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.
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.