-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplotly_working_examples.Rmd
277 lines (231 loc) · 6.25 KB
/
plotly_working_examples.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
267
268
269
270
271
272
273
274
---
output:
html_document:
fig_caption: yes
fig_height: 2.5
fig_width: 4
highlight: zenburn
toc: yes
---
```{r setup, include=FALSE, cache=FALSE}
library(knitr)
# set global chunk options
opts_chunk$set(fig.path='figure/minimal-', fig.align='center', fig.show='hold', fig.height=2.5, fig.width=4, figerror=TRUE, plotly=TRUE, error=TRUE)
```
# Plotly, ggplot2, and R
[Plotly](http://www.plot.ly) is a cool, open-source project ( [github](https://github.com/plotly) ) to help people get interactive plots online easily through a number of various programming languages. This page serves as a guide to using it with [R's](http://www.r-project.org/) [ggplot2](http://ggplot2.org/) visualization and plotting package and will be updated as support for new geoms (ggplot plots/visualization types) are added.
Most examples will be taken straight from [ggplot's docs website](http://docs.ggplot2.org/current/). The master list of geoms is at the botttom of this post. Some of the plots have minor errors.
## Installing Plot.ly
Installing Plot.ly within R is easy.
```{r, plotly=TRUE, eval=FALSE}
#autoloads ggplot
library(devtools)
install_github("plotly", "ropensci")
signup("yourusername", "[email protected]")
```
```{r, echo=FALSE}
plotly.key = 'h4lbm5pbfx'
```
```{r}
library(plotly)
py <- plotly(username= 'xysmas', key=plotly.key)
```
# On With the Plots
<<<<<<< HEAD
=======
## geom_abline, hline, vline
```{r lines}
coefs <- coef(lm(Sepal.Length ~ Petal.Width, data = iris))
p.iris <- ggplot(iris, aes(Petal.Width, Sepal.Length, color = Species))
p.iris <- p.iris + geom_point()
p.iris.hline <- p.iris + geom_hline(yintercept=7)
p.iris.abline <- p.iris + geom_abline(intercept=coefs[1], slope=coefs[2], colour="red", size=2)
p.iris.vline <- p.iris + geom_vline(xintercept=2)
p.iris.abline
p.iris.hline
p.iris.vline
```
```{r figerror=TRUE}
py$ggplotly(p.iris.abline)
# odd little error
py$ggplotly(p.iris.hline)
# odd little error
py$ggplotly(p.iris.vline)
```
geom_abline doesn't work.
>>>>>>> FETCH_HEAD
## geom_jitter
```{r jitter}
p <- ggplot(iris, aes(Petal.Width, Sepal.Length, color = Species))
p + geom_jitter()
py$ggplotly(p+geom_jitter())
#try position instead
py$ggplotly(p + geom_point(position=position_jitter()))
```
geom_jitter fails, but position jitter passed to geompoint seems to work fine.
## geom_line
(potentially the most useless plot ever, but just as an example...)
```{r line}
p <- p + geom_line()
p
```
```{r}
py$ggplotly(p)
```
yes, funny that abline and the other line geoms do not work.
<<<<<<< HEAD
=======
## geom_path
```{r paths}
# Use the arrow parameter to add an arrow to the line
# See ?grid::arrow for more details
library(grid)
c <- ggplot(economics, aes(x = date, y = pop))
# Arrow defaults to "last"
c + geom_path(arrow = arrow())
py$ggplotly(c + geom_path(arrow = arrow()))
```
>>>>>>> FETCH_HEAD
## geom_point
Clearly this works.
## geom_polygon
```{r poly}
### example straight from ggplot2
# When using geom_polygon, you will typically need two data frames:
# one contains the coordinates of each polygon (positions), and the
# other the values associated with each polygon (values). An id
# variable links the two together
ids <- factor(c("1.1", "2.1", "1.2", "2.2", "1.3", "2.3"))
values <- data.frame(
id = ids,
value = c(3, 3.1, 3.1, 3.2, 3.15, 3.5)
)
positions <- data.frame(
id = rep(ids, each = 4),
x = c(2, 1, 1.1, 2.2, 1, 0, 0.3, 1.1, 2.2, 1.1, 1.2, 2.5, 1.1, 0.3,
0.5, 1.2, 2.5, 1.2, 1.3, 2.7, 1.2, 0.5, 0.6, 1.3),
y = c(-0.5, 0, 1, 0.5, 0, 0.5, 1.5, 1, 0.5, 1, 2.1, 1.7, 1, 1.5,
2.2, 2.1, 1.7, 2.1, 3.2, 2.8, 2.1, 2.2, 3.3, 3.2)
)
# Currently we need to manually merge the two together
datapoly <- merge(values, positions, by=c("id"))
(p <- ggplot(datapoly, aes(x=x, y=y)) + geom_polygon(aes(fill=value, group=id)))
py$ggplotly(p)
rm(p)
```
<<<<<<< HEAD
=======
## geom_ribbon
```{r ribbon}
msamp <- movies[sample(nrow(movies), 1000), ]
m <- ggplot(msamp, aes(y=log(votes), x=year))
m <- m + geom_point()
m <- m + stat_summary(geom="ribbon", fun.ymin="min", fun.ymax="max")
m
py$ggplotly(m)
```
Points work, stat summary doesn't.
>>>>>>> FETCH_HEAD
## geom_segment
example from ggplot docs
```{r seg}
library(grid) # needed for arrow function
p <- ggplot(seals, aes(x = long, y = lat))
p <- p + geom_segment(aes(xend = long + delta_long, yend = lat + delta_lat), arrow = arrow(length = unit(0.1,"cm")))
p
py$ggplotly(p)
```
# master list of geoms:
[ggplot2 docs](http://docs.ggplot2.org/current/)
### geoms
* geom_abline() = FALSE
* geom_area() = FALSE
* geom_bar() = FALSE
* geom_bin2d() = FALSE
* geom_blank() =
* geom_boxplot() = FALSE
* geom_contour() = FALSE
* geom_crossbar() = FALSE
* geom_density() = FALSE
* geom_density2d() = FALSE
* geom_dotplot() = FALSE
* geom_errorbar() = FALSE
* geom_errorbarh() = FALSE
* geom_freqpoly() = FALSE
* geom_hex() = Untested
* geom_histogram() = FALSE
* geom_hline() = FALSE
* geom_jitter() = FALSE
* geom_line() = TRUE
* geom_linerange() = FALSE
* geom_map() = FALSE
* geom_path() = TRUE
* geom_point() = TRUE
* geom_pointrange() = FALSE
* geom_polygon() =TRUE
* geom_quantile() = FALSE
* geom_raster() = FALSE
* geom_rect() = FALSE
* geom_ribbon() = FALSE
* geom_rug() = FALSE
* geom_segment() = TRUE
* geom_smooth() = FALSE
* geom_step() = FALSE
* geom_text() = FALSE
* geom_tile() = FALSE
* geom_violin() = FALSE
* geom_vline() = FALSE
### stats
* stat_abline() = FALSE
* stat_bin()
* stat_bin2d()
* stat_bindot()
* stat_bindot()
* stat_binhex()
* stat_contour()
* stat_density()
* stat_density2d()
* stat_ecdf()
* stat_function()
* stat_hline()
* stat_identity()
* stat_qq()
* stat_quantile() = false
* stat_smooth()
* stat_spoke()
* stat_sum()
* stat_summary()
* stat_summary_hex()
* stat_summary2d()
* stat_unique()
* stat_vline()
* stat_ydensity()
### other ggplot stuff
* expand_limits
* guide_legend
* guide_colourbar(guide_colorbar)
* scale_alpha(scale_alpha_continuous, scale_alpha_discrete)
* scale_area()
* scale_color_brewer
* scale_color_continuous
* scale_color_discrete
* scale_color_gradient
* scale_color_gradient2
* scale_color_gradientn
* coord_cartesian
* coord_equal
* coord_fixed
* coord_flip
* coord_map = FALSE
* coord_polar
* coord_trans
*
* facet_grid
* facet_null
* facet_wrap
*
* position_dodge
* position_fill
* position_identity
* position_jitter
* position_stack