-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.qmd
245 lines (220 loc) · 6.23 KB
/
index.qmd
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
---
title: "Taxas de suicídio no Brasil: painel de dados"
lang: pt-br
author:
name: "Bruno Braga Montezano"
orcid: 0000-0002-1825-0097
affiliation: "UFRGS"
format: dashboard
theme: zephyr
server: shiny
---
```{r carregar-dados-e-paleta}
#| context: setup
#| include: false
df <- readr::read_csv("./data/txs_suicidio.csv", show_col_types = FALSE) |>
janitor::clean_names()
sf_estados <- readr::read_csv(
"./data/localizacao_estados.csv",
show_col_types = FALSE
)
df_padronizada <- df |>
dplyr::group_by(local, ano) |>
dplyr::slice_head(n = 1) |>
dplyr::ungroup() |>
dplyr::select(ano, local, taxa_bruta, taxa_padronizada) |>
dplyr::mutate(texto = glue::glue(
"<b>Local:</b> {local}\n<b>Ano:</b> {ano}\n<b>Taxa:</b> {sprintf('%.2f', {taxa_padronizada})}")
)
df_estados <- df |>
dplyr::filter(tipo_local == "UF") |>
dplyr::group_by(local, ano) |>
dplyr::slice(1) |>
dplyr::ungroup() |>
dplyr::select(uf = local, ano, taxa_bruta, taxa_padronizada) |>
dplyr::left_join(
sf_estados |> dplyr::select(abbrev_state, name_state, lon, lat),
by = dplyr::join_by(uf == abbrev_state)
)
paleta <- leaflet::colorNumeric(
palette = "PuBu",
domain = df_estados$taxa_padronizada
)
```
# Séries temporais
```{r selecionar-local}
#| content: card-sidebar
shiny::checkboxGroupInput(
"local_filtro",
"Local",
choices = unique(df$local),
selected = "Brasil"
)
```
```{r plotar-series}
#| title: Série temporal de taxas padronizadas de suicídio (por 100 mil habitantes)
plotly::plotlyOutput("plot_series")
```
# Mapa por estados
```{r selecionar-ano-estados}
#| content: card-sidebar
shiny::selectInput(
"ano_filtro_mapa",
"Ano",
choices = 2000:2021,
selected = 2000
)
```
```{r plotar-mapa-estados}
#| title: Taxa padronizada de suicídio por estado (por 100 mil habitantes)
leaflet::leafletOutput("mapa_estados")
```
# Mapas de calor
```{r selecionar-ano-local-heatmap}
#| content: card-sidebar
shiny::selectInput(
"ano_filtro_heatmap",
"Ano",
choices = 2000:2021,
selected = 2000
)
shiny::selectInput(
"local_filtro_heatmap",
"Local",
choices = unique(df$local),
selected = "Brasil"
)
```
```{r plotar-calor}
#| title: Mapa de calor de taxas padronizadas de suicídio por idade e sexo (por 100 mil habitantes)
plotly::plotlyOutput("plot_calor")
```
# Trajetória das taxas por estado
```{r plotar-animacao-estado}
#| title: Trajetória das taxas padronizadas de suicídio por estado (2000-2021)
plotly::plotlyOutput("plot_animacao_estados")
```
```{r criar-visualizacoes}
#| context: server
#| include: false
dataset_ano <- shiny::reactive({
df_estados[df_estados$ano == input$ano_filtro_mapa, ]
})
dataset_local <- shiny::reactive({
df_padronizada[df_padronizada$local %in% input$local_filtro, ]
})
dataset_calor <- shiny::reactive({
df[df$local == input$local_filtro_heatmap & df$ano == input$ano_filtro_heatmap, ]
})
output$plot_series <- plotly::renderPlotly({
plotly::plot_ly(
dataset_local(),
x = ~ ano,
y = ~ taxa_padronizada,
text = ~ texto,
hoverinfo = "text",
type = "scatter",
mode = "markers+lines",
color = ~ local,
colors = "Set2",
marker = list(size = 10),
line = list(width = 5)
) |>
plotly::layout(
yaxis = list(
title = "Taxa padronizada de suicídios (por 100 mil habitantes)",
titlefont = list(size = 15)
),
xaxis = list(
title = "Ano",
titlefont = list(size = 15),
tickvals = as.list(2000:2021),
tickmode = "array"
)
)
})
output$mapa_estados <- leaflet::renderLeaflet({
leaflet::leaflet(dataset_ano()) |>
leaflet::setView(lat = -15.178, lng = -56.777, zoom = 4) |>
leaflet::addTiles() |>
leaflet::addCircleMarkers(
lng = ~ lon,
lat = ~ lat,
fillOpacity = 0.7,
color = ~ paleta(taxa_padronizada),
radius = ~ sqrt(taxa_padronizada) * 6,
popup = ~ paste("<b>Estado:</b> ",
name_state,
"<br> <b>Ano:</b> ",
ano,
"<br> <b>Taxa Padronizada (por 100 mil):</b> ",
taxa_padronizada
)
) |>
leaflet::addLabelOnlyMarkers(
~lon, ~lat,
label = ~ as.character(glue::glue("{sprintf('%.2f', {taxa_padronizada})}")),
labelOptions = leaflet::labelOptions(noHide = TRUE, textOnly = TRUE, direction = "center")
)
})
output$plot_calor <- plotly::renderPlotly({
plotly::plot_ly(
dataset_calor(),
x = ~ faixa_etaria,
y = ~ sexo,
z = ~ taxa_especifica,
type = "heatmap",
colorbar = list(title = "Taxa de suicídio específica"),
hoverinfo = "text",
text = ~ paste0("<b>Local:</b> ",
local,
"<br> <b>Ano:</b> ",
ano,
"<br> <b>Faixa etária</b> ",
faixa_etaria,
"<br> <b>Sexo</b> ",
sexo,
"<br> <b>Taxa Específica (por 100 mil):</b> ",
taxa_especifica
)
) |>
plotly::layout(
yaxis = list(
title = "Sexo",
titlefont = list(size = 15)),
xaxis = list(
title = "Faixa etária",
titlefont = list(size = 15)
)
)
})
output$plot_animacao_estados <- plotly::renderPlotly({
plotly::plot_ly(
df_estados,
x = ~ uf,
y = ~ taxa_padronizada,
frame = ~ ano,
type = "bar",
hoverinfo = "text",
text = ~ paste0("<b>UF:</b> ",
name_state,
"<br> <b>Ano:</b> ",
ano,
"<br> <b>Taxa Padronizada (por 100 mil):</b> ",
taxa_padronizada
)
) |>
plotly::animation_slider(
currentvalue = list(prefix = "Taxas em: ", font = list(color = "black"))
) |>
plotly::layout(
yaxis = list(
title = "Taxa padronizada de suicídios (por 100 mil habitantes)",
titlefont = list(size = 15)),
xaxis = list(
title = "Estado",
titlefont = list(size = 15)
)
)
})
```