-
Notifications
You must be signed in to change notification settings - Fork 0
/
ct-covid-map.Rmd
189 lines (113 loc) · 7.05 KB
/
ct-covid-map.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
---
title: "CT COVID Map: Variations"
author: David Braze
output:
flexdashboard::flex_dashboard:
theme: yeti
orientation: columns
vertical_layout: fill
---
```{css echo=FALSE}
h3 {
font-size: 100px;
}
```
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(FDBpub)
theme_set(FDBpub::theme_fdbplot(base_size=10))
theme_update(axis.line = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank(),
panel.border = element_blank())
knitr::opts_chunk$set(fig.width=7,
fig.height=7/(16/9),
fig.align = "center", ## horz center figs on page
out.width="95%",
out.height="95%",
dpi=if(doc_type()=="pdf") 300 else 200,
dev=if(doc_type()=="pdf") "cairo_pdf" else "png",
dev.args=if(doc_type()=="pdf") NULL else list(type="cairo-png"),
echo=FALSE,
message=FALSE,
warning=FALSE,
include=TRUE,
results='asis',
cache=FALSE,
cache.lazy=FALSE)
```
```{r workhorse, include=FALSE}
source(here::here("ct-covid-map.R"))
```
# Intro
## Column {data-width=333}
## Column {data-width=333}
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](https://davebraze.github.io/ct-covid-map-examples/). The code used to build that page is on github [here](https://github.com/davebraze/ct-covid-map-examples).
## Column {data-width=333}
# GGplot
## Column {data-width=750}
### **GGplot**
```{r ggplot}
map.positivity
```
## Column {data-width=250}
### **Notes**
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().
# Plot_ly
## Column {data-width=750}
### **Plot_ly**
```{r plotly}
map.positivity.plotly
```
## Column {data-width=250}
### **Notes**
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.
# Leaflet
## Column {data-width=750}
### **Leaflet**
`r paste("<p style='font-size:20px; font-family:sans-serif; font-weight:bold '>10 Day Average Covid-19 Test Positivity<br>in Connecticut Towns<br>for period ending", format(max(ct.covid$Date), "%B %d, %Y"), "</p>") `
```{r leaflet}
map.positivity.leaflet
```
## Column {data-width=250}
### **Notes**
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.
# About
## Column {data-width=333}
### About the Maps
Covid data used in these maps was compiled by CT Dept. of Public Health through `r format(max(ct.covid$Date), "%b %d, %Y")` and accessed through [https://data.ct.gov/stories/s/COVID-19-data/wa3g-tfvc/#data-library](https://data.ct.gov/stories/s/COVID-19-data/wa3g-tfvc/#data-library).
Maps were created by David Braze ([email protected]) 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/](https://davebraze.github.io/ct-covid-map-examples/).
The code used to build this dashboard and the maps it contains can be found on github at: [https://github.com/davebraze/ct-covid-map-examples](https://github.com/davebraze/ct-covid-map-examples).
## Column {data-width=333}
### About the Author
David Braze is a researcher and consultant with a background in linguistics and reading research. He has more than 25 years experience investigating the cognitive foundations of language, literacy, and educational achievement, including 17 years as a Senior Research Scientist at [Haskins Laboratories](https://haskinslabs.org/). His research at Haskins, funded by the National Institutes of Health, emphasized the neurobiology of language and reading and applications to education. Dr. Braze consults widely for business, government, and non-profit sectors.
| email: [email protected]
| website: [davebraze.org](https://davebraze.org/ "D. Braze's website")
## Column {data-width=333}
### About the Software
All data summaries in this dashboard were produced with the _R_ statistical environment, version `r paste(version$major, version$minor, sep=".")`. 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.
```{r r-package-tab, include=TRUE}
package_tab <-
sessioninfo::package_info() %>%
unclass() %>%
as_tibble() %>%
filter(attached == TRUE) %>%
select(package, ondiskversion, date) %>%
rename(version = ondiskversion)
caption <- "R packages used in this dashboard."
package_tab %>%
FDBpub::format_table(caption=caption, out_type=doc_type())
```