add_rasterly: Add "rasterly" trace to a Plotly visualization

Description Usage Arguments Examples

Description

Add trace to a Plotly visualization.

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
add_rasterly_heatmap(
  p,
  x = NULL,
  y = NULL,
  z = NULL,
  ...,
  data = NULL,
  inherit = TRUE,
  on = NULL,
  size = NULL,
  scaling = NULL
)

add_rasterly_image(
  p,
  x = NULL,
  y = NULL,
  z = NULL,
  ...,
  data = NULL,
  inherit = TRUE,
  color = NULL,
  on = NULL,
  size = NULL
)

Arguments

p

A plotly object

x

Numeric vector or expression. The x variable, to be passed on to aes().

y

Numeric or expression. The y variable, to be passed on to aes().

z

Numeric. A numeric matrix (optional), to be processed with add_heatmap.

...

Arguments (i.e., attributes) passed along to the trace type or rasterly.

data

A data.frame or SharedData object (optional).

inherit

Logical. Inherit attributes from plotly?

on

Numeric vector or expression. Provides the data on which to reduce, to be passed on to aes().

size

Numeric vector or expression. Pixel size for each observation, to be passed on to aes().

scaling

Character string or function. The scaling method to be used for the trace.

color

Numeric vector or expression. Pixel color for each observation, to be passed on to aes().

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## Not run: 
if(requireNamespace("plotly") && requireNamespace("data.table") &&
  requireNamespace("lubridate")) {
 # Load data
 url1 <- "https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data1.csv"
 ridesRaw_1 <-  url1 %>%
   data.table::fread(stringsAsFactors = FALSE)
 url2 <- "https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data2.csv"
 ridesRaw_2 <-  url2 %>%
   data.table::fread(stringsAsFactors = FALSE)
 url3 <- "https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data3.csv"
 ridesRaw_3 <-  url3 %>%
   data.table::fread(stringsAsFactors = FALSE) 
 ridesDf <- list(ridesRaw_1, ridesRaw_2, ridesRaw_3) %>% 
   data.table::rbindlist()
 time <- lubridate::ymd_hms(ridesDf$`Date/Time`)
 ridesDf <-  ridesDf[, 'Date/Time':=NULL][, list(Lat, 
                                                 Lon,
                                                 hour = lubridate::hour(time),
                                                 month = lubridate::month(time),
                                                 day = lubridate::day(time))]
 ############################# add_rasterly_heatmap #############################
 #### quick start
 p <- plot_ly(data = ridesDf) %>%
        add_rasterly_heatmap(x = ~Lat, y = ~Lon)
 p
 #### set artificial scaling function
 zeroOneTransform <- function(z) {
   minz <- min(z)
   maxz <- max(z)
   M <- matrix((z - minz)/(maxz - minz), nrow = dim(z)[1])
   return(M)
 }
 plot_ly(data = ridesDf) %>%
   add_rasterly_heatmap(x = ~Lat,
                y = ~Lon,
                on = ~-Lat,
                reduction_func = "max",
                scaling = zeroOneTransform) %>%
   plotly::layout(
     xaxis = list(
       title = "x"
     ),
     yaxis = list(
       title = "y"
     )
   )
 ############################# add_rasterly_image #############################
 p <- plot_ly(data = ridesDf) %>%
        add_rasterly_image(x = ~Lat, y = ~Lon, color = ~hour,
                           # even `color_map` is deprecated,
                           # it is still a good way to specify the color mapping
                           color_map = hourColors_map, 
                           plot_width = 400, plot_height = 400)
 p
 }

## End(Not run)

rasterly documentation built on July 2, 2020, 2:12 a.m.