-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.rmd
266 lines (169 loc) · 5.2 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
---
title: "Interactive Graphics in R"
subtitle: "<a href='https://plot.ly/r/'>with Plotly</a>"
author: "Morten Andersen & Andreas Hald Espensen"
date: "2019-04-23 (updated: `r Sys.Date()`)"
output:
xaringan::moon_reader:
#chakra: libs/remark-latest.min.js
css: [default, default-fonts, custom.css]
lib_dir: libs
nature:
highlightStyle: monokai-sublime
highlightLines: true
countIncrementalSlides: false
ratio: "16:9"
---
# Basics
Plotly in R website: https://plot.ly/r/
--
"Plotly is an R package for creating interactive web-based graphs via the open source JavaScript graphing library [plotly.js](https://plot.ly/javascript/)."
--
Rendered locally through the [htmlwidgets](http://www.htmlwidgets.org/) framework.
--
**Installation**:
```{r echo=TRUE, message=FALSE, warning=FALSE}
if (!require("plotly")) install.packages("plotly")
library(plotly)
```
--
**Getting Started**: https://plot.ly/r/getting-started/
--
*How did we come across the Plotly library?*
???
By no means exhaustive. Only an introduction to plotly.
Working on shiny apps as a Student Assistant at Grundfos' Data Analytics department.
---
# Creating a Plot
`plot_ly`: Maps R objects to plotly.js.
**Usage**:
```{r eval=FALSE}
plot_ly(data = data.frame(), ..., type = NULL, name, color, colors = NULL,
alpha = NULL, stroke, strokes = NULL, alpha_stroke = 1, size,
sizes = c(10, 100), span, spans = c(1, 20), symbol, symbols = NULL,
linetype, linetypes = NULL, split, frame, width = NULL, height = NULL,
source = "A")
```
`data`: Data frame (optional)
`...`: Arguments passed along to the trace type. E.g. `x = 1:10`, `x = ~Time`, `y`, etc.
???
---
# Creating a Plot - Let's see it in action!
```{r echo=TRUE, fig.height=5.5, fig.width=12}
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p
```
???
`Midwest` data set: Demographic information of midwest counties.
`percollege`: Percent college educated.
---
# Connection with ggplot2
Contains the function `ggplotly` which will convert `ggplot2` figures into a Plotly object.
--
You have the option to manipulate the object with the `style` function.
--
More information at: https://plot.ly/ggplot2/extending-ggplotly/
???
---
# Connection with ggplot2
```{r fig.height=5.5, fig.width=12}
p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line()
p
```
???
`gold`: Gold price time series - Daily morning gold prices in US dollars. 1 January 1985 - 31 March 1989.
---
# Connection with ggplot2
```{r fig.height=5.5, fig.width=12}
p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line()
gg <- ggplotly(p)
gg
```
???
---
# Connection with ggplot2
```{r fig.height=5.5, fig.width=12}
p <- ggplot(fortify(forecast::gold), aes(x, y)) + geom_line()
gg <- ggplotly(p)
gg <- style(gg, line = list(color = 'gold'), hoverinfo = "y", traces = 1)
gg
```
???
---
# Cheatsheet + Cookbook
Handy cheatsheet available at: https://images.plot.ly/plotly-documentation/images/r_cheat_sheet.pdf.
--
Cookbook available at: https://plotly-r.com/index.html
???
---
# Creating a Plot
```{r fig.height=5.5, fig.width=12}
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, trace_0, trace_1, trace_2)
head(data)
```
???
---
# Creating a Plot
```{r fig.height=4.5, fig.width=12}
p <- plot_ly(data, x = ~x, y = ~trace_0, name = 'trace 0', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') %>%
add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
#p <- p %>% layout(hovermode = 'compare')
p
```
???
---
# Creating a Plot
```{r fig.height=4.5, fig.width=12}
p_hist <- data %>% plot_ly(alpha = 0.6) %>%
add_histogram(x = ~trace_0) %>% add_histogram(x = ~trace_1) %>% add_histogram(x = ~trace_2) %>%
layout(barmode = "overlay", xaxis = list(title = "My x-axis title"))
p_hist
```
???
---
# Iris
```{r fig.height=4, fig.width=12}
pal <- c("red", "blue", "green")
p <- iris %>% plot_ly(x = ~Sepal.Length, y = ~Petal.Length, color = ~Species, colors = pal,
type = "scatter", mode = "markers", hoverinfo = 'text',
text = ~paste('</br> Species: ', Species,
'</br> Petal Lenght: ', Petal.Length,
'</br> Petal Width: ', Petal.Width))
p
```
???
---
# dygraphs for R
dygraphs in R website: https://rstudio.github.io/dygraphs/index.html
"The dygraphs package is an R interface to the dygraphs JavaScript charting library. It provides rich facilities for charting time-series data in R" (https://rstudio.github.io/dygraphs/index.html)
```{r echo=TRUE, message=FALSE, warning=FALSE}
if (!require("dygraphs")) install.packages("dygraphs")
library(dygraphs)
```
???
---
# dygraphs for R
```{r fig.height=5.5, fig.width=12}
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths) %>% dyRangeSelector()
```
???
---
# Custom plots
```{r fig.height=4.5, fig.width=12}
lungDeaths <- cbind(mdeaths, fdeaths)
dygraph(lungDeaths) %>%
dyRangeSelector() %>%
dyBarSeries('fdeaths') %>%
dyFilledLine('mdeaths')
```
???
---
class: center, middle
# Thank you!
Slides created via the [**xaringan**](https://github.com/yihui/xaringan) R package.