-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractivegraphics.Rmd
More file actions
395 lines (330 loc) · 12.6 KB
/
interactivegraphics.Rmd
File metadata and controls
395 lines (330 loc) · 12.6 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
---
title: "Interactive Graphics"
author: "Elizabeth Borgognoni Souto"
date: "`r Sys.Date()`"
output:
xaringan::moon_reader:
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
---
## Pre-requisites
To run this tutorial, you'll need:
- Packages: dplyr, tydir, broom, ggplot2, plotly, highcharter
```{r, eval=FALSE}
packages = c('dplyr', 'tydir', 'broom', 'ggplot2', 'plotly', 'highcharter')
install.packages(packages)
```
---
## Packages to Interactive Graphics
- The `plotly` is R package for creating interactive web-based graphs via plotly's JavaScript graphing library, plotly.js. The `ploty` R library contains a function ggplotly which will convert ggplot2 figures into graphs drawn with plotly.js which can be saved to your online plotly account or rendered locally.
- The `highcharter` package enables the creation of Highcharts type plots within R. `highcharter` provides a rich R interface to the popular Highcharts J. The functions of `highcharter` use standar evaluation. Recommended to make the final graph instead of using the package to visually explore the data.
```{r packages, message = FALSE}
library("dplyr") # for data manipulation
library("tidyr") # for data manipulation
library("broom") # for data statistical manipulation
library("ggplot2") # for plotting power curves
library("plotly") # for interactive power curves
library("highcharter") # for interactive power curves
```
---
## Plotly
Benefits:
- Using with shiny
- 100% Free and Open-Source, Forever (MIT license)
- Statistical and engineering charts
- Plotly’s ggplot2 converter turns ggplot2 plots into interactive, web-based plots. You can control the tooltip.
- Plotly objects are data frames with a class of plotly and an environment that tracks the mapping from data to visual properties.
- Plotly also supports interactive maps
- Plotly offline using the Plotly R client, Shiny and RStudio
Negative points
- Complicated to work with a large volume of data
- When use ggplot2 with plotly, ggplot2 doesn't know how to deal with data of class numeric
---
See more in :
[https://plot.ly/](About plotly)
[https://plot.ly/ggplot2/](About plotly with ggplot2)
---
## Using Plotly with ggplot2
```{r, eval=FALSE}
set.seed(1234)
dat <- data.frame(
cond = factor(rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1)))
mytheme <- theme_dark() +
theme(text = element_text(colour="blue"), axis.title = element_text(size = rel(1.25)))
#creating my own theme
p<-ggplot(dat, aes(x = cond, y = rating, fill = cond)) +
geom_boxplot() +
ggtitle("Box Plot") +
labs(fill="Condition") +
scale_fill_brewer(palette="Dark2") +
geom_text(label = 0) + annotate("text", label = "outlier", x= dat$cond[20], y = 2.6, size = 3, colour = "blue") +
##add name to the outlier
mytheme
ggplotly(p)
```
---
```{r, echo=FALSE}
set.seed(1234)
dat <- data.frame(
cond = factor(rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1)))
mytheme <- theme_dark() +
theme(text = element_text(colour="blue"), axis.title = element_text(size = rel(1.25)))
#creating my own theme
p<-ggplot(dat, aes(x = cond, y = rating, fill = cond)) +
geom_boxplot() +
ggtitle("Box Plot") +
labs(fill="Condition") +
scale_fill_brewer(palette="Dark2") +
geom_text(label = 0) + annotate("text", label = "outlier", x= dat$cond[20], y = 2.6, size = 3, colour = "blue") +
##add name to the outlier
mytheme
ggplotly(p)
```
---
Box plot with Jittered Points
```{r, eval = FALSE}
set.seed(1234)
dat<- data.frame(
cond = (rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1))
)
p<-ggplot(dat,aes(x = cond, y = rating, fill = cond)) +
geom_boxplot() +
ggtitle("Box plot") +
labs(fill="Condition") +
scale_fill_brewer(palette="Accent") +
geom_point(position = position_jitter(width = 0.2))+
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
ggplotly(p)
```
---
```{r, echo = FALSE}
set.seed(1234)
dat<- data.frame(
cond = (rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1))
)
p<-ggplot(dat,aes(x = cond, y = rating, fill = cond)) +
geom_boxplot() +
ggtitle("Box plot") +
labs(fill="Condition") +
scale_fill_brewer(palette="Accent") +
geom_point(position = position_jitter(width = 0.2)) +
theme_bw() +
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank())
ggplotly(p)
```
---
## Same example without ggplot2
```{r, eval=FALSE}
set.seed(1234)
a<-rnorm(50)
a2 <- rnorm(50, 1)
plot_ly(y = a, type = 'box') %>%
add_trace(y = a2) %>%
layout(title = 'Box Plot',xaxis = list(title = "cond", showgrid = F), yaxis = list(title = "rating"),
annotations = list(
x = -0.01,
y = boxplot.stats(a)$out,
text = "Outlier",
showarrow = FALSE,
xanchor = "right"
))
```
---
```{r, echo=FALSE}
set.seed(1234)
a<-rnorm(50)
a2 <- rnorm(50, 1)
plot_ly(y = a, type = 'box') %>%
add_trace(y = a2) %>%
layout(title = 'Box Plot',xaxis = list(title = "cond", showgrid = F), yaxis = list(title = "rating"),
annotations = list(
x = -0.01,
y = boxplot.stats(a)$out,
text = "Outlier",
showarrow = FALSE,
xanchor = "right"
))
```
---
##Highcharter
Benefits:
- Using with shiny
- Plots are built like ggplot2 by layering, although they use the pipe operator (%>%) instead of +
- It is possible to configure your plots with pre-implemented themes like Economist, Financial Times, Google, and FiveThirtyEight among others
- There are some Plugins: Motion, drag points, fontawesome, url-pattern, annotations.
- Has more tools than plotly
- The posibility to create or modify themes and customize in every way your chart: beatiful tooltips, titles, credits, legends, add plotlines or plotbands.
Negative points:
- Because has more tools is more difficult, sometimes you need to use another package to modificate your data
- The grapich is interactive but doesn't have the bar with tools like plotly
---
See more in:
[https://www.rstudio.com/rviews/2016/10/19/creating-interactive-plots-with-r-and-highcharts/](Examples Highcharts)
[http://jkunst.com/highcharter/](About Highcharts)
[https://github.com/jbkunst/highcharter/blob/master/vignettes/charting-data-frames.Rmd](News Highcharter)
---
##Using Highcharter
Start with empty chart and add components
```{r, eval = FALSE}
set.seed(1234)
dat <- data.frame(
cond = (rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1))
)
hcboxplot(x = dat$rating, var = dat$cond,name = "cond", showInLegend = TRUE) %>%
hc_chart(type = "column") %>%
hc_title(text = "Box Plot")
```
---
```{r, echo = FALSE}
set.seed(1234)
dat <- data.frame(
cond = (rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1))
)
hcboxplot(x = dat$rating, var = dat$cond, name = "cond", showInLegend = TRUE) %>%
hc_chart(type = "column") %>%
hc_title(text = "Box Plot")
```
---
##It's possible modificate themes, colors and modificate the legend
```{r, eval = FALSE}
set.seed(1234)
dat <- data.frame(
cond = (rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1))
)
highchart() %>%
hc_title(text = "Box Plot") %>%
hcboxplot(x = dat$rating, var = dat$cond, name = "cond", showInLegend = TRUE) %>%
hc_chart(type = "column") %>%
hc_legend(align = "right", verticalAlign = "top",
layout = "vertical", x = 0, y = 100, enable = TRUE ) %>%
hc_add_theme(hc_theme_sandsignika())
```
---
```{r, echo = FALSE}
set.seed(1234)
dat <- data.frame(
cond = (rep(c("trace 0", "trace 1"), each = 50)),
rating = c(rnorm(50), rnorm(50, mean = 1))
)
hcboxplot(x = dat$rating, var = dat$cond, name = "cond", showInLegend = TRUE) %>%
hc_chart(type = "column") %>%
hc_legend(align = "right", verticalAlign = "top",
layout = "vertical", x = 0, y = 100, enable = TRUE ) %>%
hc_title(text = "Box Plot") %>%
hc_add_theme(hc_theme_sandsignika())
```
---
##Plotly with ggplot, geom_smooth Linear Regression
```{r, eval = FALSE}
set.seed(1234)
dat <- data.frame(cond = rep(c("A", "B"), each=50),
xvar = 1:50 + rnorm(50,sd=3),
yvar = 1:50 + rnorm(50,sd=3))
p<-ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1, colour = "red") +
geom_smooth(method=lm) +
ggtitle("Linear Regression")
ggplotly(p)
```
---
```{r, echo = FALSE}
set.seed(1234)
dat <- data.frame(cond = rep(c("A", "B"), each=50),
xvar = 1:50 + rnorm(50,sd=3),
yvar = 1:50 + rnorm(50,sd=3))
p<-ggplot(dat, aes(x=xvar, y=yvar), colour="blue") +
geom_point(shape=1, colour = "red") +
geom_smooth(method=lm) +
ggtitle("Linear Regression")
ggplotly(p)
```
---
##Linear regression using highcharts
```{r, eval = FALSE}
set.seed(1234)
dat <- data.frame(cond = rep(c("A", "B"), each=50),
xvar = 1:50 + rnorm(50,sd=3),
yvar = 1:50 + rnorm(50,sd=3))
highchart() %>%
hc_add_series(dat, "point", hcaes(xvar, yvar, group = cond), regression = TRUE) %>%
hc_add_series(lm(yvar ~ xvar, data = dat), name = "Regression") %>%
hc_add_theme(hc_theme_google())
```
---
```{r, echo = FALSE}
set.seed(1234)
dat <- data.frame(cond = rep(c("A", "B"), each=50),
xvar = 1:50 + rnorm(50,sd=3),
yvar = 1:50 + rnorm(50,sd=3))
highchart() %>%
hc_add_series(dat, "point", hcaes(xvar, yvar, group = cond), regression = TRUE) %>%
hc_add_series(lm(yvar ~ xvar, data = dat), name = "Regression") %>%
hc_add_theme(hc_theme_google())
```
---
##Last Example
##three clusters, within which points are distributed according to a multivariate gaussian
Plotly
```{r, eval = FALSE}
library("dplyr")
set.seed(1234)
centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2))
points <- centers %>% group_by(cluster) %>%
do(data.frame(x1=rnorm(.$size[1], .$x1[1]),
x2=rnorm(.$size[1], .$x2[1])))
p<-ggplot(points, aes(x1, x2, color=cluster)) + geom_point() + ggtitle("Multivariate gaussian")
ggplotly(p)
```
---
```{r, echo = FALSE}
library("dplyr")
set.seed(1234)
centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2))
points <- centers %>% group_by(cluster) %>%
do(data.frame(x1=rnorm(.$size[1], .$x1[1]),
x2=rnorm(.$size[1], .$x2[1])))
p<-ggplot(points, aes(x1, x2, color=cluster)) + geom_point() + ggtitle("Multivariate gaussian")
ggplotly(p)
```
---
##Last Example
Highcharter
```{r, eval = FALSE}
library("dplyr")
set.seed(1234)
centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2))
points <- centers %>% group_by(cluster) %>%
do(data.frame(x1=rnorm(.$size[1], .$x1[1]),
x2=rnorm(.$size[1], .$x2[1])))
highchart() %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_title(text = "Multivariate gaussian") %>%
hc_add_series(points, type = "scatter", hcaes(x = x1, y = x2, group = cluster), showInLegend = TRUE)
```
---
```{r, echo = FALSE}
library("dplyr")
set.seed(1234)
centers <- data.frame(cluster=factor(1:3), size=c(100, 150, 50), x1=c(5, 0, -3), x2=c(-1, 1, -2))
points <- centers %>% group_by(cluster) %>%
do(data.frame(x1=rnorm(.$size[1], .$x1[1]),
x2=rnorm(.$size[1], .$x2[1])))
highchart() %>%
hc_add_theme(hc_theme_smpl()) %>%
hc_title(text = "Multivariate gaussian") %>%
hc_add_series(points, type = "scatter", hcaes(x = x1, y = x2, group = cluster), showInLegend = TRUE)
```
---
Therefore, we note that the two packages are good each with its benefits and negatives points. It concludes that the use of each of them depends on the type of analysis that is done and what you want to demonstrate in your chart. Highcharter has many interesting themes that act on the beauty of the chart, its graphics are geared more to the final graph and not to analysis. Thus it is recommended to analyze your data first with the ggplot2 package, and then transform into interactive graph. The problem of plotly is that sometimes with the amount of graph data volume it does not support interactivity. However, plotly has more graphics focused on statistics than highcharter. The other packages presented are ways to improve your data and arrange them to take less work when plotting the graph. Especially when use highcharter to make statistical charts. The latest highchart update was very promising, focused on the style of the ggplot2 package and with regression graphs.