• Stars
    star
    157
  • Rank 238,399 (Top 5 %)
  • Language
    R
  • License
    GNU General Publi...
  • Created about 6 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Drawing textured rectangles and bars with ggplot

ggtextures

Written by Claus O. Wilke

This package provides functions to draw textured rectangles and bars with the grid graphics system and with ggplot2.

Note: The package is at the stage of tech demo/proof of concept. It is not ready for production purposes.

Installation

Please install from github via:

devtools::install_github("clauswilke/ggtextures")

Example

Basic example of a textured rectangle drawn with grid:

library(ggtextures)
library(grid)
library(magick)
#> Linking to ImageMagick 6.9.9.39
#> Enabled features: cairo, fontconfig, freetype, lcms, pango, rsvg, webp
#> Disabled features: fftw, ghostscript, x11

img <- image_read("https://jeroen.github.io/images/Rlogo.png")

grid.newpage()
tg1 <- texture_grob(
  img,
  x = unit(.2, "npc"), y = unit(.05, "npc"),
  width = unit(.1, "npc"), height = unit(.9, "npc"),
  img_width = unit(.5, "in"), ncol = 1
)
tg2 <- texture_grob(
  img,
  x = unit(.5, "npc"), y = unit(.05, "npc"),
  width = unit(.3, "npc"), height = unit(.6, "npc"),
  img_width = unit(.5, "in"), ncol = 1
)

grid.draw(tg1)
grid.draw(tg2)

This is a basic example of textured rectangles in ggplot2:

library(ggplot2)
library(tibble)

data <- tibble(
  xmin = c(1, 2.5), ymin = c(1, 1), xmax = c(2, 4), ymax = c(4, 3),
  image = list(
    "https://jeroen.github.io/images/Rlogo.png",
    image_read_svg("https://jeroen.github.io/images/tiger.svg")
  )
)

ggplot(data, aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, image = image)) +
  geom_textured_rect(img_width = unit(1, "in"))

Note that we are reading in the svg file explicitly, using the function `image_read_svg()` from the magick package. This is needed for proper handling of transparencies in svg files.

We can also make a textured equivalent to geom_col() or geom_bar():

df <- tibble(
  trt = c("a", "b", "c"),
  outcome = c(2.3, 1.9, 3.2),
  image = c(
    "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg",
    "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/stone2-256.jpg",
    "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/siding1-256.jpg"
  )
)

ggplot(df, aes(trt, outcome, image = image)) +
  geom_textured_col(img_width = unit(0.5, "null"))

images = c(
  compact = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks2-256.jpg",
  midsize = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/stone2-256.jpg",
  suv = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/siding1-256.jpg",
  `2seater` = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/mulch1-256.jpg",
  minivan = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/rocks1-256.jpg",
  pickup = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/wood3-256.jpg",
  subcompact = "http://www.hypergridbusiness.com/wp-content/uploads/2012/12/concrete1-256.jpg"
)

ggplot(mpg, aes(class, image = class)) +
  geom_textured_bar() +
  scale_image_manual(values = images)

ggplot(mpg, aes(factor(trans), image = class)) +
  geom_textured_bar() +
  scale_image_manual(values = images)

Isotype bars can be drawn with geom_isotype_bar() and geom_isotype_col(). The units of the images are set as grid native units. Default is that the image height corresponds to one data unit.

data <- tibble(
  count = c(5, 3, 6),
  animal = c("giraffe", "elephant", "horse"),
  image = list(
    image_read_svg("http://steveharoz.com/research/isotype/icons/giraffe.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/elephant.svg"),
    image_read_svg("http://steveharoz.com/research/isotype/icons/horse.svg")
  )
)

ggplot(data, aes(animal, count, image = image)) +
  geom_isotype_col() +
  theme_minimal()

ggplot(data, aes(animal, count, image = image)) +
  geom_isotype_col(
    img_width = grid::unit(1, "native"), img_height = NULL,
    ncol = NA, nrow = 1, hjust = 0, vjust = 0.5, fill = "#80808040"
  ) +
  coord_flip() +
  theme_minimal()

More Repositories

1

dataviz

A book covering the fundamentals of data visualization
HTML
3,179
star
2

colorblindr

An R package to simulate colorblindness on R figures.
R
306
star
3

ggjoy

Deprecated. Please use ggridges.
R
293
star
4

practical_ggplot2

Step-by-step examples of building publication-quality figures in ggplot2
HTML
167
star
5

multiscales

Multivariate scales for ggplot2
R
100
star
6

dviz.supp

Supporting materials for Claus Wilke's data visualization book
R
90
star
7

PeptideBuilder

A simple Python library to generate model peptides
Python
78
star
8

relayer

Rethinking layers in ggplot2
R
73
star
9

ggisoband

Drawing isolines and isobands with 'ggplot2'
R
49
star
10

sinab

Sinab is not a browser.
Rust
27
star
11

COVID19-IFR

COVID-19 Infection Fatality Rates
25
star
12

proteinER

Supporting code for the paper "Measuring evolutionary rates of proteins in a structural context"
Python
12
star
13

pinetree

🌲 a flexible gene expression simulator with codon-specific translation rates
C++
11
star
14

artifact

Complete code for generative art project The Artifact.
JavaScript
5
star
15

sneronoi

Stochastic neighbor embedding meets Voronoi tessellation.
JavaScript
5
star
16

structural_prediction_of_ER

Code and data for Shahmoradi et al., Predicting evolutionary site variability from structure in viral proteins: buriedness, packing, flexibility, and design, J. Mol. Evol. 79:130–142 (2014).
R
5
star
17

codon_tools

A python package containing various tools for codon optimization and de-optimization.
Python
4
star
18

Ecoli_FBA_input_prediction

Code and data for Sridhara et al., Predicting growth conditions from internal metabolic fluxes in an in-silico model of E. coli, PLOS ONE 9:e114608 (2014).
TeX
3
star
19

protein_design_and_site_variability

Code and data for Jackson et al, Amino-acid site variability among natural and designed proteins, PeerJ 1:e211 (2013).
Python
3
star
20

fxrandomjs

Deterministic pseudorandom number generator
JavaScript
3
star
21

GBM_genomics

TeX
3
star
22

clauswilke.github.io.old

HTML
3
star
23

zipcodes

Translate zip codes into fips codes
HTML
3
star
24

clauswilke.github.io

HTML
2
star
25

MACV_SMD

Code and data for Meyer et al, Analyzing Machupo virus-receptor binding by molecular dynamics simulations, PeerJ 2:e266 (2014).
Tcl
2
star
26

alien_clock

HTML
1
star
27

Omega_MutSel

Code and data for Spielman and Wilke, The relationship between dN/dS and scaled selection coefficients, Mol. Biol. Evol. 2015.
TeX
1
star
28

Guo_etal_SCV

Data and code for Guo et al., Single-cell virology
HTML
1
star