Plot using plot.sf method
# Apply the new palette and also use a log transformation to the field
plot(acscovid["Positiv"],
main = "Number of Positive COVID-19 Cases in NYC",
nbreaks = 8,
breaks = "jenks",
pal = colorpal,
border = NA,
graticule = st_crs(4326),
axes = TRUE,
logz = TRUE)

# Use the color brewer to create a color palette, 8 colors from orange to red
# for the 8 breaks
RColorBrewer::brewer.pal(8, "OrRd") -> colorpalette
# Remove missing values from blackPp column
acscovid[!is.na(acscovid$blackPp),] -> acscovid
# Apply the new palette and also use a log transformation to the field
plot(acscovid["blackPp"],
main = "Black Population in NYC",
nbreaks = 8,
breaks = "jenks",
pal = colorpalette,
border = NA,
graticule = st_crs(4326),
axes = TRUE,
logz = TRUE)
## Warning in classInt::classIntervals(v0, min(nbreaks, n.unq), breaks, warnSmallN
## = FALSE): var has infinite values, omitted in finding classes

acscovid %>% sf::st_centroid() %>%
dplyr::filter(Positiv > 1800) %>%
sf::st_coordinates() -> labelCoordsCovid
## Warning in st_centroid.sf(.): st_centroid assumes attributes are constant over
## geometries of x
acscovid %>% sf::st_centroid() %>%
dplyr::filter(Positiv > 1800) %>%
dplyr::mutate(x = labelCoordsCovid[,1], y = labelCoordsCovid[,2]) -> labelDataCovid
## Warning in st_centroid.sf(.): st_centroid assumes attributes are constant over
## geometries of x
Using ggplot
ggplot(acscovid) +
geom_sf(aes(fill = Positiv)) +
geom_sf_label(data = labelDataCovid,
aes(x = x, y = y, label = PO_NAME %>% as.character()),
label.size = .09,
size = 3,
segment.color = rgb(0.8,0.8,0.9),
segment.size = 0.8) +
scale_fill_continuous(type = 'viridis') +
labs(x = 'Longitude', y = 'Latitude',
title = 'COVID-19 Cases in NYC',
caption = 'Data Source: New York City Department of Health') -> COVIDCases
## Warning: Ignoring unknown parameters: segment.colour, segment.size
COVIDCases

acscovid %>% sf::st_centroid() %>%
dplyr::filter(blackPp > 45644) %>%
sf::st_coordinates() -> labelCoordsBlack
## Warning in st_centroid.sf(.): st_centroid assumes attributes are constant over
## geometries of x
acscovid %>% sf::st_centroid() %>%
dplyr::filter(blackPp > 45644) %>%
dplyr::mutate(x = labelCoordsBlack[,1], y = labelCoordsBlack[,2]) -> labelDataBlack
## Warning in st_centroid.sf(.): st_centroid assumes attributes are constant over
## geometries of x
ggplot(acscovid) +
geom_sf(aes(fill = blackPp)) +
geom_sf_label(data = labelDataBlack,
aes(x = x, y = y, label = PO_NAME %>% as.character()),
label.size = .09,
size = 3,
segment.color = rgb(0.8,0.8,0.9),
segment.size = 0.8) +
scale_fill_continuous(type = 'viridis') +
labs(x = 'Longitude', y = 'Latitude',
title = 'Black Population in NYC',
caption = 'Data Source: New York City Department of Health') -> BlackPop
## Warning: Ignoring unknown parameters: segment.colour, segment.size
BlackPop

grid.arrange(COVIDCases, BlackPop, ncol=2)

st_transform(acscovid, 4326) -> NewYork_WGS84
leaflet(NewYork_WGS84) %>%
addPolygons()
paste0("Zip Code:", NewYork_WGS84$ZIPCODE) -> p_tip
paste0("<strong> Zip Code </strong>",
NewYork_WGS84$ZIPCODE,
" <br/>",
"<strong> Black Population: </strong>",
NewYork_WGS84$blackPp,
sep="") -> p_popup
paste0("<strong> Zip Code </strong>",
NewYork_WGS84$ZIPCODE,
" <br/>",
"<strong> COVID-19 Cases: </strong>",
NewYork_WGS84$Positiv,
sep="") -> covid_popup
classIntervals(acscovid$blackPp, n = 7, style = "quantile") -> breaks_qt_black
classIntervals(acscovid$Positiv, n = 7, style = "quantile") -> breaks_qt_covid
leaflet::highlightOptions(opacity = 1.0, fillColor = 'black') -> polyHighlightOption
leaflet::labelOptions(opacity = 0.6) -> polyLabelOption
leaflet(NewYork_WGS84) %>%
addPolygons(
stroke = FALSE, # remove polygon borders
fillColor = ~color_pal(blackPp), # set fill color with function from above and value
fillOpacity = 0.8, smoothFactor = 0.5, # make it nicer
popup = p_popup,
group = "New York City",
label = p_tip,
highlightOptions = polyHighlightOption,
labelOptions = polyLabelOption) %>% # add popup
addTiles(group = "OSM") %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addLegend("bottomright", # location
colors = brewer.pal(7, "BuGn"),
labels = paste0("up to ", format(breaks_qt_black$brks[-1], digits = 2)),
title = 'Black Population in New York City by Neighborhood') %>% # legend title
addLayersControl(baseGroups = c("OSM", "Carto"),
overlayGroups = c("New York City"))

leaflet(NewYork_WGS84) %>%
addPolygons(
stroke = FALSE, # remove polygon borders
fillColor = ~color_pal(Positiv), # set fill color with function from above and value
fillOpacity = 0.8, smoothFactor = 0.5, # make it nicer
popup = covid_popup, # add popup
group = "New York City",
label = p_tip,
highlightOptions = polyHighlightOption,
labelOptions = polyLabelOption) %>% # add popup
addTiles(group = "OSM") %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addLegend("bottomright", # location
colors = brewer.pal(7, "YlOrRd"),
labels = paste0("up to ", format(breaks_qt_covid$brks[-1], digits = 2)),
title = 'COVID-19 Cases in New York City by Neighborhood') %>% # legend title
addLayersControl(baseGroups = c("OSM", "Carto"),
overlayGroups = c("New York City")) -> htmlMap

htmlMap