Choropleth maps are a way to show the geographic distribution of a statistic of interest (e.g., population density, household income) using color or shading. These maps provide an easy way to visualize how a variable differs across geographic areas, like states, counties, or towns.
In the tabs that follow, I’ve built choropleth maps depicting the 10 day average Covid-19 test postivity rate for Connecticut’s 169 towns using three different toolkits. The first is a static map built using basic functions from the R package {ggplot2}. The next two are interactive maps. You can hover the mouse over each town for more detail. One was built using the R package {plotly}, the other with the {leaflet} package. Both of these rely on external javascript tools to do the actual work.
This data sketch is about choropleths as a data visualization method. It is not about Covid-19, as such. Those data were chosen just because they make a handy example.
You can always find the most current version of this data sketch online here. The code used to build that page is on github here.
This is a non-interactive map built with basic ggplot2 functions.
I am using a shapefile-based set of multipolygons for the town outlines.
On the following tabs, I’ve created interactive versions of this same figure using plotly::plot_ly() and leaflet::leaflet().
Interactive choropleth with plotly::plot_ly().
I’m using a geojson-based set of polygons for town outlines in this map. Plot_ly() gets the aspect ratio wrong if using the shapefile-based set of multipolygons that I used with ggplot() and ggplotly() maps. I’m not sure why.
Also, the shapefiles are probably higher resolution than needed. Using the lower res geojson polygons saves nearly 3mb on the total file size.
10 Day Average Covid-19 Test Positivity
in Connecticut Towns
for
period ending June 24, 2022
Interactive choropleth via leaflet::leaflet().
Previous plots in this series used multipolygon shapes for towns taken from conventional shape files. I couldn’t get that to work with leaflet and so switched to simple polygons. These were read from geojson files, but I think the crucial difference is multipolygon vs. somple polygons.
I don’t know why the colorscale in the legend is inverted relative to expectations. It’s annoying, but there doesn’t seem to be a way to correct it. A couple of “solutions” I’ve seen proposed on StackOverflow just don’t work.
Leaflet maps don’t provide a “title” feature, as such. Here, I’ve
just added the title text above the map. Formatting is by way of the
HTML <font>
tag. It’s not best practice, but it
works.
Covid data used in these maps was compiled by CT Dept. of Public Health through Jun 24, 2022 and accessed through https://data.ct.gov/stories/s/COVID-19-data/wa3g-tfvc/#data-library.
Maps were created by David Braze (davebraze@gmail.com) using R statistical software and released under the Creative Commons v4.0 CC-by license.
You can always find the most current version of this data sketch online at: https://davebraze.github.io/ct-covid-map-examples/.
All data summaries in this dashboard were produced with the
R statistical environment, version 4.1.0. The dashboard itself
was made using an Rmarkdown workflow. The following table lists the
non-base R packages used in building the dashboard. To see a full
citation for a specific package, assuming you have both R and
the particular package installed, call (e.g.)
citation("dplyr")
from the R prompt.
package | version | date |
---|---|---|
dplyr | 1.0.9 | 2022-04-28 |
FDBpub | 0.0.1.9999 | 2022-04-29 |
FDButils | 0.0.10 | 2022-01-29 |
flexdashboard | 0.5.2 | 2020-06-24 |
fs | 1.5.2 | 2021-12-08 |
ggplot2 | 3.3.6 | 2022-05-03 |
here | 1.0.1 | 2020-12-13 |
httr | 1.4.3 | 2022-05-04 |
leaflet | 2.1.1 | 2022-03-23 |
lubridate | 1.8.0 | 2021-10-07 |
plotly | 4.10.0 | 2021-10-09 |
purrr | 0.3.4 | 2020-04-17 |
RSocrata | 1.7.11.2 | 2021-09-14 |
sf | 1.0.7 | 2022-03-07 |
stringr | 1.4.0 | 2019-02-10 |
XML | 3.99.0.10 | 2022-06-09 |