Examples for 'plotly::plotly_data'


Obtain data associated with a plotly graph

Aliases: plotly_data groups.plotly ungroup.plotly group_by.plotly mutate.plotly do.plotly summarise.plotly arrange.plotly select.plotly filter.plotly distinct.plotly slice.plotly rename.plotly transmute.plotly group_by_.plotly mutate_.plotly do_.plotly summarise_.plotly arrange_.plotly select_.plotly filter_.plotly distinct_.plotly slice_.plotly rename_.plotly transmute_.plotly

Keywords:

### ** Examples


# use group_by() to define groups of visual markings
p <- txhousing %>%
  group_by(city) %>%
  plot_ly(x = ~date, y = ~sales)
p
No trace type specified:
  Based on info supplied, a 'scatter' trace seems appropriate.
  Read more about this trace type -> https://plotly.com/r/reference/#scatter
No scatter mode specifed:
  Setting the mode to markers
  Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode
Warning: Ignoring 568 observations
Error in loadNamespace(name): there is no package called 'webshot'
# plotly objects preserve data groupings 
groups(p)
[[1]]
city
plotly_data(p)
# A tibble: 8,602 × 9
   city     year month sales   volume median listings inventory  date
   <chr>   <int> <int> <dbl>    <dbl>  <dbl>    <dbl>     <dbl> <dbl>
 1 Abilene  2000     1    72  5380000  71400      701       6.3 2000 
 2 Abilene  2000     2    98  6505000  58700      746       6.6 2000.
 3 Abilene  2000     3   130  9285000  58100      784       6.8 2000.
 4 Abilene  2000     4    98  9730000  68600      785       6.9 2000.
 5 Abilene  2000     5   141 10590000  67300      794       6.8 2000.
 6 Abilene  2000     6   156 13910000  66900      780       6.6 2000.
 7 Abilene  2000     7   152 12635000  73500      742       6.2 2000.
 8 Abilene  2000     8   131 10710000  75000      765       6.4 2001.
 9 Abilene  2000     9   104  7615000  64500      771       6.5 2001.
10 Abilene  2000    10   101  7040000  59300      764       6.6 2001.
# … with 8,592 more rows
# 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)
# A tibble: 1 × 7
  date         pce    pop psavert uempmed unemploy   rate
  <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>  <dbl>
1 1982-12-01 2162. 233160    10.9    10.2    12051 0.0517
add_markers(p)
Error in loadNamespace(name): there is no package called 'webshot'
layout(p, annotations = list(x = ~date, y = ~rate, text = "peak"))
Error in loadNamespace(name): there is no package called 'webshot'
# 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)
No trace type specified:
  Based on info supplied, a 'histogram' trace seems appropriate.
  Read more about this trace type -> https://plotly.com/r/reference/#histogram
No trace type specified:
  Based on info supplied, a 'histogram' trace seems appropriate.
  Read more about this trace type -> https://plotly.com/r/reference/#histogram
No trace type specified:
  Based on info supplied, a 'histogram' trace seems appropriate.
  Read more about this trace type -> https://plotly.com/r/reference/#histogram
Error in loadNamespace(name): there is no package called 'webshot'
# 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)
Joining, by = "drv"
No trace type specified: Based on info supplied, a 'histogram' trace seems
appropriate. Read more about this trace type ->
https://plotly.com/r/reference/#histogram
No trace type specified: Based on info supplied, a 'histogram' trace seems
appropriate. Read more about this trace type ->
https://plotly.com/r/reference/#histogram
No trace type specified: Based on info supplied, a 'histogram' trace seems
appropriate. Read more about this trace type ->
https://plotly.com/r/reference/#histogram
Error in loadNamespace(name): there is no package called 'webshot'
# 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()
# A tibble: 1 × 11
    mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1    21     6   160   110   3.9  2.62  16.5     0     1     4     4
p %>% slice(1) %>% add_markers(name = "first observation")
Error in loadNamespace(name): there is no package called 'webshot'
p %>% filter(cyl == 4) %>% plotly_data()
# A tibble: 11 × 11
     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1
 2  24.4     4 147.     62  3.69  3.19  20       1     0     4     2
 3  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2
 4  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
 5  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
 6  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
 7  21.5     4 120.     97  3.7   2.46  20.0     1     0     3     1
 8  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
 9  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
10  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
11  21.4     4 121     109  4.11  2.78  18.6     1     1     4     2
p %>% filter(cyl == 4) %>% add_markers(name = "four cylinders")
Error in loadNamespace(name): there is no package called 'webshot'

[Package plotly version 4.10.0 Index]