db_compute_raster: Aggregates intersections of two variables

View source: R/raster.R

db_compute_rasterR Documentation

Aggregates intersections of two variables

Description

To visualize two continuous variables, we typically resort to a Scatter plot. However, this may not be practical when visualizing millions or billions of dots representing the intersections of the two variables. A Raster plot may be a better option, because it concentrates the intersections into squares that are easier to parse visually.

Uses dplyr operations to aggregate data. Because of this approach, the calculations automatically run inside the database if 'data' has a database or sparklyr connection. The 'class()' of such tables in R are: tbl_sql, tbl_dbi, tbl_spark

Usage

db_compute_raster(data, x, y, fill = n(), resolution = 100, complete = FALSE)

db_compute_raster2(data, x, y, fill = n(), resolution = 100, complete = FALSE)

Arguments

data

A table (tbl)

x

A continuous variable

y

A continuous variable

fill

The aggregation formula. Defaults to count (n)

resolution

The number of bins created per variable. The higher the number, the more records will be imported from the source

complete

Uses tidyr::complete to include empty bins. Inserts value of 0.

Details

There are two considerations when using a Raster plot with a database. Both considerations are related to the size of the results downloaded from the database:

- The number of bins requested: The higher the bins value is, the more data is downloaded from the database.

- How concentrated the data is: This refers to how many intersections return a value. The more intersections without a value, the less data is downloaded from the database.

Value

An ungrouped data.frame with three columns: the x variable bins, the y variable bins, and the aggregated fill values for each x-y intersection.

For 'db_compute_raster2': A data.frame with five columns - the x and y variable bins, the fill values, and additional columns for the upper bounds of each bin (x_2 and y_2), useful for defining precise tile boundaries.

Examples

## Not run: 
library(DBI)
library(dplyr)
con <- dbConnect(duckdb::duckdb(), ":memory:")
db_faithful <- copy_to(con, faithful, "faithful")

# Returns a 100x100 grid of record count of intersections of eruptions and waiting
db_faithful |>
  db_compute_raster(eruptions, waiting)

# Returns a 50x50 grid of eruption averages of intersections of eruptions and waiting
db_faithful |>
  db_compute_raster(eruptions, waiting, fill = mean(eruptions), resolution = 50)

dbDisconnect(con)

## End(Not run)

dbplot documentation built on March 13, 2026, 5:06 p.m.