You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.jpgpath="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 conversionannotation_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))
The text was updated successfully, but these errors were encountered:
annotation_raster
can be quite slow to display a large image. The current implementation converts the numeric image to text (viagrDevices::as.raster
) which must be converted back to numeric values for actual drawing bygrid::rasterGrob
. SupportingnativeRaster
removes this round-trip conversion and dramatically speeds rendering.This reprex uses a modified
annotation_raster_native
to demonstrate the improvement. Aggplot2
implementation could simply skip theas.raster
call inannotation_raster
if the incoming raster object inherits fromnativeRaster
. No changes are needed inGeomRasterAnn
becausegrid::rasterGrob
, which it uses, works withnativeRaster
objects.The text was updated successfully, but these errors were encountered: