Learn R Programming

rphylopic (version 1.0.0)

add_phylopic: Add a PhyloPic to a ggplot plot

Description

Specify an existing image, taxonomic name, or PhyloPic uuid to add a PhyloPic silhouette as a separate layer to an existing ggplot plot.

Usage

add_phylopic(
  img = NULL,
  name = NULL,
  uuid = NULL,
  x = NULL,
  y = NULL,
  ysize = NULL,
  alpha = 1,
  color = "black"
)

Arguments

img

A Picture or png array object, e.g., from using get_phylopic().

name

A taxonomic name to be passed to get_uuid().

uuid

A valid uuid for a PhyloPic silhouette (such as that returned by get_uuid() or pick_phylopic()).

x

x value of the silhouette center. Ignored if y and ysize are not specified.

y

y value of the silhouette center. Ignored if x and ysize are not specified.

ysize

Height of the silhouette. The width is determined by the aspect ratio of the original image. Ignored if x and y are not specified.

alpha

A value between 0 and 1, specifying the opacity of the silhouette (0 is fully transparent, 1 is fully opaque).

color

Color to plot the silhouette in.

Details

One (and only one) of img, name, or uuid must be specified. Use parameters x, y, and ysize to place the silhouette at a specified position on the plot. If all three of these parameters are unspecified, then the silhouette will be plotted to the full height and width of the plot. The aspect ratio of the silhouette will always be maintained.

Examples

Run this code
# Put a silhouette behind a plot based on a taxonomic name
library(ggplot2)
ggplot(iris) +
  add_phylopic(name = "Iris", alpha = .2) +
  geom_point(aes(x = Sepal.Length, y = Sepal.Width))

# Put a silhouette anywhere based on UUID
posx <- runif(10, 0, 10)
posy <- runif(10, 0, 10)
sizey <- runif(10, 0.4, 2)
cols <- sample(c("black", "darkorange", "grey42", "white"), 10,
  replace = TRUE)

# Since we are plotting a lot of the same image, we should just save
# the image in our environment first
cat <- get_phylopic("23cd6aa4-9587-4a2e-8e26-de42885004c9")
p <- ggplot(data.frame(cat.x = posx, cat.y = posy), aes(cat.x, cat.y))
for (i in 1:10) {
  p <- p + add_phylopic(cat, x = posx[i], y = posy[i],
                        ysize = sizey[i], color = cols[i])
}
p + ggtitle("R Cat Herd!!")

Run the code above in your browser using DataLab