Title: | Rasterize Layers for 'ggplot2' |
---|---|
Description: | Rasterize only specific layers of a 'ggplot2' plot while simultaneously keeping all labels and text in vector format. This allows users to keep plots within the reasonable size limit without loosing vector properties of the scale-sensitive information. |
Authors: | Viktor Petukhov [aut, cph], Teun van den Brand [aut], Evan Biederstedt [cre, aut] |
Maintainer: | Evan Biederstedt <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.2 |
Built: | 2025-01-16 03:55:05 UTC |
Source: | https://github.com/vpetukhov/ggrastr |
geom_beeswarm
, but creates a raster layerThis geom is similar to geom_beeswarm
, but creates a raster layer
geom_beeswarm_rast( ..., priority = c("ascending", "descending", "density", "random", "none"), cex = 1, groupOnX = NULL, dodge.width = 0, raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
geom_beeswarm_rast( ..., priority = c("ascending", "descending", "density", "random", "none"), cex = 1, groupOnX = NULL, dodge.width = 0, raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
... |
Other arguments passed on to |
priority |
string Method used to perform point layout (see ggbeeswarm::position_beeswarm). |
cex |
numeric Scaling for adjusting point spacing (see ggbeeswarm::position_beeswarm) |
groupOnX |
boolean Whether jitter be added to the x axis (default=NULL). if TRUE then jitter is added to the x axis and if FALSE jitter is added to the y axis. (The default NULL causes the function to guess which axis is the categorical axis based on the number of unique entries in each). Refer to see ggbeeswarm::position_beeswarm for more details. |
dodge.width |
numeric Amount by which points from different aesthetic groups will be dodged (default=0). This requires that one of the aesthetics is a factor. Refer to see ggbeeswarm::position_beeswarm for more details. |
raster.dpi |
integer Resolution of the rastered image in dots per inch (default=300). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_beeswarm plot with rasterized layer
library(ggplot2) library(ggrastr) ggplot(mtcars) + geom_beeswarm_rast(aes(x = factor(cyl), y = mpg), raster.dpi = 600, cex = 1.5)
library(ggplot2) library(ggrastr) ggplot(mtcars) + geom_beeswarm_rast(aes(x = factor(cyl), y = mpg), raster.dpi = 600, cex = 1.5)
geom_boxplot
, but allows to jitter outlier points and to raster points layer.This geom is similar to geom_boxplot
, but allows to jitter outlier points and to raster points layer.
geom_boxplot_jitter( mapping = NULL, data = NULL, dev = "cairo", stat = "boxplot", position = "dodge", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ..., outlier.jitter.width = NULL, outlier.jitter.height = 0, raster.dpi = getOption("ggrastr.default.dpi", 300), scale = 1 )
geom_boxplot_jitter( mapping = NULL, data = NULL, dev = "cairo", stat = "boxplot", position = "dodge", na.rm = FALSE, show.legend = NA, inherit.aes = TRUE, ..., outlier.jitter.width = NULL, outlier.jitter.height = 0, raster.dpi = getOption("ggrastr.default.dpi", 300), scale = 1 )
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
dev |
string Specifies the device used, which can be one of: |
stat |
string The statistical transformation to use on the data for this layer, either as a ggproto Geom subclass or as a string naming the stat stripped of the stat_ prefix (e.g. "count" rather than "stat_count"). Refer to ggplot2::layer. |
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
... |
Other arguments passed on to |
outlier.jitter.width |
numeric Amount of horizontal jitter (default=NULL). The jitter is added in both positive and negative directions, so the total spread is twice the value specified here. If NULL, no jitter performed. |
outlier.jitter.height |
numeric Amount of horizontal jitter (default=0). The jitter is added in both positive and negative directions, so the total spread is twice the value specified here. |
raster.dpi |
integer Resolution of the rastered image (default=300). Ignored if |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_boxplot plot with rasterized layer
geom_boxplot()
understands the following aesthetics (required aesthetics are in bold):
x
or y
lower
or xlower
upper
or xupper
middle
or xmiddle
ymin
or xmin
ymax
or xmax
alpha
colour
fill
group
linetype
linewidth
shape
size
weight
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
library(ggplot2) library(ggrastr) yvalues = rt(1000, df=3) xvalues = as.factor(1:1000 %% 2) ggplot() + geom_boxplot_jitter(aes(y=yvalues, x=xvalues), outlier.jitter.width = 0.1, raster = TRUE)
library(ggplot2) library(ggrastr) yvalues = rt(1000, df=3) xvalues = as.factor(1:1000 %% 2) ggplot() + geom_boxplot_jitter(aes(y=yvalues, x=xvalues), outlier.jitter.width = 0.1, raster = TRUE)
geom_jitter
, but creates a raster layerThis geom is similar to geom_jitter
, but creates a raster layer
geom_jitter_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
geom_jitter_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
... |
Other arguments passed on to |
raster.dpi |
integer Resolution of the rastered image in dots per inch (default=300). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_point_rast plot with rasterized layer
geom_point()
understands the following aesthetics (required aesthetics are in bold):
x
y
alpha
colour
fill
group
shape
size
stroke
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
library(ggplot2) library(ggrastr) ggplot(mpg) + geom_jitter_rast(aes(x = factor(cyl), y = hwy), raster.dpi = 600)
library(ggplot2) library(ggrastr) ggplot(mpg) + geom_jitter_rast(aes(x = factor(cyl), y = hwy), raster.dpi = 600)
geom_point
, but creates a raster layerThis geom is similar to geom_point
, but creates a raster layer
geom_point_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
geom_point_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
... |
Other arguments passed on to |
raster.dpi |
integer Resolution of the rastered image in dots per inch (default=300). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_point plot with rasterized layer
geom_point()
understands the following aesthetics (required aesthetics are in bold):
x
y
alpha
colour
fill
group
shape
size
stroke
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
library(ggplot2) library(ggrastr) ggplot() + geom_point_rast(aes(x=rnorm(1000), y=rnorm(1000)), raster.dpi=600)
library(ggplot2) library(ggrastr) ggplot() + geom_point_rast(aes(x=rnorm(1000), y=rnorm(1000)), raster.dpi=600)
geom_quasirandom
, but creates a raster layerThis geom is similar to geom_quasirandom
, but creates a raster layer
geom_quasirandom_rast( ..., width = NULL, varwidth = FALSE, bandwidth = 0.5, nbins = NULL, method = "quasirandom", groupOnX = NULL, dodge.width = 0, raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
geom_quasirandom_rast( ..., width = NULL, varwidth = FALSE, bandwidth = 0.5, nbins = NULL, method = "quasirandom", groupOnX = NULL, dodge.width = 0, raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
... |
Other arguments passed on to |
width |
the maximum amount of spread (default: 0.4) |
varwidth |
vary the width by the relative size of each group |
bandwidth |
the bandwidth adjustment to use when calculating density Smaller numbers (< 1) produce a tighter "fit". (default: 0.5) |
nbins |
the number of bins used when calculating density (has little effect with quasirandom/random distribution) |
method |
the method used for distributing points
(quasirandom, pseudorandom, smiley, maxout, frowney, minout, tukey, tukeyDense).
See |
groupOnX |
|
dodge.width |
Amount by which points from different aesthetic groups will be dodged. This requires that one of the aesthetics is a factor. |
raster.dpi |
integer Resolution of the rastered image in dots per inch (default=300). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_quasirandom plot with rasterized layer
geom_point()
understands the following aesthetics (required aesthetics are in bold):
x
y
alpha
colour
fill
group
shape
size
stroke
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
library(ggplot2) library(ggrastr) ggplot(mtcars) + geom_quasirandom_rast(aes(x = factor(cyl), y = mpg), raster.dpi = 600)
library(ggplot2) library(ggrastr) ggplot(mtcars) + geom_quasirandom_rast(aes(x = factor(cyl), y = mpg), raster.dpi = 600)
geom_tile
, but creates a raster layerThis geom is similar to geom_tile
, but creates a raster layer
geom_tile_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
geom_tile_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
... |
Other arguments passed on to |
raster.dpi |
integer Resolution of the rastered image in dots per inch (default=300). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_tile plot with rasterized layer
geom_tile()
understands the following aesthetics (required aesthetics are in bold):
x
y
alpha
colour
fill
group
height
linetype
linewidth
width
Note that geom_raster()
ignores colour
.
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
library(ggplot2) library(ggrastr) coords <- expand.grid(1:100, 1:100) coords$Value <- 1 / apply(as.matrix(coords), 1, function(x) sum((x - c(50, 50))^2)^0.01) ggplot(coords) + geom_tile_rast(aes(x=Var1, y=Var2, fill=Value))
library(ggplot2) library(ggrastr) coords <- expand.grid(1:100, 1:100) coords$Value <- 1 / apply(as.matrix(coords), 1, function(x) sum((x - c(50, 50))^2)^0.01) ggplot(coords) + geom_tile_rast(aes(x=Var1, y=Var2, fill=Value))
geom_violin
, but creates a raster layerThis geom is similar to geom_violin
, but creates a raster layer
geom_violin_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
geom_violin_rast( ..., raster.dpi = getOption("ggrastr.default.dpi", 300), dev = "cairo", scale = 1 )
... |
Other arguments passed on to |
raster.dpi |
integer Resolution of the rastered image in dots per inch (default=300). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
geom_violin_rast plot with rasterized layer
geom_violin()
understands the following aesthetics (required aesthetics are in bold):
x
y
alpha
colour
fill
group
linetype
linewidth
weight
Learn more about setting these aesthetics in vignette("ggplot2-specs")
.
library(ggplot2) library(ggrastr) ggplot(mpg) + geom_violin_rast(aes(x = factor(cyl), y = hwy), raster.dpi = 600)
library(ggplot2) library(ggrastr) ggplot(mpg) + geom_violin_rast(aes(x = factor(cyl), y = hwy), raster.dpi = 600)
Rasterise ggplot layers Takes a ggplot object or a layer as input and renders their graphical output as a raster.
rasterise(input, ...) ## S3 method for class 'Layer' rasterise(input, ..., dpi = NULL, dev = "cairo", scale = 1) ## S3 method for class 'list' rasterise(input, ..., dpi = NULL, dev = "cairo", scale = 1) ## S3 method for class 'ggplot' rasterise( input, ..., layers = c("Point", "Tile"), dpi = NULL, dev = "cairo", scale = 1 )
rasterise(input, ...) ## S3 method for class 'Layer' rasterise(input, ..., dpi = NULL, dev = "cairo", scale = 1) ## S3 method for class 'list' rasterise(input, ..., dpi = NULL, dev = "cairo", scale = 1) ## S3 method for class 'ggplot' rasterise( input, ..., layers = c("Point", "Tile"), dpi = NULL, dev = "cairo", scale = 1 )
input |
ggplot plot object to rasterize |
... |
ignored |
dpi |
integer Sets the desired resolution in dots per inch (default=NULL). |
dev |
string Specifies the device used, which can be one of: |
scale |
numeric Scaling factor to modify the raster object size (default=1). The parameter 'scale=1' results in an object size that is unchanged, 'scale'>1 increase the size, and 'scale'<1 decreases the size. These parameters are passed to 'height' and 'width' of grid::grid.raster(). Please refer to 'rasterise()' and 'grid::grid.raster()' for more details. |
layers |
list of layer types that should be rasterized |
The default dpi
(NULL
(i.e. let the device decide)) can conveniently be controlled by setting the option "ggrastr.default.dpi"
(e.g. options("ggrastr.default.dpi" = 30)
for drafting).
A modified Layer
object.
Teun van den Brand <[email protected]>
require(ggplot2) # `rasterise()` is used to wrap layers ggplot(pressure, aes(temperature, pressure)) + rasterise(geom_line()) # The `dpi` argument controls resolution ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), dpi = 5) # The `dev` argument offers a few options for devices require(ragg) ggplot(diamonds, aes(carat, depth, z = price)) + rasterise(stat_summary_hex(), dev = "ragg") # The `scale` argument allows you to render a 'big' plot in small window, or vice versa. ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), scale = 4)
require(ggplot2) # `rasterise()` is used to wrap layers ggplot(pressure, aes(temperature, pressure)) + rasterise(geom_line()) # The `dpi` argument controls resolution ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), dpi = 5) # The `dev` argument offers a few options for devices require(ragg) ggplot(diamonds, aes(carat, depth, z = price)) + rasterise(stat_summary_hex(), dev = "ragg") # The `scale` argument allows you to render a 'big' plot in small window, or vice versa. ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), scale = 4)
Rasterise ggplot layers Takes a ggplot object or a layer as input and renders their graphical output as a raster.
rasterize(input, ...)
rasterize(input, ...)
input |
ggplot plot object to rasterize |
... |
ignored |
The default dpi
(NULL
(i.e. let the device decide)) can conveniently be controlled by setting the option "ggrastr.default.dpi"
(e.g. options("ggrastr.default.dpi" = 30)
for drafting).
A modified Layer
object.
Teun van den Brand <[email protected]>
require(ggplot2) # `rasterise()` is used to wrap layers ggplot(pressure, aes(temperature, pressure)) + rasterise(geom_line()) # The `dpi` argument controls resolution ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), dpi = 5) # The `dev` argument offers a few options for devices require(ragg) ggplot(diamonds, aes(carat, depth, z = price)) + rasterise(stat_summary_hex(), dev = "ragg") # The `scale` argument allows you to render a 'big' plot in small window, or vice versa. ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), scale = 4)
require(ggplot2) # `rasterise()` is used to wrap layers ggplot(pressure, aes(temperature, pressure)) + rasterise(geom_line()) # The `dpi` argument controls resolution ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), dpi = 5) # The `dev` argument offers a few options for devices require(ragg) ggplot(diamonds, aes(carat, depth, z = price)) + rasterise(stat_summary_hex(), dev = "ragg") # The `scale` argument allows you to render a 'big' plot in small window, or vice versa. ggplot(faithful, aes(eruptions, waiting)) + rasterise(geom_point(), scale = 4)
Pretty theme
theme_pdf(show.ticks = TRUE, legend.pos = NULL)
theme_pdf(show.ticks = TRUE, legend.pos = NULL)
show.ticks |
boolean Whether to show x- and y-ticks (default=TRUE). |
legend.pos |
Vector with x and y position of the legend (default=NULL). |
ggplot2 with plot ticks and positioned legend
library(ggplot2) library(ggrastr) data = rnorm(100) colors = (1:100/100) ggplot() + geom_point(aes(x=data, y=data, color=colors)) + theme_pdf(FALSE, legend.pos=c(1, 1))
library(ggplot2) library(ggrastr) data = rnorm(100) colors = (1:100/100) ggplot() + geom_point(aes(x=data, y=data, color=colors)) + theme_pdf(FALSE, legend.pos=c(1, 1))