knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

ggvctrcoords

Travis build status Codecov test coverage

Eventually, this package aims to provide a skeleton for vctrs-based scales and coordinate system for ggplot2 that can be extended for vctrs-based classes.

It is pretty bare-bones at the moment and depends on the development version of ggplot2, and I wouldn't recommend incorporating this pacakge in anything else than a sandbox to play around with.

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("teunbrand/ggvctrcoords")

Example

The main problem that I watned to tackle is to walk through the internals of ggplot2 with custom classes of vectors that preserve attributes and classes. As shown below, the data for x has lost attributes.

library(ggplot2)

df <- data.frame(
  x = structure(1:10, foo = "bar"),
  y = 10:1
)

plot <- ggplot(df, aes(x, y)) +
  geom_point()

lay_dat <- layer_data(plot)
attributes(lay_dat$x)

The basic premise of this package is that one should be able to march a vector through the internals of ggplot if they have appropriate methods. One such example is implemented in the interval class:

library(ggvctrcoords)

x <- interval(3:5, c(4,6,8))
print(x)

We can see that the class survives the internals of ggplot2 by asking for the layer_data() of the plot:

df <- data.frame(
  x = x,
  y = 1:3
)

plot <- ggplot(df, aes(x, y)) +
  geom_point() +
  coord_cartesian_vctr()

lay_dat <- layer_data(plot)
print(lay_dat)

This makes it possible to perform context dependant evaluation of the class. For example, the interval class gives the start positions when asked for xmin and the end positions if asked for xmax.

ggplot(df, aes(xmin = x, xmax = x,
               ymin = y - 0.4, ymax = y + 0.4)) +
  geom_rect() +
  coord_cartesian_vctr()


teunbrand/ggvctrcoords documentation built on Jan. 12, 2020, 6:25 p.m.