-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractions.qmd
268 lines (206 loc) · 7.68 KB
/
interactions.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
---
title: "Faire beau, utile et moderne"
subtitle: "Des graphiques interactifs dans les documents html ou dans les présentations"
date: 12/05/2022
author: Xavier Timbeau
institute: "*OFCE/Sciences Po*"
lang: fr
title-block-banner: true
format:
html:
theme: simple
code-fold: true
revealjs:
theme: [simple, custom.scss]
logo: ofce.png
incremental: false
progress: true
fig-align: center
code-fold: true
reveal_options:
minScale: 1.0
maxScale: 1.0
editor: visual
---
## Pourquoi interactif ? {.smaller}
- au delà d'une simple image, on peut :
- accroître l'information montrée par le graphique
- zoomer sur une plage de l'axe des abscisses ou des ordonnées
- lire un chiffre ou une date précisément
- ajouter des informations contextuelles
- présenter une famille de graphiques indexées par un paramètre en réduisant la charge du graphique
- ce qui permet :
- d'augmenter la portée du graphique
- de simplifier la présentation
- d'adapter la présentation aux choix de l'utilisateur
- de répondre à son attente
## une carte : l'interactivité est attendue \[moderne, utile\]
```{r, echo=TRUE, message=FALSE, warning=FALSE}
library(tmap)
tmap_mode("view")
load("newgtfs.rda")
tm_shape(res$routes_sf)+tm_lines(col="route_id", lwd=3) +
tm_shape(res$am_stop_name_departures)+tm_dots(size=0.02, col="purple", alpha=0.5)
```
## Un graphique \[beau, utile\] {.smaller}
```{r, echo=TRUE, message=FALSE, warning=FALSE}
library(tidyverse)
library(ofce)
library(ggh4x)
cocol <- qs::qread("data/coicops.qs") |> pull(color, name = coicop)
gcom <- qs::qread("data/gcom.qs")
load("data/gcom.rdata")
CMOr <- CMOr |> mutate(
text = glue::glue(str_c(
"{variable}",
"obs. on: {format(time, format = '%B %Y')}",
"real price index (2018=100): {round(pcom_real,1)}",
"nominal price {prettyNum(epcom, digits=3, format='fg', big.mark = ',')} {eunit}", sep="<br>")))
selected <- CMOr |>
distinct(variable, type) |>
filter(type!="unknown") |>
group_by(type) |>
slice_sample(n=2) |>
pull(variable)
caption <- str_c(
"Sources : World Bank Commodity Markets, Eurostat HICP monthly since 1990, INSEE National Accounts, OECD EO110")
g <- ggplot(CMOr |> filter(variable %in% selected))+
geom_line(
aes(x=time, y=pcom_real,
color=type, group=variable,
text=text),
show.legend = FALSE, linewidth=0.25)+
scale_y_log10(
breaks = scales::log_breaks(10),
minor_breaks = scales::log_breaks(16),
guide = "axis_minor")+
scale_color_manual(
values = c(food = cocol[["CP01"]],
nrg = cocol[["CP07"]],
fer = cocol[["CP05"]])) +
xlab("")+ylab("")+
labs(
title = "",
subtitle = "",
caption = caption) +
theme_ofce(base_family="Arial", base_size=9)+
scale_x_date(
guide="axis_minor",
date_minor_breaks = "1 year",
date_labels = "%Y")
g+facet_wrap(vars(variable), ncol = 2)
```
## beau et utile parce que
- Calculé en termes réels, rebasé pour chaque commodité, avec un indice de prix sur 60 ans, des taux de change en *mensuel* donc un peu de travail sur les données
- des facets qui évitent le spaghetti
- échelle logarithmique qui permet de comparer les différentes commodités
- une chaîne de mise à jour dans R (accès par programme à Eikon, INSEE, banque mondiale, OCDE, presque Datastream)
- un choix de couleurs, élégant, sobre, *by Paul Malliet*, le theme_ofce
mais {{< fa skull >}} {{< fa skull >}} {{< fa skull >}}
## version *print* {.scrollable}
{.scrollable fig-align="center" width="628"}
## + tooltip + sélection de la date \[beau, utile, moderne, facile\] {.smaller .scrollable}
```{r, echo=TRUE, message=FALSE, warning=FALSE }
library(plotly)
gplotly <- map(
selected[1:4],
~(g %+% theme(text = element_text(family = "arial", size = 9))
%+% scale_y_continuous()
%+% (CMOr |> filter(variable == .x))) |>
plotly::ggplotly(tooltip = "text", dynamicTicks = TRUE) |>
layout(
annotations = list(list(x=0, y=3,
showarrow=FALSE, text=.x,
xref="x", yref="y", align="left")),
yaxis = list(fixedrange=FALSE, type="log", tick0 = 50, dtick = "L50"),
showlegend = FALSE,
xaxis = list(fixedrange=TRUE, rangeslider=list(visible=TRUE)),
hovermode = "x unified"))
subplot(gplotly,
shareX = TRUE, shareY = TRUE,
nrows=2,
titleY = 1) |>
layout(yaxis = list(type= "log", dtick=2)) |>
config(displayModeBar=FALSE)
```
::: {style="color: gray; font-size: 14pt"}
sources: www.worldbank.org/en/research/commodity-markets, logscale, alt-click pour zoomer
:::
## ou le retour des spaghettis ! {.smaller .scrollable}
```{r, echo=TRUE, message=FALSE, warning=FALSE}
library(plotly)
(ggplot(CMOr |> filter(variable %in% selected))+
geom_line(
aes(x=time,
y=pcom_real,
color=variable,
group=variable,
text=text),
show.legend = FALSE, linewidth=0.25)+
xlab("")+ylab("")+
labs(
title = "",
subtitle = "",
caption = caption) +
theme_ofce(base_family="Arial", base_size=9)+
scale_x_date(
guide="axis_minor",
date_minor_breaks = "1 year",
date_labels = "%Y") )|>
plotly::ggplotly(tooltip = "text", dynamicTicks = TRUE) |>
layout(
yaxis = list(fixedrange=FALSE,
type="log",
tick0 = 50, dtick = "L50"),
showlegend = TRUE,
xaxis = list(fixedrange=TRUE,
rangeslider=list(visible=TRUE)),
hovermode = "x unified") |>
config(displayModeBar=FALSE)
```
::: {style="color: gray; font-size: 14pt"}
:::
## + sélection par légende \[beau, utile, moderne, facile\]
```{r, echo=TRUE, message=FALSE, warning=FALSE}
load("data/comday.rdata")
ggplotly(comday, tooltip = "text",
width = 1000,
height = 600) |>
layout(showlegend=TRUE,
legend = list(x=0, y=-0.1, orientation="h"),
xaxis = list(fixedrange=TRUE, rangeslider=list(visible=TRUE)),
yaxis = list(fixedrange=TRUE),
hovermode = "x unified") |>
config(displayModeBar=FALSE)
```
::: {style="color: gray; font-size: 14pt"}
sources: eikon&Datastream
:::
## + on simplifie par légende \[beau, utile, moderne, facile\]
```{r, echo=TRUE, message=FALSE, warning=FALSE}
load("data/comday.rdata")
ggplotly(comday, tooltip = "text",
width = 1000,
height = 600) |>
layout(showlegend=TRUE,
legend = list(x=0, y=-0.1, orientation="h"),
xaxis = list(fixedrange=FALSE),
yaxis = list(fixedrange=TRUE),
hovermode = "closest") |>
config(displayModeBar=FALSE)
```
::: {style="color: gray; font-size: 14pt"}
sources: eikon&Datastream
:::
## références
- cette présentation : [xtimbeau/quartopres (github.com)](https://github.com/xtimbeau/quartopres), public
- l'aide de quarto est le point de départ : [quarto](https://quarto.org/docs)
- l'interactivité repose sur des *htmlwigets* pour R :
- [Leaflet](http://rstudio.github.io/leaflet/) (cartes, tmap utilise leaflet),
- [Plotly](https://plot.ly/r/) (démontrés dans cette présentation),
- [dygraphs](http://rstudio.github.io/dygraphs/) (alternative pour les séries temporelles),
- [threejs](https://bwlewis.github.io/rthreejs/) (avec des possibilités spectaculaires)
- shiny peut être également le moteur de l'interactivité
- fonctionnel, mais quelques réglages à opérer
- Le livre (gratuit) [Interactive web-based data visualization with R, plotly, and shiny](https://plotly-r.com/index.html)
{fig-align="right" width="105"}