| Title: | Use Images in 'ggplot2' |
|---|---|
| Description: | Add image layers to 'ggplot2' plots. Images can be placed at data coordinates, used as plot backgrounds, or embedded as subplots. Supports local image files, image URLs, and image sources including 'PhyloPic', 'Pokemon', emoji, and icons. Interactive image layers are available for 'HTML' output. |
| Authors: | Guangchuang Yu [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-6485-8781>), Shuangbin Xu [ctb] (ORCID: <https://orcid.org/0000-0003-3513-5362>), Yonghe Xia [ctb] |
| Maintainer: | Guangchuang Yu <[email protected]> |
| License: | Artistic-2.0 |
| Version: | 0.3.8 |
| Built: | 2026-10-16 07:33:02 UTC |
| Source: | https://github.com/yulab-smu/ggimage |
obtaion suggestions for full names based on partial text name
autocomplete_name(name, ...)autocomplete_name(name, ...)
name |
partial text name |
... |
additional parameters |
scientific name
Clear the image caches and their diagnostics.
clear_image_cache()clear_image_cache()
NULL, invisibly.
get_image_cache_stats() reset_image_cache_stats()
download phylopic images
download_phylopic( id, destdir = ".", ..., reuse = TRUE, lock_timeout = getOption("ggimage.phylopic_lock_timeout", 30), lock_poll = getOption("ggimage.phylopic_lock_poll", 0.05) )download_phylopic( id, destdir = ".", ..., reuse = TRUE, lock_timeout = getOption("ggimage.phylopic_lock_timeout", 30), lock_poll = getOption("ggimage.phylopic_lock_poll", 0.05) )
id |
phylopic id |
destdir |
directory where the downloaded images are to be saved. |
... |
additional parameters passed to download.file |
reuse |
whether to reuse existing non-empty regular files |
lock_timeout |
maximum seconds to wait for a concurrent download lock |
lock_poll |
seconds between lock acquisition attempts |
This function allows users to download phylopic images using phylopic id
a character string (or vector) with downloaded file path
Guangchuang Yu
draw_key_image() draws the legend key of image layers (i.e. geom_image()).
The type of the key is determined by the global option ggimage.keytype,
which supports "point" (the default when the option is unset), "rect",
"image" and "blank" (no key, the behaviour of image layers before the
key was implemented). An unsupported value falls back to "point" with a
warning.
draw_key_image(data, params, size)draw_key_image(data, params, size)
data |
A single row data frame containing the scaled aesthetics to display in this key |
params |
A list of additional parameters supplied to the geom. |
size |
Width and height of key in mm |
The "image" key shows the image carried by the key data, which is the
layer's image argument or the images a guide is given with override.aes;
ggplot2 has no scale with which to look a per-row image up, so a layer that
maps image to a column puts the same image in every key. Without any image
to show, the key falls back to the logo bundled with the package.
Image layers have no default colour, so colour can be missing (i.e. NULL)
or NA in data, for instance when only alpha is mapped. Such keys fall
back to "black", so that alpha stays visible in the legend.
A grid grob
add image as background to plot panel.
geom_bgimage(image, pixel = NULL, x = 0, y = 0, scale = NULL)geom_bgimage(image, pixel = NULL, x = 0, y = 0, scale = NULL)
image |
image file, or a magick image object |
pixel |
the pixel of |
x |
x coordinate of the data point that |
y |
y coordinate of the data point that |
scale |
number of data units per image pixel. Required when |
An object that + adds to a plot as a background layer. The
background never takes part in the scale ranges: with pixel = NULL it
covers the panel whatever the data, and with pixel given the axes can be
discrete as well as continuous.
Guangchuang Yu
library("ggplot2") library("ggimage") img <- system.file("extdata/Rlogo.png", package = "ggimage") d <- data.frame(x = c(1, 2, 3), y = c(1, 2, 1.5)) p <- ggplot(d, aes(x, y)) + scale_x_continuous(limits = c(0, 4)) + scale_y_continuous(limits = c(0, 4)) ## by default the image is stretched to fill the panel p + geom_bgimage(img) + geom_point() ## with `pixel`, the image is placed in data coordinates instead: the ## centre of the bottom-left pixel is anchored to the origin, and `scale` ## sets the size info <- magick::image_info(magick::image_read(img)) p + geom_bgimage(img, pixel = c(1, info$height), x = 0, y = 0, scale = 4 / info$width) + geom_point()library("ggplot2") library("ggimage") img <- system.file("extdata/Rlogo.png", package = "ggimage") d <- data.frame(x = c(1, 2, 3), y = c(1, 2, 1.5)) p <- ggplot(d, aes(x, y)) + scale_x_continuous(limits = c(0, 4)) + scale_y_continuous(limits = c(0, 4)) ## by default the image is stretched to fill the panel p + geom_bgimage(img) + geom_point() ## with `pixel`, the image is placed in data coordinates instead: the ## centre of the bottom-left pixel is anchored to the origin, and `scale` ## sets the size info <- magick::image_info(magick::image_read(img)) p + geom_bgimage(img, pixel = c(1, info$height), x = 0, y = 0, scale = 4 / info$width) + geom_point()
geom layer for using emoji image
geom_emoji( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )geom_emoji( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )
mapping |
aes mapping |
data |
data |
inherit.aes |
whether inherit aes mapping from ggplot() |
na.rm |
whether remove NA values |
by |
one of 'width' or 'height' for specifying size |
... |
additional parameter |
ggplot2 layer
Guangchuang Yu
geom layer for using flag image
geom_flag( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )geom_flag( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )
mapping |
aes mapping |
data |
data |
inherit.aes |
whether inherit aes mapping from ggplot() |
na.rm |
whether remove NA values |
by |
one of 'width' or 'height' for specifying size |
... |
additional parameter |
ggplot2 layer
Guangchuang Yu
geom layer for using icon
geom_icon( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )geom_icon( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )
mapping |
aes mapping |
data |
data |
inherit.aes |
whether inherit aes mapping from ggplot() |
na.rm |
whether remove NA values |
by |
one of 'width' or 'height' for specifying size |
... |
additional parameter |
ggplot2 layer
Guangchuang Yu
geom layer for visualizing image files
geom_image( mapping = NULL, data = NULL, stat = "identity", position = "identity", inherit.aes = TRUE, na.rm = FALSE, by = "width", nudge_x = 0, nudge_y = 0, use_cache = TRUE, width = NULL, height = NULL, ... )geom_image( mapping = NULL, data = NULL, stat = "identity", position = "identity", inherit.aes = TRUE, na.rm = FALSE, by = "width", nudge_x = 0, nudge_y = 0, use_cache = TRUE, width = NULL, height = NULL, ... )
mapping |
aes mapping |
data |
data |
stat |
stat |
position |
position |
inherit.aes |
logical, whether inherit aes from ggplot() |
na.rm |
logical, whether remove NA values |
by |
one of 'width' or 'height' |
nudge_x |
horizontal adjustment to nudge image |
nudge_y |
vertical adjustment to nudge image |
use_cache |
logical, whether to use image caching for better performance (default: TRUE) |
width, height
|
Image width and height in native panel units, matching the
plotting units used by the
|
... |
additional parameters |
geom layer
geom_image() understands the following aesthetics (required aesthetics
are in bold):
x
y
image
size
width
height
colour
angle
alpha
colour and alpha are applied per row: colour tints the image and
alpha sets its opacity, so both aes(colour = ...) and
aes(alpha = ...) work. alpha is an ordinary ggplot2 aesthetic, so its
default continuous scale maps the data range onto [0.1, 1]; add
scale_alpha_identity() to pass the values through unchanged.
image may also be given as a list-column of magick images, one image per
row, in addition to a path or a URL per row. Such images are cached by their
content rather than by object identity.
Guangchuang Yu
## Not run: library("ggplot2") library("ggimage") set.seed(2017-02-21) d <- data.frame(x = rnorm(10), y = rnorm(10), image = sample(c("https://www.r-project.org/logo/Rlogo.png", "https://jeroenooms.github.io/images/frink.png"), size=10, replace = TRUE) ) # With caching enabled (default) ggplot(d, aes(x, y)) + geom_image(aes(image=image)) # With caching disabled ggplot(d, aes(x, y)) + geom_image(aes(image=image), use_cache=FALSE) # Per-row image size, via the `width` and `height` aesthetics ggplot(d, aes(x, y)) + geom_image(aes(image=image, width=abs(x)/10, height=abs(y)/10)) # Only one of them: the other dimension keeps the ratio of the image ggplot(d, aes(x, y)) + geom_image(aes(image=image, width=abs(x)/10)) ## End(Not run)## Not run: library("ggplot2") library("ggimage") set.seed(2017-02-21) d <- data.frame(x = rnorm(10), y = rnorm(10), image = sample(c("https://www.r-project.org/logo/Rlogo.png", "https://jeroenooms.github.io/images/frink.png"), size=10, replace = TRUE) ) # With caching enabled (default) ggplot(d, aes(x, y)) + geom_image(aes(image=image)) # With caching disabled ggplot(d, aes(x, y)) + geom_image(aes(image=image), use_cache=FALSE) # Per-row image size, via the `width` and `height` aesthetics ggplot(d, aes(x, y)) + geom_image(aes(image=image, width=abs(x)/10, height=abs(y)/10)) # Only one of them: the other dimension keeps the ratio of the image ggplot(d, aes(x, y)) + geom_image(aes(image=image, width=abs(x)/10)) ## End(Not run)
The geometry is based on geom_image().
See the documentation for those functions for more details.
geom_image_interactive(...)geom_image_interactive(...)
... |
see also the parameters of |
## Not run: library("ggplot2") library("ggimage") library("ggiraph") set.seed(2017-02-21) d <- data.frame(x = rnorm(10), y = rnorm(10), image = sample(c("https://www.r-project.org/logo/Rlogo.png", "https://jeroenooms.github.io/images/frink.png"), size=10, replace = TRUE) ) d$id <- sample(10) p <- ggplot(d, aes(x, y)) + geom_image_interactive(aes(image=image, tooltip = id, data_id = id)) girafe(ggobj = p) ## End(Not run)## Not run: library("ggplot2") library("ggimage") library("ggiraph") set.seed(2017-02-21) d <- data.frame(x = rnorm(10), y = rnorm(10), image = sample(c("https://www.r-project.org/logo/Rlogo.png", "https://jeroenooms.github.io/images/frink.png"), size=10, replace = TRUE) ) d$id <- sample(10) p <- ggplot(d, aes(x, y)) + geom_image_interactive(aes(image=image, tooltip = id, data_id = id)) girafe(ggobj = p) ## End(Not run)
geom layer for visualizing image files with basic overlap avoidance
geom_image_repel( mapping = NULL, data = NULL, stat = "identity", position = "identity", inherit.aes = TRUE, na.rm = FALSE, by = "width", nudge_x = 0, nudge_y = 0, use_cache = TRUE, width = NULL, height = NULL, max.iter = 100, force = 0.1, box.padding = 0, direction = "both", ... )geom_image_repel( mapping = NULL, data = NULL, stat = "identity", position = "identity", inherit.aes = TRUE, na.rm = FALSE, by = "width", nudge_x = 0, nudge_y = 0, use_cache = TRUE, width = NULL, height = NULL, max.iter = 100, force = 0.1, box.padding = 0, direction = "both", ... )
mapping |
aes mapping |
data |
data |
stat |
stat |
position |
position |
inherit.aes |
logical, whether inherit aes from ggplot() |
na.rm |
logical, whether remove NA values |
by |
one of 'width' or 'height' |
nudge_x |
horizontal adjustment to nudge image |
nudge_y |
vertical adjustment to nudge image |
use_cache |
logical, whether to use image caching for better performance |
width, height
|
Image width and height in native panel units, see
|
max.iter |
non-negative whole number of displacement iterations.
|
force |
non-negative number, the fraction of the current overlap that
is resolved in one iteration (each image of an overlapping pair moves by
|
box.padding |
non-negative number, extra space added around every
image box, in the same units as |
direction |
one of 'both', 'x' or 'y', the axis (or axes) along which
images are allowed to move. Overlap is always detected on both axes;
|
... |
additional parameters |
geom_image_repel() draws images like geom_image(), but moves images
that overlap each other apart before drawing them. The displacement is
computed offline (no network, no rendering, no ggrepel dependency) from
the bounding boxes of the images, so the result is deterministic: the same
plot always produces the same layout.
The algorithm works on the centres of the images, after they have been
nudged (nudge_x/nudge_y) and transformed by the coordinate system
(this is the space in which width/height are expressed, i.e. fractions
of the panel):
each image gets an axis aligned box of size
width + box.padding by height + box.padding;
for every pair of boxes that overlap on both axes, the two
images are pushed apart along the allowed direction, by
force * overlap / 2 each, away from each other;
step 2 is repeated at most max.iter times, stopping early once no
pair overlaps. The residual overlap after max.iter iterations is
about (1 - force)^max.iter of the initial one, so the default
(force = 0.1, max.iter = 100) leaves essentially no overlap.
which keeps the result reproducible. For large inputs, an adaptive uniform-grid
broad phase estimates candidate density before each sweep: sparse layouts use
one immutable grid, while dense layouts use the full scan when grid lookup and
candidate handling would cost more than checking every pair. If a sparse-grid
sweep moves any image, its candidate set is no longer current; the remainder
of that sweep conservatively falls back to the full scan. The grid costs
O(n + k) per sweep for k candidate checks (with O(n^2) worst-case dense
behaviour), while small inputs retain the full pair scan. The cleanup is
hard-capped at 256 sweeps so dense inputs cannot make cleanup grow with n
beyond that fixed number of sweeps.
Images are pushed away from each other only, they are not kept inside
the panel, and images are never re-ordered or removed. Images with
size = Inf (e.g. the background image added by geom_bgimage()) and
images that cannot be loaded are ignored by the repulsion: they keep their
original position and do not push the other images around.
The width of an image that is sized through size/by (instead of an
explicit width) is derived by grid from its height and from the
physical aspect of the panel. geom_image_repel() uses the aspect
reported by the coordinate system (coord_fixed(), exact) and assumes a
square panel otherwise, which over-estimates the width of a panel that is
wider than it is high. Pass explicit width/height when the exact box
matters.
geom layer
geom_image_repel() understands the same aesthetics as geom_image()
(required aesthetics are in bold):
x
y
image
size
width
height
colour
angle
alpha
colour and alpha are applied to the drawn image and do not influence
the repulsion, which is computed from x, y, size, width and
height alone.
Guangchuang Yu
library("ggplot2") library("ggimage") d <- data.frame(x = c(0.50, 0.52, 0.51), y = c(0.50, 0.51, 0.49), image = system.file("extdata/Rlogo.png", package = "ggimage")) ## the fixed scale matters: with the default limits the three points would ## be spread over the whole panel and would not overlap at all p <- ggplot(d, aes(x, y, image = image)) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1)) ## as they are, the images overlap p + geom_image(width = 0.12) ## ... and are pushed apart by geom_image_repel() p + geom_image_repel(width = 0.12, box.padding = 0.01) ## max.iter = 0 keeps the original coordinates p + geom_image_repel(width = 0.12, max.iter = 0) ## only move them apart horizontally p + geom_image_repel(width = 0.12, direction = "x")library("ggplot2") library("ggimage") d <- data.frame(x = c(0.50, 0.52, 0.51), y = c(0.50, 0.51, 0.49), image = system.file("extdata/Rlogo.png", package = "ggimage")) ## the fixed scale matters: with the default limits the three points would ## be spread over the whole panel and would not overlap at all p <- ggplot(d, aes(x, y, image = image)) + scale_x_continuous(limits = c(0, 1)) + scale_y_continuous(limits = c(0, 1)) ## as they are, the images overlap p + geom_image(width = 0.12) ## ... and are pushed apart by geom_image_repel() p + geom_image_repel(width = 0.12, box.padding = 0.01) ## max.iter = 0 keeps the original coordinates p + geom_image_repel(width = 0.12, max.iter = 0) ## only move them apart horizontally p + geom_image_repel(width = 0.12, direction = "x")
geom layer for using phylopic image
geom_phylopic( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", image_fun = function(x) magick::image_background(x, color = "none", flatten = FALSE), ... )geom_phylopic( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", image_fun = function(x) magick::image_background(x, color = "none", flatten = FALSE), ... )
mapping |
aes mapping |
data |
data |
inherit.aes |
whether inherit aes mapping from ggplot() |
na.rm |
whether remove NA values |
by |
one of 'width' or 'height' for specifying size |
image_fun |
a function to process magick-image objects, default is
|
... |
additional parameter |
ggplot2 layer
Guangchuang Yu
The geometry is based on geom_phylopic().
See the documentation for those functions for more details.
geom_phylopic_interactive(...)geom_phylopic_interactive(...)
... |
see also the parameters of |
geom layer for using pokemon image
geom_pokemon( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )geom_pokemon( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )
mapping |
aes mapping |
data |
data |
inherit.aes |
whether inherit aes mapping from ggplot() |
na.rm |
whether remove NA values |
by |
one of 'width' or 'height' for specifying size |
... |
additional parameter |
ggplot2 layer
Guangchuang Yu
subview geom
geom_subview( mapping = NULL, data = NULL, width = 0.1, height = 0.1, x = NULL, y = NULL, subview = NULL )geom_subview( mapping = NULL, data = NULL, width = 0.1, height = 0.1, x = NULL, y = NULL, subview = NULL )
mapping |
aes mapping, requires 'x', 'y' and 'subview' |
data |
data frame |
width |
width |
height |
height |
x |
x position of subview. This parameter works if mapping and data is not provided |
y |
y position of subview. This parameter works if mapping and data is not provided |
subview |
subview to plot, if not provided in data and specify by mapping |
Subviews can be placed in three ways. The positions and the subviews may be taken from the plot's own mapping, the usual 'ggplot2' idiom:
ggplot(df, aes(x, y, subview = pie)) + geom_subview() ggplot(df, aes(x, y)) + geom_subview(aes(subview = pie))
They may also all be given to the layer, with the positions and the subviews taken from its own data:
geom_subview(aes(x = x, y = y, subview = pie, width = width, height = width),
data = df)
For one or more subviews given by position, the first argument is read as the subview rather than as a mapping:
geom_subview(legend, x = 10, y = 50) geom_subview(df$pie, df$x, df$y, width = df$width, height = df$height)
A geom_subview() whose aesthetics are not all supplied by its own arguments
is resolved when it is added to a plot, so that an x, y or subview
inherited from ggplot() can be seen; what is missing from both places is
reported then, at the +, with the same message.
layer
guangchuang yu
geom layer for wordcup 2018
geom_worldcup2018( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )geom_worldcup2018( mapping = NULL, data = NULL, inherit.aes = TRUE, na.rm = FALSE, by = "width", ... )
mapping |
aes mapping |
data |
data |
inherit.aes |
whether inherit aes mapping from ggplot() |
na.rm |
whether remove NA values |
by |
one of 'width' or 'height' for specifying size |
... |
additional parameter |
ggplot2 layer
Guangchuang Yu
Cache limits may also be supplied with options(): use
ggimage.image_cache_capacity, ggimage.image_cache_ttl,
ggimage.image_cache_bytes, and ggimage.image_cache_eviction for both
caches, or the corresponding ggimage.image_cache_base_* and
ggimage.image_cache_transform_* options independently. Byte limits are
approximate object-size guardrails rather than process-memory measurements.
The default capacity, TTL, and byte cap are infinite, preserving the
historical unbounded cache. eviction is one of "lru", "fifo", or
"none"; the last value disables capacity and byte eviction. clock is
intended for deterministic tests and should return seconds or POSIXct.
get_image_cache_policy() set_image_cache_policy( capacity = NULL, ttl = NULL, eviction = NULL, base_capacity = NULL, transform_capacity = NULL, base_ttl = NULL, transform_ttl = NULL, bytes = NULL, base_bytes = NULL, transform_bytes = NULL, clock = NULL )get_image_cache_policy() set_image_cache_policy( capacity = NULL, ttl = NULL, eviction = NULL, base_capacity = NULL, transform_capacity = NULL, base_ttl = NULL, transform_ttl = NULL, bytes = NULL, base_bytes = NULL, transform_bytes = NULL, clock = NULL )
capacity, ttl
|
Optional shared capacity (number of entries) and TTL
(seconds). |
eviction |
Optional eviction policy: |
base_capacity, transform_capacity
|
Cache-specific capacities. |
base_ttl, transform_ttl
|
Cache-specific TTLs in seconds. |
bytes |
Optional shared estimated byte cap for each cache. |
base_bytes, transform_bytes
|
Cache-specific estimated byte caps. |
clock |
Optional function used as the cache clock. |
get_image_cache_policy() returns a policy list. The setter returns
the previous policy invisibly.
The bytes and entries values are estimates/current counts. Byte estimates
use object.size() (and a pixel-based lower bound for magick images), so
native ImageMagick allocations are not accounted for exactly.
get_image_cache_stats() get_image_cache_diagnostics()get_image_cache_stats() get_image_cache_diagnostics()
A list with base and transform named diagnostic vectors.
set background for ggplot
ggbackground(gg, background, ...)ggbackground(gg, background, ...)
gg |
gg object |
background |
background image |
... |
additional parameter to manipulate background image, see also geom_image |
gg object, with the background as its first layer. The layer sits on
one of the observations the plot already has, so it does not widen the axis
ranges and needs no continuous axis. The observation is taken from the
plot-level aes(), or from the first layer that maps x and y.
guangchuang yu
preview a plot befor saving it to a file.
ggpreview( filename = NULL, plot = last_plot(), width = NA, height = NA, units = "in", ... )ggpreview( filename = NULL, plot = last_plot(), width = NA, height = NA, units = "in", ... )
filename |
If it is not NULL, the previewed figure will be save to the file |
plot |
any plot that supported by the 'ggplotify' package |
width |
width of the figure |
height |
height of the figure |
units |
units of the 'width' and 'height' |
... |
additional parameters pass to ggsave() if filename is not NULL |
a preview of the figure
Guangchuang Yu
read image (by magick::image_read) with the ability to remove marginal empty space
image_read2(path, ..., cut_empty_space = TRUE)image_read2(path, ..., cut_empty_space = TRUE)
path |
file path |
... |
additional parameters that pass to magick::image_read |
cut_empty_space |
whether remove marginal empty space |
magick-image object
Guangchuang Yu
list available flag
list.flag()list.flag()
flag vector
Guangchuang Yu
list available icon
list.icon()list.icon()
icon vector
Guangchuang Yu
list available pokemon
list.pokemon()list.pokemon()
pokemon vector
Guangchuang Yu
list flags of worldcup 2018
list.worldcup2018()list.worldcup2018()
flag vector
Guangchuang Yu
query phylopic to get uid from scientific name
phylopic_uid(name, seed = 123)phylopic_uid(name, seed = 123)
name |
scientific name |
seed |
The random seed to use to generate the same uid, because a name might have many uid, the function will extract one of them randomly, default is 123. |
phylopic uid
Guangchuang Yu
Cache entries are retained; use clear_image_cache() to clear entries and
reset these counters together.
reset_image_cache_stats()reset_image_cache_stats()
A theme that only show the plot panel
theme_nothing(base_size = 11, base_family = "")theme_nothing(base_size = 11, base_family = "")
base_size |
font size |
base_family |
font family |
ggplot2 theme
Guangchuang Yu
transparent background theme
theme_transparent(...)theme_transparent(...)
... |
additional parameter to tweak the theme |
ggplot object
Guangchuang Yu with contributions from Hugo Gruson