The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
rasterize
When you need to plot data with many observations, storing plots
completely in vector format is unsuitable: it requires tons of space and
is very slow to work with in graphic editors. On the other hand,
completely rasterizing the plot distorts important text content and
prevents readers from editing and copying. The solution provided here
within the package ggrastr
is to rasterize layers that have
a lot of data points, keeping all the rest in vector format.
The simplest way to rasterize some of your layers is to call
rasterize
on the plot object:
library(ggplot2)
library(ggrastr)
plot <- ggplot(diamonds, aes(carat, price, colour = cut)) +
geom_point()
rasterize(plot, layers='Point', dpi=50)
The layer parameter here accepts the layers, which should be rasterized and can work with vectors of layer types.
The same function can be applied on the level of individual layers. It allows users to rasterize only some layers of the same type:
ggplot() +
rasterise(geom_point(aes(carat, price, colour = cut), data=diamonds), dpi=30) +
geom_point(aes(x=runif(20, 0, 5), y=runif(20, 0, 20000)), size=10, color="black", shape=8)
Note that when the aspect ratio is distorted, the objects are rendered without distortion, i.e. the points in this example are still circles:
By default, plots are rendered with Cairo. However,
users now have the option to render plots with the ragg device. The motivation for
using ragg
is that ragg
can be faster and has
better anti-aliasing. That being said, the default ragg device also has
some alpha blending quirks. Because of these quirks, users are
recommended to use the ragg_png
option to work around the
alpha blending.
The differences in devices are best seen at lower resolution:
# Using 'ragg' gives better anti-aliasing but has unexpected alpha blending
plot + rasterise(geom_point(), dpi = 5, dev = "ragg")
# Using 'ragg_png' solves the alpha blend, but requires writing a temporary file to disk
plot + rasterise(geom_point(), dpi = 5, dev = "ragg_png")
Note that facets are rendered correctly without users having to adjust the width/height settings.
Users are also able to change the size of the raster objects with the
parameter scale
. The default behavior is not to modify the
size with scale=1
:
# unchanged scaling, scale=1
plot <- ggplot(diamonds, aes(carat, price, colour = cut))
plot + rasterise(geom_point(), dpi = 300, scale = 1)
Setting scale
to values greater than 1 will increase the
size of the rasterized objects. In this case, scale=2
will
double the size of the points in comparison to the original plot:
# larger objects, scale > 1
plot <- ggplot(diamonds, aes(carat, price, colour = cut))
plot + rasterise(geom_point(), dpi = 300, scale = 2)
Similarly, values less than 1 will result in smaller objects. Here we
see scale=0.5
results in points half the size of the points
in the original plot above:
As of ggrastr versions >=0.2.3
, users are also able
to rasterize multiple layers at once using (valid) lists. It is mainly
useful when working with geom_sf
, as it returns the list
object instead of a single geom:
world1 <- sf::st_as_sf(maps::map('world', plot = FALSE, fill = TRUE))
ggplot() + rasterise(
list(
list(
geom_sf(data = world1),
theme(panel.background = element_rect(fill = "skyblue"))
),
list(
list(
geom_point(aes(x = rnorm(100, sd = 10), y = rnorm(100, sd = 10)))
),
theme(panel.border = element_rect(fill = NA, colour = "blue"))
)
)
)
The parameter dpi
is an integer which sets the desired
resolution in dots per inch. With ggrastr versions
>=0.2.2
, users can set this parameter globally, using options().
In the following example, plots will be rendered with
dpi=750
after the user sets this with
options(ggrastr.default.dpi=750)
:
For legacy reasons, we have all popular geoms wrapped inside the
package. However, we strongly encourage users to use the
rasterise()
function instead.
geom_point_rast
: raster scatter plotsgeom_jitter_rast
: raster jittered scatter plotsgeom_boxplot_jitter
: boxplots that allows to jitter and
rasterize outlier pointsgeom_tile_rast
: raster heatmapgeom_beeswarm_rast
: raster bee swarm
plotsgeom_quasirandom_rast
: raster quasirandom
scatter plotFor more details, see the vignettes detailing these legacy functions here (for the Markdown version) and here (for the HTML version).
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.