Skip to content

Commit 8d71a24

Browse files
committed
adopt use of highlight_unit() in demos where it makes sense
1 parent 0ba1c87 commit 8d71a24

21 files changed

+35
-53
lines changed

R/ggplotly.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@
4747
#' ggplotly(viz, tooltip = c("text", "size"))
4848
#'
4949
#' # linked scatterplot brushing
50-
#' library(crosstalk)
51-
#' d <- SharedData$new(mtcars)
50+
#' d <- highlight_unit(mtcars)
5251
#' qplot(data = d, x = mpg, y = wt) %>%
5352
#' subplot(qplot(data = d, x = mpg, y = vs)) %>%
5453
#' layout(title = "Click and drag to select points") %>%
@@ -59,7 +58,7 @@
5958
#' demo("crosstalk-highlight-ggplotly", package = "plotly")
6059
#'
6160
#' # client-side linked brushing in a scatterplot matrix
62-
#' SharedData$new(iris) %>%
61+
#' highlight_unit(iris) %>%
6362
#' GGally::ggpairs(aes(colour = Species), columns = 1:4) %>%
6463
#' ggplotly(tooltip = c("x", "y", "colour")) %>%
6564
#' highlight("plotly_selected")

data-raw/res-us.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ res <- sf::st_read("~/Downloads/tl_2017_us_aiannh/tl_2017_us_aiannh.shp")
99
# (2) crosstalk highlight should set fillcolor...
1010

1111
res %>%
12-
SharedData$new(~NAME) %>%
12+
highlight_unit(~NAME) %>%
1313
plot_ly(text = ~NAME) %>%
1414
highlight(selectize = TRUE, dynamic = TRUE)
1515

demo/animation-tour-USArrests.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ax <- list(
4545
# for nicely formatted slider labels
4646
options(digits = 3)
4747

48-
tour_dat <- crosstalk::SharedData$new(tour_dat, ~state, group = "A")
48+
tour_dat <- highlight_unit(tour_dat, ~state, group = "A")
4949

5050
tour <- proj_dat %>%
5151
plot_ly(x = ~x, y = ~y, frame = ~step, color = I("black")) %>%

demo/crosstalk-filter-dynamic-axis.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ library(tidyr)
33
library(crosstalk)
44

55
m <- gather(mtcars, variable, value, -vs)
6-
msd <- SharedData$new(m, ~variable)
6+
msd <- highlight_unit(m, ~variable)
77
gg <- ggplot(msd, aes(factor(vs), value)) +
88
geom_jitter(alpha = 0.3)
99

demo/crosstalk-filter-lines.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ library(ggplot2)
33
library(gapminder)
44
library(plotly)
55

6-
sd <- SharedData$new(gapminder)
6+
sd <- highlight_unit(gapminder)
77

88
g <- ggplot(sd, aes(year, lifeExp, color = country, group = country)) +
99
geom_line()

demo/crosstalk-highlight-binned-target-a.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# These examples demonstrate ways to display binned/aggregated selections
2-
library(crosstalk)
32
library(plotly)
43

5-
d <- SharedData$new(mpg)
4+
d <- highlight_unit(mpg)
65
dots <- plot_ly(d, colors = "Set1", color = ~class, x = ~displ, y = ~cyl) %>%
76
layout(
87
xaxis = list(title = "Engine displacement"),
Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
# These examples demonstrate ways to display binned/aggregated selections
2-
library(crosstalk)
32
library(plotly)
43

5-
d <- SharedData$new(mtcars)
4+
d <- highlight_unit(mtcars)
65
sp <- plot_ly(d, x = ~mpg, y = ~disp) %>%
76
add_markers(color = I("black"))
87

9-
# hist/box/violin are all 'statistical trace types' meaning
10-
# they compute aggregations on the fly
8+
# 'statistical trace types'
119
hist <- plot_ly(d, x = ~factor(cyl)) %>%
1210
add_histogram(color = I("black"))
1311
box <- plot_ly(d, y = ~disp, color = I("black")) %>%
@@ -22,7 +20,4 @@ subplot(sp, box, violin, shareY = TRUE, titleX = TRUE, titleY = TRUE) %>%
2220
title = "Click and drag scatterplot",
2321
showlegend = FALSE
2422
) %>%
25-
highlight(
26-
"plotly_selected",
27-
selected = attrs_selected(showlegend = FALSE)
28-
)
23+
highlight("plotly_selected")

demo/crosstalk-highlight-binned-target-c.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
# These examples demonstrate ways to display binned/aggregated selections
2-
library(crosstalk)
32
library(plotly)
43

5-
tx <- SharedData$new(txhousing, ~city)
4+
tx <- highlight_unit(txhousing, ~city)
65
p1 <- ggplot(tx, aes(date, median, group = city)) + geom_line() + xlab(NULL)
76
gg1 <- ggplotly(p1, tooltip = c("city", "date", "median"))
87
p2 <- plot_ly(tx, x = ~median, color = I("black")) %>%

demo/crosstalk-highlight-epl-2.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ library(engsoccerdata)
22
library(dplyr)
33
library(tidyr)
44
library(plotly)
5-
library(crosstalk)
65

76
# shape data into desired format
87
dat <- england %>%
@@ -21,7 +20,7 @@ dat <- england %>%
2120
mutate(meanP = mean(cumpts)) %>%
2221
filter(Season > 2006)
2322

24-
sd <- SharedData$new(dat, ~team, "Select a team")
23+
sd <- highlight_unit(dat, ~team, "Select a team")
2524

2625
# a 'wormchart' like fig 8 here http://www.gradaanwr.net/wp-content/uploads/2016/06/dataApr16.pdf
2726
p <- ggplot(sd, aes(x = gameno, y = cumpts - meanP)) +
@@ -33,7 +32,7 @@ p <- ggplot(sd, aes(x = gameno, y = cumpts - meanP)) +
3332
y = "Cumulative points (above/below) average"
3433
)
3534

36-
gg <- ggplotly(p, width = 1050, height = 600, tooltip = "team")
35+
gg <- ggplotly(p, tooltip = "team")
3736

3837
highlight(
3938
gg,

demo/crosstalk-highlight-epl.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ library(engsoccerdata)
22
library(dplyr)
33
library(tidyr)
44
library(plotly)
5-
library(crosstalk)
65

76
# shape data into desired format
87
dat <- england %>%
@@ -17,7 +16,7 @@ dat <- england %>%
1716
group_by(Season, team) %>%
1817
mutate(gameno = row_number(), cumpts = cumsum(pts))
1918

20-
sd <- SharedData$new(dat, ~Season, "Select a season")
19+
sd <- highlight_unit(dat, ~Season, "Select a season")
2120

2221
p <- ggplot(sd, aes(x = gameno, y = cumpts)) +
2322
geom_line(aes(color = Season, group = Season), alpha = 0.5) +

demo/crosstalk-highlight-ggpairs.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
library(GGally)
2-
d <- SharedData$new(iris)
3-
p <- GGally::ggpairs(d, aes(colour = Species), columns = 1:5)
4-
ggplotly(p) %>% highlight("plotly_selected")
2+
d <- highlight_unit(iris)
3+
p <- ggpairs(d, aes(colour = Species), columns = 1:5)
4+
ggplotly(p) %>%
5+
highlight("plotly_selected")

demo/crosstalk-highlight-ggplotly.R

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
library(plotly)
2-
library(crosstalk)
32

43
# see https://vimeo.com/202647310
5-
d <- SharedData$new(txhousing, ~city, "Select a city")
4+
d <- highlight_unit(txhousing, ~city, "Select a city")
65
p <- ggplot(d, aes(date, median, group = city)) + geom_line()
76
ggplotly(p, tooltip = "city") %>%
87
layout(title = "Click on a line to highlight a year") %>%
@@ -11,16 +10,15 @@ ggplotly(p, tooltip = "city") %>%
1110
# crosstalk keys are automatically added to the group aesthetic...
1211
# if you want to avoid adding the key to group for a layer,
1312
# use the original data
14-
sd <- SharedData$new(txhousing, ~city)
15-
p <- ggplot(sd, aes(month, median)) +
13+
p <- ggplot(d, aes(month, median)) +
1614
geom_line(aes(group = city)) +
1715
geom_smooth(data = txhousing, method = "gam") +
1816
facet_wrap(~ year)
1917
ggplotly(p) %>%
2018
layout(title = "Click on a line to highlight a year")
2119

2220
# perhaps a more useful example
23-
sd <- SharedData$new(txhousing, ~year)
21+
sd <- highlight_unit(txhousing, ~year)
2422
p <- ggplot(sd, aes(month, median)) +
2523
geom_line(aes(group = year)) +
2624
geom_smooth(data = txhousing, method = "gam") +

demo/crosstalk-highlight-intro.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
library(plotly)
2-
library(crosstalk)
32

43
nPatients <- 50
54
nVisits <- 10
@@ -11,7 +10,7 @@ df <- data.frame(
1110
)
1211

1312
# delare the patient variable as the "unit of interest"
14-
sd <- SharedData$new(df, ~patient)
13+
sd <- highlight_unit(df, ~patient)
1514

1615
p <- plot_ly(sd, x = ~visit, y = ~perc, color = I("black"),
1716
text = ~paste("Patient:", patient)) %>%

demo/crosstalk-highlight-leaflet.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ library(htmltools)
77
# leaflet should respect these "global" highlight() options
88
options(opacityDim = 0.5)
99

10-
sd <- SharedData$new(quakes)
10+
sd <- crosstalk_unit(quakes)
1111

1212
p <- plot_ly(sd, x = ~depth, y = ~mag) %>%
1313
add_markers(alpha = 0.5) %>%

demo/crosstalk-highlight-pipeline.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
library(plotly)
2-
library(crosstalk)
32

4-
sd <- SharedData$new(txhousing, ~city, "Select a city")
3+
sd <- highlight_unit(txhousing, ~city, "Select a city")
54

65
base <- plot_ly(sd, color = I("black"), height = 400) %>%
76
group_by(city)

demo/crosstalk-highlight-subplot.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
library(plotly)
2-
library(crosstalk)
32

4-
d <- SharedData$new(mtcars)
3+
d <- highlight_unit(mtcars)
54
s <- subplot(
65
qplot(data = d, x = mpg, y = wt),
76
qplot(data = d, x = mpg, y = vs)

demo/sf-dt.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
library(plotly)
2-
library(crosstalk)
32

43
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
5-
ncsd <- SharedData$new(nc)
4+
ncsd <- highlight_unit(nc)
65

76
map <- plot_ly(ncsd, split = ~NAME, color = I("gray"), hoveron = "fills") %>%
87
highlight(persistent = TRUE, color = "red", opacityDim = 1) %>%

demo/sf-mapbox-data.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plot_mapbox(res_mn, split = ~INDRESNAME, span = I(1))
1010
plot_mapbox(res_mn, split = ~INDRESNAME, color = ~AREA, stroke = ~PERIMETER, span = I(1))
1111

1212
# linking with DT
13-
mn <- crosstalk::SharedData$new(res_mn)
13+
mn <- highlight_unit(res_mn)
1414
bscols(
1515
plot_mapbox(mn, split = ~INDRESNAME, text = ~INDRESNAME, hoverinfo = "text", hoveron = "fills") %>%
1616
layout(title = "Click a reservation", showlegend = FALSE),
@@ -19,7 +19,7 @@ bscols(
1919

2020
# linking with plotly
2121
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
22-
ncsd <- SharedData$new(nc)
22+
ncsd <- highlight_unit(nc)
2323

2424
# note that brushing counties is currently possible with plot_ly(), but isn't quite working
2525
# yet with plot_mapbox() -- https://github.com/plotly/plotly.js/issues/2512

demo/sf-plotly-3D-globe.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
library(plotly)
22
library(dplyr)
3-
library(crosstalk)
43

54
# latitude, longitude, and altitiude of tropical storms
65
storms <- sf::st_read(system.file("shape/storms_xyz.shp", package = "sf"), quiet = TRUE)
@@ -43,7 +42,7 @@ globe <- plot_ly(height = 500) %>%
4342
hoverinfo = "none"
4443
) %>%
4544
add_sf(
46-
data = SharedData$new(storms, group = "storm paths"),
45+
data = highlight_unit(storms, group = "storm paths"),
4746
name = "storm paths",
4847
x = ~ 1.001 * cos(degrees2radians(x)) * cos(degrees2radians(y)),
4948
y = ~ 1.001 * sin(degrees2radians(x)) * cos(degrees2radians(y)),
@@ -98,7 +97,7 @@ distanceByAlt <- storms %>%
9897
group_by(L1) %>%
9998
mutate(dist = arc_dist(X, Y)) %>%
10099
rename(altitude = Z) %>%
101-
SharedData$new(~L1, group = "storm paths") %>%
100+
highlight_unit(~L1, group = "storm paths") %>%
102101
plot_ly(x = ~dist, y = ~altitude, height = 400) %>%
103102
# plotly.js doesn't support color gradient along *2D* lines
104103
add_lines(color = I("gray")) %>%

demo/sf-plotly-storms.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
library(htmltools)
2-
library(crosstalk)
1+
library(plotly)
32

43
storms <- sf::st_read(system.file("shape/storms_xyz.shp", package = "sf"), quiet = TRUE)
5-
stormz <- SharedData$new(storms)
4+
stormz <- highlight_unit(storms)
65

76
xy <- plot_ly(stormz, color = ~z, mode = "markers+lines", line = list(color = "gray"), hoverinfo = "none")
87
xz <- add_sf(xy, y = ~z, color = ~y)
@@ -61,5 +60,5 @@ xyz3D <- plot_ly(stormz, color = ~z, z = ~z) %>%
6160
)
6261

6362

64-
63+
library(htmltools)
6564
browsable(tagList(xyz2D, xyz3D))

man/ggplotly.Rd

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)