Introduction to rasterly

2020-06-07

library(rasterly)
library(data.table)
library(lubridate)
library(grid)
library(plotly)

rasterly makes it easy to rapidly generate raster images for large datasets. Although the package is inspired by the Datashader library available for Python, rasterly does not attempt to reproduce all the features of Datashader. Rather, rasterly offers comparable performance to Datashader when generating rasters from source data. rasterly attempts to provide a flexible, convenient interface which should feel familiar to users of ggplot2 and its aesthetics-based approach to customizing plots and figures.

A raster may be described as a matrix of cells or pixels arranged in grid-like fashion, in which each pixel represents a value in the source data. When combined with the package and Plotly.js, rasterly enables analysts to generate interactive figures with very large datasets which are responsive enough to embed into Dash for R applications.

The rasterly function creates a rasterly object, to which aggregation layers may be added. This function is the first step in the process of generating raster image data using the package. The rasterly function is not intended to be used in isolation, since aggregation layers are required for full functionality.

Data set

The dataset used in this vignette describes Uber trips taken in New York City from April 1st to September 30th of 2014.

# Load data
ridesRaw_1 <- "https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data1.csv" %>%
ridesRaw_2 <- "https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data2.csv" %>%
ridesRaw_3 <- "https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data3.csv"  %>%
ridesDf <- list(ridesRaw_1, ridesRaw_2, ridesRaw_3) %>%
data.table::rbindlist()

# Extract hour of trip taken

rasterly Structure

Subsetting

rasterly() generates a parent layer containing initial settings to generate the raster, which include plot_height, plot_width among others; child layers such as rasterly_points() can be piped in as well. Note that “p” above is a list of environments. The elements in “p” can be easily extracted or replaced by [ and [<-.

p["background"]
# $rasterly_env # [1] "white" #$rasterlyPoints1
# [1] "white"
########### Replace the background in child layer rasterly_points()
p["background", level = 2] <- "black"
p["background"]
# $rasterly_env # [1] "white" #$rasterlyPoints1
# [1] "black"
########## Colors in both rasterly() and rasterly_points() are replaced
## fire is a vector of colors (as character strings) with length 256
## see rasterly::fire
p["color", level = 1:2] <- fire_map
p

• level helps to define which layer to replace; the default is 1 (the parent layer generated by rasterly()).
• Available states which can be extracted or replaced are listed here:
1. Aggregation: data, mapping, plot_width, plot_height, range, x_range, y_range, xlim, ylim, aesthetics, reduction_func, glyph, max_size, group_by_data_table, drop_data, variable_check
2. Display: background, color, alpha, span, show_raster, layout

Build rasterly by rasterly_build()

To retrieve display info, use rasterly_build():

build <- rasterly_build(p)
str(build)

It contains:

• agg: aggregation matrices, a list of numerical matrices
• image: a raster matrix (has the same dimension with aggregation matrices)
• lims: a list of x limits and y limits for each layer
• x_range: the range of x over all layers
• y_range: the range of y over all layers
• plot_height: plot height, number of rows in aggregation matrix
• plot_width: plot width, number of columns in aggregation matrix
• variable_names: names of variables
• background: background color
• colors: color used to map in each pixel or used for categorical variable. It is distinguished by whether “color” is set in aes() or not

Display

rasterly does not provide any functionality to display the raster image data it generates, but instead relies on other packages.

plotly graphics

• add_rasterly_heatmap(): Layers are added to Plotly objects via add_trace(...); rasterly provides the add_rasterly_heatmap() function which also leverages add_heatmap() to generate single channel heatmap overlays for Plotly figures. Multi-channel heatmaps are not currently supported; this feature will be available in an upcoming release.

plotly::plot_ly(ridesDf, x = ~Lat, y = ~Lon) %>%
)