plotly_data: Obtain data associated with a plotly graph

Description Usage Arguments Examples

View source: R/plotly_data.R

Description

plotly_data() returns data associated with a plotly visualization (if there are multiple data frames, by default, it returns the most recent one).

Usage

 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
plotly_data(p, id = p$x$cur_data)

## S3 method for class 'plotly'
groups(x)

## S3 method for class 'plotly'
ungroup(x, ...)

## S3 method for class 'plotly'
group_by(.data, ...)

## S3 method for class 'plotly'
mutate(.data, ...)

## S3 method for class 'plotly'
do(.data, ...)

## S3 method for class 'plotly'
summarise(.data, ...)

## S3 method for class 'plotly'
arrange(.data, ...)

## S3 method for class 'plotly'
select(.data, ...)

## S3 method for class 'plotly'
filter(.data, ...)

## S3 method for class 'plotly'
distinct(.data, ...)

## S3 method for class 'plotly'
slice(.data, ...)

## S3 method for class 'plotly'
rename(.data, ...)

## S3 method for class 'plotly'
transmute(.data, ...)

## S3 method for class 'plotly'
group_by_(.data, ...)

## S3 method for class 'plotly'
mutate_(.data, ...)

## S3 method for class 'plotly'
do_(.data, ...)

## S3 method for class 'plotly'
summarise_(.data, ...)

## S3 method for class 'plotly'
arrange_(.data, ...)

## S3 method for class 'plotly'
select_(.data, ...)

## S3 method for class 'plotly'
filter_(.data, ...)

## S3 method for class 'plotly'
distinct_(.data, ...)

## S3 method for class 'plotly'
slice_(.data, ...)

## S3 method for class 'plotly'
rename_(.data, ...)

## S3 method for class 'plotly'
transmute_(.data, ...)

Arguments

p

a plotly visualization.

id

a character string or number referencing an "attribute layer".

x

a plotly visualization.

...

arguments passed onto the relevant method.

.data

a plotly visualization.

Examples

 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
# use group_by() to define groups of visual markings
p <- txhousing %>%
  group_by(city) %>%
  plot_ly(x = ~date, y = ~sales)
p

# plotly objects preserve data groupings 
groups(p)
plotly_data(p)

# dplyr verbs operate on plotly objects as if they were data frames
p <- economics %>%
  plot_ly(x = ~date, y = ~unemploy / pop) %>%
  add_lines() %>%
  mutate(rate = unemploy / pop) %>% 
  filter(rate == max(rate))
plotly_data(p)
add_markers(p)
layout(p, annotations = list(x = ~date, y = ~rate, text = "peak"))

# use group_by() + do() + subplot() for trellis displays 
d <- group_by(mpg, drv)
plots <- do(d, p = plot_ly(., x = ~cty, name = ~drv))
subplot(plots[["p"]], nrows = 3, shareX = TRUE)

# arrange displays by their mean
means <- summarise(d, mn = mean(cty, na.rm = TRUE))
means %>%
  dplyr::left_join(plots) %>%
  arrange(mn) %>%
  subplot(nrows = NROW(.), shareX = TRUE)
  
# more dplyr verbs applied to plotly objects
p <- mtcars %>%
  plot_ly(x = ~wt, y = ~mpg, name = "scatter trace") %>%
  add_markers()
p %>% slice(1) %>% plotly_data()
p %>% slice(1) %>% add_markers(name = "first observation")
p %>% filter(cyl == 4) %>% plotly_data()
p %>% filter(cyl == 4) %>% add_markers(name = "four cylinders")

plotly documentation built on Oct. 10, 2021, 1:06 a.m.