-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotly.Rmd
More file actions
114 lines (93 loc) · 3.06 KB
/
plotly.Rmd
File metadata and controls
114 lines (93 loc) · 3.06 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
---
title: Diving into Plotly with R
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(plotly)
set.seed(100)
```
---

[http://plot.ly](http://plot.ly)
---
## Plot.ly
- Data viz company. Trying to compete with likes of Tableau.
- Web service with a JavaScript plotting library at heart
- Cross platform interactive and editable charts
- Add basic interactivity with little overhead
- Great for exploration
- As supplementary content? Embedded in journal websites?
- plotly.js library open sourced late 2015
- Online editing, collaboration, dashboards etc.
- Excel online
- Many more esoteric features
- e.g. websockets
- Vector plots, can use pixels for better performance (WebGL)
- Can usually plot <1m 2d points without dramas
- Actively maintained client libraries for Python, R, Matlab & JS
- Free version has limitations - no vector graphic export
- But vector graphics aren't interactive?!
- Student pricing 5$/mo
---
*Some plots are posted to the web for processing, but most plotting is now implemented in the client, and inside RStudio everything is done offline*
---
## Interactive scatter plots
``` {r}
head(diamonds) # example dataset
```
#### Basic 2d scatter
```{r scatter1, echo=TRUE, message=FALSE}
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price)
```
#### 2d scatter with marker size, colour and custom labels
```{r scatter2, echo=TRUE, message=FALSE}
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d,
x = ~carat,
y = ~price,
color = ~carat,
size = ~carat,
text = ~paste("Price: ", price, '$<br>Cut:', cut))
# Lots of information easily accessible, not overwhelming
```
#### As above, but using plotly_POST to share charts (requires a plot.ly account)
```
d <- diamonds[sample(nrow(diamonds), 1000), ]
c <- plot_ly(d,
x = ~carat,
y = ~price,
color = ~carat,
size = ~carat,
text = ~paste("Price: ", price, '$<br>Cut:', cut))
chart_link = plotly_POST(c, filename="scatterer")
chart_link # returns URL of web chart
```
## Box and whisker
``` {r, message=FALSE}
plot_ly(diamonds, x = ~price, color = ~cut, type = "box")
```
## Bubble plots
## ggplotly()
- Free interactivity for ggplot()
``` {r, message=FALSE}
p <- ggplot(data = d, aes(x = carat, y = price)) +
geom_point(aes(text = paste("Clarity:", clarity))) +
geom_smooth(aes(colour = cut, fill = cut)) + facet_wrap(~ cut)
ggplotly(p)
```
## Chart type inference
- Instead of defaulting to a scatter trace, plot_ly() infers a sensible trace type based on provided data.
- e.g. if we supply a discrete variable to x (or y), we get a vertical (or horizontal) bar chart:
``` {r, message=FALSE}
subplot(
plot_ly(diamonds, y = ~cut, color = ~clarity),
plot_ly(diamonds, x = ~cut, color = ~clarity),
margin = 0.05
) %>% hide_legend()
```
Or send discrete variables to both X and Y:
``` {r, message=FALSE}
plot_ly(diamonds, x = ~cut, y = ~clarity)
```