rasterize ¶
rasterize(
layer: LayerSpec | FeatureSpecArray, dpi: float = 300
) -> LayerSpec | FeatureSpecArray
Draw a layer as a bitmap in SVG and PDF output (R package ggrastr).
A plot with a million points saves as a small vector file: the rasterized layer is one embedded image per panel, while axes, text, legends and the other layers stay vector. The image is clipped to the panel like the layer would be and its pixels line up with the axes.
PNG output is unchanged (it is a bitmap already). HTML output (notebooks) keeps the layer vector, so its tooltips and zoom keep working.
Parameters:
-
layer(LayerSpec or FeatureSpecArray) –The layer, e.g.
geom_point(...). Ageom_*()that adds several layers (geom_boxplot()with its outliers) is rasterized as a whole. -
dpi(float, default:300) –Resolution of the bitmap in dots per inch of the output, in
(0, 4800]. In SVG a plot pixel is 1/96 inch, so 300 dpi draws about 3 bitmap pixels per plot pixel; in PDF the page size sets the inch. The bitmap of one panel may have at most 32,768 pixels per side, so very high values fail at render time on large panels (RenderError).
Returns:
-
LayerSpec or FeatureSpecArray–A copy of
layerthat is drawn rasterized.
Raises:
-
UnsupportedValueError–When
layeris not a layer, ordpiis not a number. -
SpecError–When
dpiis outside(0, 4800].
Examples:
>>> from ggrust import *
>>> data = {"x": [1, 2, 3, 4], "y": [2, 4, 3, 5]}
>>> plot = ggplot(data, aes("x", "y")) + rasterize(geom_point(size=4), dpi=300)