Skip to content

Support nativeRaster in annotation_raster #3388

Closed
@kent37

Description

@kent37

annotation_raster can be quite slow to display a large image. The current implementation converts the numeric image to text (via grDevices::as.raster) which must be converted back to numeric values for actual drawing by grid::rasterGrob. Supporting nativeRaster removes this round-trip conversion and dramatically speeds rendering.

This reprex uses a modified annotation_raster_native to demonstrate the improvement. A ggplot2 implementation could simply skip the as.raster call in annotation_raster if the incoming raster object inherits from nativeRaster. No changes are needed in GeomRasterAnn because grid::rasterGrob, which it uses, works with nativeRaster objects.

library(ggplot2)

# Image from https://github.com/akoyabio/phenoptrExamples/blob/master/inst/extdata/samples/Set12_20-6plex_%5B14146%2C53503%5D_composite_image.jpg
path = "Set12_20-6plex_[14146,53503]_composite_image.jpg"
image = jpeg::readJPEG(path)
dim(image) # 1400 x 1868

# This takes about 4 seconds to display the plot in RStudio on my computer
print(ggplot() + annotation_raster(image,
                                 xmin=0, xmax=1,
                                 ymin=0, ymax=1))

image2 = jpeg::readJPEG(path, native=TRUE)

# annotation_raster without the grDevices::as.raster conversion
annotation_raster_native <- function(raster, xmin, xmax, ymin, ymax,
                              interpolate = FALSE) {
  layer(
    data = ggplot2:::dummy_data(),
    mapping = NULL,
    stat = StatIdentity,
    position = PositionIdentity,
    geom = GeomRasterAnn,
    inherit.aes = FALSE,
    params = list(
      raster = raster,
      xmin = xmin,
      xmax = xmax,
      ymin = ymin,
      ymax = ymax,
      interpolate = interpolate
    )
  )
}

# This version takes less than two seconds to display.
print(ggplot() + annotation_raster_native(image2,
                                   xmin=0, xmax=1,
                                   ymin=0, ymax=1))

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions