Don’t you hate that last or first part of your commute, where you have to go over what seems like an endless stream of speedbumps? Whose fault is that, bad drivers? The City? Or perhaps those in your neighborhood associating making a ruckus until they get installed? With this data I intend to see if there is any pattern to the placement of speedbumps and their location in or out of a neighborhood association.
The data comes from the City of San Antonio Open Database. I used the GeoJSON data because of it having all the necessary coordinate information.
Neighborhoods <- rgdal::readOGR("http://opendata-cosagis.opendata.arcgis.com/datasets/f2b4965f14ab4821a6ebb8bd441b5cea_0.geojson?outSR={%22latestWkid%22:2278,%22wkid%22:102740}")
Speedbumps <- rgdal::readOGR("http://opendata-cosagis.opendata.arcgis.com/datasets/827a1513da6b491eb780d95d301ad542_0.geojson?outSR={%22latestWkid%22:2278,%22wkid%22:102740}")
I used GEOJSON data because the CVS provided did not give coordinate information.
map2 <- leaflet(Neighborhoods)%>%
addTiles() %>%
addPolygons(data = Neighborhoods, color = "#4B088A") %>%
addMarkers(data = Speedbumps,
clusterOptions=markerClusterOptions())
map2
The data seems spotty at best, at first glance there may be a connection, but upon closer inspection, the groups split into areas inside and outside of neighborhood associates. Also, as a side note, I live in a San Antonio neighborhood with an association, but we were not listed on the data.
Using Leaflet, we can also create a Choropleth map based on the population of every county. To do so, I first downloaded a GeoJSON from the State of Texas (https://data.texas.gov/Government-and-Taxes/County-Map/48ag-x9aa). I then found the population data for Texas by county on the following website (https://worldpopulationreview.com/us-counties/tx/). Since my skills with Spatialdata are still a bit lacking, I elected to use a third party to merge my data properly. This did require a bit of cleaning using the following code:
CountyPop <- gsub(" county", "", CountyPop)
This was to ensure that both my datasets had matching variables so that the population data would be added correctly to each county. I used the following website to complete the merge (https://funkeinteraktiv.github.io/geo-data-merger/). The final step was to upload the data.
TXcounties <- geojson_read("MergeCountyMap.geojson", what = "sp")
Now with a fully functioning GeoJSOn, I could begin to create the map. the first step was to check if I could accurately chart each county, to make sure it was all in working order.
Texas <- leaflet(TXcounties) %>%
addTiles()
From there, I added the YlGnBu color palette from ColorBrewer.This also included the creation of a scale for my data, which ranged from around 100 to over 4 million! Because of how my data was merged, the population value is read as a character, but that is remedied with the as.integer function below.
bins <- c(100, 2500, 5000, 10000, 50000, 100000, 150000, 2500000, Inf)
shader <- colorBin("YlGnBu", domain = as.integer(TXcounties$Pop), bins = bins)
Next is labels of the map, so that you can move your mouse over and see the population displayed if need be. Using some html and the sprintf fucntion, we can have our labels read in the respective data. We can even improve its readability by adding a comma between each 3 digits as necessary.
labels <- sprintf(
"<strong>%s</strong><br/>%s people",
TXcounties$name, format(as.integer(TXcounties$Pop), big.mark=",", Scientific = FALSE)
) %>% lapply(htmltools::HTML)
Now with all the pieces in place, the map data is ready to be implemented.
Map3 <- Texas %>%
addPolygons(
fillColor = ~shader(as.integer(TXcounties$Pop)),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.65,
highlight = highlightOptions(
weight = 4,
color = "#666",
dashArray = "",
fillOpacity = 0.65,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "12px",
direction = "auto"))
Map3