-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.Rmd
373 lines (327 loc) · 8.94 KB
/
index.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
---
output: html_document
resource_files:
- '.'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
# the code below is modified from
# https://glin.github.io/reactable/articles/popular-movies/popular-movies.html.
# thank you, reactable!
```
```{r table, echo=FALSE, message=FALSE}
library(reactable)
library(jsonlite)
library(tidyverse)
library(crosstalk)
library(htmltools)
library(crosstool)
low_wage_data <- read_csv("low_wage_data.csv") |>
select(
category,
category_group,
share = low_wage_share,
count = low_wage_count,
low_wage_threshold,
dates
)
table_data <- low_wage_data
get_share_color <- function(share) {
my_pal <- function(x) rgb(colorRamp(c("#f58a61", "#d4214a"))(x), maxColorValue = 255)
normalized <- (share - min(share)) / (max(share) - min(share))
my_pal(normalized)
}
table_data <- mutate(table_data, share_color = get_share_color(share))
# Shared data for the table
shared_data_new <- SharedData$new(table_data)
# Shared data for the Crosstalk filters. This is a separate shared data object
# so we can filter on columns that aren't in the table. The same group is used
# to link the two datasets together.
shared_wages <- SharedData$new(low_wage_data, group = shared_data_new$groupName())
table <- reactable(
shared_data_new,
defaultColDef = colDef(vAlign = "center", headerClass = "header"),
columns = list(
category = colDef(
name = "Group",
align = "right",
minWidth = 150
),
share = colDef(
name = "Share of workers<br/>paid less than $15",
defaultSortOrder = "desc",
cell = JS("renderShare"),
html = TRUE,
align = "center",
class = "user-score",
minWidth = 200
),
count = colDef(
name = "Number of workers<br/>paid less than $15",
html = TRUE,
align = "left",
minWidth = 200,
format = colFormat(separators = TRUE)
),
category_group = colDef(show = FALSE),
dates = colDef(show = FALSE),
share_color = colDef(show = FALSE),
low_wage_threshold = colDef(show = FALSE),
dates = colDef(show=FALSE)
),
highlight = TRUE,
theme = reactableTheme(
highlightColor = "#f3fafb",
borderColor = "hsl(0, 0%, 93%)",
headerStyle = list(borderColor = "hsl(0, 0%, 90%)")
),
class = "movies-tbl",
width = "auto",
pagination = FALSE,
elementId = "htmlwidget-1"
)
```
```{js table_js, echo=FALSE}
// Custom JavaScript cell renderer for the donut
function renderShare(cellInfo) {
return donutChart(cellInfo.value, cellInfo.row['share_color'])
}
// Generates HTML for a donut chart given a percentage value (out of 100) and color
function donutChart(value, color) {
// All units are in rem for relative scaling
const radius = 3.5
const diameter = 9.75
const center = diameter / 2
const width = 2.5
const sliceLength = 2 * Math.PI * radius
const sliceOffset = sliceLength * (1 - value / 100)
const donutChart = `
<svg width="${diameter}rem" height="${diameter}rem" style="transform: rotate(-90deg)" focusable="false">
<circle cx="${center}rem" cy="${center}rem" r="${radius}rem" fill="none" stroke-width="${width}rem" stroke="rgba(0,0,0,0.1)"></circle>
<circle cx="${center}rem" cy="${center}rem" r="${radius}rem" fill="none" stroke-width="${width}rem" stroke="${color}"
stroke-dasharray="${sliceLength}rem" stroke-dashoffset="${sliceOffset}rem"></circle>
</svg>
`
const label = `
<div style="position: absolute; top: 49%; left: 52%; transform: translate(-50%, -50%)">
${value}%
</div>
`
return `
<div style="display: inline-flex; position: relative">
${donutChart}
${label}
</div>
`
}
```
```{r filters, echo=FALSE}
# Custom Crosstalk select filter. This is a single-select input that works
# on columns containing multiple values per row (list columns).
select_filter <- function(id, label, shared_data, group, choices = NULL,
width = "100%", class = "filter-input") {
values <- shared_data$data()[[group]]
keys <- shared_data$key()
if (is.list(values)) {
# Multiple values per row
flat_keys <- unlist(mapply(rep, keys, sapply(values, length)))
keys_by_value <- split(flat_keys, unlist(values), drop = TRUE)
choices <- if (is.null(choices)) sort(unique(unlist(values))) else choices
} else {
# Single value per row
keys_by_value <- split(seq_along(keys), values, drop = TRUE)
choices <- if (is.null(choices)) sort(unique(values)) else choices
}
script <- sprintf("
window['__ct__%s'] = (function() {
const handle = new window.crosstalk.FilterHandle('%s')
const keys = %s
return {
filter: function(value) {
if (!value) {
handle.clear()
} else {
handle.set(keys[value])
}
}
}
})()
", id, shared_data$groupName(), toJSON(keys_by_value))
div(
class = class,
tags$label(`for` = id, label),
tags$select(
id = id,
onchange = sprintf("window['__ct__%s'].filter(this.value)", id),
style = sprintf("width: %s", validateCssUnit(width)),
tags$option(value = "", "All categories"),
lapply(choices, function(value) tags$option(value = value, value))
),
tags$script(HTML(script))
)
}
```
```{r text_calculations, echo = FALSE}
total_low_wage <- low_wage_data |>
filter(category == "All workers") |>
pull(count)
total_low_wage <- round(total_low_wage/10^6)
data_dates <- low_wage_data |>
filter(row_number() == 1) |>
pull(dates)
```
---
title: `r total_low_wage` million workers are paid less than $15 per hour
---
```{r output, echo=FALSE}
div(
class = "movies",
div(
class = "filters",
select_filter("filter_genres", "Select a group of workers", shared_data_new, "category_group")
),
#tags$hr(),
table
)
```
---
Source: Analysis by [Ben Zipperer](https://www.epi.org/people/ben-zipperer/) of the Economic Policy Institute Current Population Survey [extracts](https://microdata.epi.org/), `r data_dates`. Wages include overtime, tips, and commissions. Download the [data](low_wage_data.csv) shown in the figure above or the [code](https://github.com/Economic/under15) that produces it.
```{css echo=FALSE}
body {
max-width: 1000px;
margin: auto;
padding: 1em;
}
/* Font from https://fontsarena.com/hanken-grotesk-by-hanken-design-co/ */
@font-face {
font-family: 'Hanken Grotesk';
font-style: normal;
font-weight: 400;
src: url("fonts/HKGrotesk-Regular.woff2") format("woff2"),
url("fonts/HKGrotesk-Regular.woff") format("woff");
}
@font-face {
font-family: 'Hanken Grotesk';
font-style: normal;
font-weight: 600;
src: url("fonts/HKGrotesk-SemiBold.woff2") format("woff2"),
url("fonts/HKGrotesk-SemiBold.woff") format("woff");
}
@font-face {
font-family: 'Hanken Grotesk';
font-style: normal;
font-weight: 700;
src: url("fonts/HKGrotesk-Bold.woff2") format("woff2"),
url("fonts/HKGrotesk-Bold.woff") format("woff");
}
.movies {
font-family: 'Hanken Grotesk', Helvetica, Arial, sans-serif;
width: 600px;
margin: 0 auto;
}
.movies h2 {
font-weight: 600;
}
.movies a {
color: #007899;
text-decoration: none;
}
.movies a:hover,
.movies a:focus {
text-decoration: underline;
text-decoration-thickness: max(1px, 0.0625rem);
}
.movies-tbl {
margin-top: 1rem;
font-size: 1.5rem;
}
.header {
color: hsl(0, 0%, 45%);
font-weight: 700;
font-size: 1.25rem;
letter-spacing: 0.4px;
text-transform: uppercase;
padding: 0;
}
.title {
text-align: center;
}
.header:hover[aria-sort],
.header[aria-sort='ascending'],
.header[aria-sort='descending'] {
color: hsl(0, 0%, 5%);
}
.movie-info {
display: flex;
align-items: center;
}
.movie-info-text {
margin-left: 0.75rem;
font-weight: 600;
overflow: hidden;
text-overflow: ellipsis;
}
.movie-info-details {
margin-top: 0.125rem;
font-size: 0.875rem;
font-weight: 400;
color: hsl(0, 0%, 40%);
overflow: hidden;
text-overflow: ellipsis;
}
.movie-poster {
width: 45px;
height: 68px;
box-shadow: 0 0 0 1px hsl(0, 0%, 95%);
}
.movie-runtime {
display: inline-block;
}
.movie-rating {
margin-right: 0.25rem;
padding: 0 0.25rem;
border: 1px solid hsl(0, 0%, 75%);
border-radius: 2px;
}
.user-score {
font-weight: 600;
font-size: 1.75rem;
}
.filters {
display: flex;
flex-wrap: wrap;
margin-top: 1rem;
margin-left: -32px;
text-align: left;
}
.filter-input {
margin-top: 0.5rem;
margin-left: 32px;
flex: 1;
min-width: 250px;
}
.filter-input label {
color: hsl(0, 0%, 45%);
font-weight: 700;
font-size: 0.8125rem;
letter-spacing: 0.4px;
text-transform: uppercase;
}
.filter-input select,
.filter-input input[type="search"] {
padding: 0 0.375rem;
height: 2rem;
}
.filter-input input[type="search"] {
/* Revert Bootstrap 5's Reboot styles, which change native search input styling */
-webkit-appearance: searchfield;
outline-offset: revert;
border-color: revert;
}
```
```{css echo=FALSE}
/* pkgdown articles */
.row > main {
max-width: 940px;
}
```