Skip to content

rlugojr/ggimage

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ggimage: Use Image in 'ggplot2'

CRAN_Status_Badge gitter DOI

Supports aesthetic mapping of image files to be visualized in 'ggplot2' graphic system.

Authors

Guangchuang YU https://guangchuangyu.github.io

School of Public Health, The University of Hong Kong

Installation

Get the released version from CRAN:

## include BioC repo for dependencies
setRepositories(ind=1:2)
install.packages("ggimage")

Or the development version from github:

setRepositories(ind=1:2)
## install.packages("devtools")
devtools::install_github("GuangchuangYu/ggimage")

Related Packages

package purpose base plot ggplot2 geom layer& aes mapping^
CatterPlots cats Y
rphylopic phylopic Y Y Y & N*
emoGG emoji Y Y
ggflags flags Y Y Y
ggimage general, all kinds of images Y Y Y

* rphylopic supports using p+add_phylopic() to add 1 image each time since it internally use annotation_custom.

& a geom layer supports adding multiple (copy/different) images simultaneously.

^ without aes mapping, a layer can only add multiple copy of an image at different positions. While with aes, one can map a categorical variable to different images and adding them to a layer.

Examples

ggimage provides general solution for using images in ggplot2. It's easy to reproduce examples of other packages that designed for specific need.

CatterPlots

library(ggplot2)
library(ggimage)

mytheme <- theme_minimal() +
    theme(axis.title=element_blank())
theme_set(mytheme)

x <- seq(-2*pi, 2*pi, length.out=30)
d <- data.frame(x=x, y=sin(x))

img <- "http://www.belleamibengals.com/bengal_cat_2.png"
ggplot(d, aes(x, y)) + geom_image(image=img, size=.1)

<img src="https://guangchuangyu.github.io/blog_images/R/ggimage/ggimage_CatterPlots.png", width="400">

rphylopic

img <- "http://phylopic.org/assets/images/submissions/500bd7c6-71c1-4b86-8e54-55f72ad1beca.128.png"
ggplot(d, aes(x, y)) + geom_image(image=img, size=.1)

<img src="https://guangchuangyu.github.io/blog_images/R/ggimage/ggimage_rphylopic.png", width="400">

emoGG

emoji <- "https://twemoji.maxcdn.com/72x72/1f63b.png"
ggplot(d, aes(x, y)) + geom_image(image=emoji)

<img src="https://guangchuangyu.github.io/blog_images/R/ggimage/ggimage_emoGG.png", width="400">

ggflags

cn <- "https://behdad.github.io/region-flags/png/CN.png"
fr <- "https://behdad.github.io/region-flags/png/FR.png"
us <- "https://behdad.github.io/region-flags/png/US.png"

set.seed(123)
d$image <- sample(c(cn, fr, us), size=nrow(d), replace=TRUE)
ggplot(d, aes(x, y)) + geom_image(aes(image=image))

<img src="https://guangchuangyu.github.io/blog_images/R/ggimage/ggimage_ggflags.png", width="400">

TODO

  • legend of images (draw_key_image function)
  • move ggtree::subview to ggimage.
  • reimplement ggtree::phylopic function to geom_phylopic layer.

Packages

No packages published

Languages

  • R 87.4%
  • Makefile 12.6%