-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.R
142 lines (116 loc) · 5.34 KB
/
index.R
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
## ---- include = FALSE----------------------------------------------------
#> knitr::opts_chunk$set(
#> message = FALSE,
#> fig.width = 10,
#> fig.height = 4,
#> comment = "#>",
#> collapse = TRUE,
#> eval = FALSE
#> )
## ---- eval = TRUE--------------------------------------------------------
library(plotly)
txhousing
## ------------------------------------------------------------------------
#> Abilene <- subset(txhousing, city == "Abilene")
#> p <- ggplot(Abilene, aes(month, sales, group = year)) +
#> geom_line()
#> ggplotly(p, dynamicTicks = "x")
## ------------------------------------------------------------------------
#> p <- ggplot(Abilene, aes(month, sales, frame = year)) +
#> geom_line()
#> ggplotly(p)
## ------------------------------------------------------------------------
#> p <- plot_ly(Abilene, x = ~month, y = ~log(sales), frame = ~year, showlegend = F)
#> add_lines(p)
## ------------------------------------------------------------------------
#> p <- ggplot(Abilene, aes(month, sales)) +
#> geom_line(aes(group = year), alpha = 0.2) +
#> geom_line(aes(frame = year), color = "red")
#> ggplotly(p)
## ------------------------------------------------------------------------
#> animation_opts(
#> ggplotly(p), frame = 1000, easing = "elastic"
#> )
## ------------------------------------------------------------------------
#> animation_button(
#> ggplotly(p), x = 1, xanchor = "right", y = 1, yanchor = "middle"
#> )
## ------------------------------------------------------------------------
#> animation_slider(
#> ggplotly(p), currentvalue = list(prefix = "YEAR ", font = list(color = "red"))
#> )
## ------------------------------------------------------------------------
#> data(gapminder, package = "gapminder")
#> gg <- ggplot(gapminder, aes(gdpPercap, lifeExp, color = continent, size = pop)) +
#> geom_point(alpha = 0.1) +
#> geom_point(aes(frame = year, ids = country)) +
#> scale_x_log10()
#> ggplotly(gg) %>% animation_opts(1000, redraw = FALSE)
## ------------------------------------------------------------------------
#> library(crosstalk)
#> d <- SharedData$new(Abilene, ~year)
#> p <- ggplot(d, aes(month, sales)) +
#> geom_line(aes(group = year))
#> ggplotly(p, tooltip = "year")
## ------------------------------------------------------------------------
#> d <- subset(txhousing, city %in% c("Galveston", "Midland", "Odessa", "South Padre Island"))
#> sd <- SharedData$new(d, ~year)
#> p <- ggplot(sd, aes(month, median, group = year)) + geom_line() +
#> facet_wrap(~city, ncol = 2)
#> (gg <- ggplotly(p, tooltip = "year"))
## ------------------------------------------------------------------------
#> txhousing %>%
#> select(median, volume, listings, inventory) %>%
#> GGally::ggpairs() %>% ggplotly() %>% highlight("plotly_selected")
## ------------------------------------------------------------------------
#> highlight(gg, "plotly_hover")
## ------------------------------------------------------------------------
#> highlight(gg, defaultValues = "2006")
## ------------------------------------------------------------------------
#> highlight(
#> gg, dynamic = TRUE, persistent = TRUE, selectize = TRUE
#> )
## ------------------------------------------------------------------------
#> highlight(
#> gg, dynamic = TRUE, persistent = TRUE,
#> selected = attrs_selected(mode = "markers+lines", marker = list(symbol = "x"))
#> )
## ------------------------------------------------------------------------
#> demo("highlight-pipeline", package = "plotly")
## ------------------------------------------------------------------------
#> demo("highlight-binned-target", package = "plotly")
## ------------------------------------------------------------------------
#> demo("highlight-binned-target", package = "plotly")
## ------------------------------------------------------------------------
#> a <- SharedData$new(Abilene, ~month)
#> p <- ggplot(a, aes(month, sales, frame = year)) +
#> geom_line() + geom_point()
#> highlight(ggplotly(p), "plotly_selected")
## ------------------------------------------------------------------------
#> g <- SharedData$new(gapminder, ~continent)
#> gg <- ggplot(g, aes(gdpPercap, lifeExp, color = continent, frame = year)) +
#> geom_point(aes(size = pop, ids = country)) +
#> geom_smooth(se = FALSE, method = "lm")
#> ggplotly(gg + scale_x_log10())
## ------------------------------------------------------------------------
#> demo("tour-USArrests", package = "plotly")
## ------------------------------------------------------------------------
#> tx <- SharedData$new(txhousing)
#> widgets <- bscols(widths = c(12, 12, 12),
#> filter_select("city", "Cities", tx, ~city),
#> filter_slider("sales", "Sales", tx, ~sales),
#> filter_checkbox("year", "Years", tx, ~year, inline = TRUE)
#> )
#> widgets
## ------------------------------------------------------------------------
#> bscols(
#> widths = c(4, 8), widgets,
#> plot_ly(tx, x = ~date, y = ~median, showlegend = FALSE) %>%
#> add_lines(color = ~city, colors = "black", connectgaps = FALSE) # bug in plotly.js?
#> )
## ------------------------------------------------------------------------
#> library(leaflet)
#> sd <- SharedData$new(quakes)
#> p <- plot_ly(sd, x = ~depth, y = ~mag) %>% add_markers(alpha = 0.5) %>% highlight("plotly_selected")
#> map <- leaflet(sd) %>% addTiles() %>% addCircles()
#> bscols(p, map)