Skip to content

YuLab-SMU/yplot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yplot: The Why of Plotting

Slogan: Focus on the Why, forget the How.

yplot is an R package that implements a universal grammar of graphics, decoupled from the rendering backend. It allows you to define what you want to see (the Why) and lets the backend handle how to draw it.

Philosophy

Most plotting libraries are tightly coupled to their rendering engine. ggplot2 is tied to grid. Base plot is tied to graphics. yplot separates the specification of the plot (the Intermediate Representation) from the rendering.

This architecture allows:

  • Portability: The same plot code can render to a static image (Base/Grid) or an interactive web visualization (D3/Vega).
  • Extensibility: New backends can be added without changing the user API.
  • Clarity: Users focus on data mapping, not drawing commands.
  • Modern Syntax: Uses the native pipe |> for composing layers, distinct from ggplot2's +.

Installation

# Not yet on CRAN
# install.packages("yplot")

Usage

The API is inspired by the Grammar of Graphics but designed for the pipe era.

library(yplot)

# Define the plot (The Why)
# Note: Functions are prefixed with y_ to avoid conflicts with ggplot2
# Note: Uses |> pipe instead of +
p <- yplot(mtcars, y_aes(x = wt, y = mpg)) |>
  y_point(color = "red") |>
  y_line(color = "blue")

# Mapped aesthetics + scale
p2 <- yplot(mtcars, y_aes(x = wt, y = mpg, colour = factor(cyl))) |>
  y_point() |>
  y_scale_color_discrete()

# Render using Base Graphics (The How)
print(p, backend = "base")

# Render using Grid Graphics
print(p, backend = "grid")

# Render to D3 (JSON Spec)
print(p, backend = "d3")

# Export to files
y_save(p, "plot.pdf", backend = "base")
y_save(p, "plot.html", backend = "d3", open = FALSE)

yplot_register_backend("txt", list(
  render = function(plot, file = NULL, open = FALSE, ...) {
    if (is.null(file)) stop("file is required")
    n_layers <- if (is.null(plot$layers)) 0 else length(plot$layers)
    writeLines(paste0("layers: ", n_layers), file)
    file
  }
))
y_save(p, "plot.txt", backend = "txt", open = FALSE)

# Optional: build once, render many
built <- y_build(p)
render_base(built)
render_grid(built)
render_d3(built, file = "plot.html", open = FALSE)

Architecture

  1. Frontend: yplot(), y_aes(), y_point() create a yplot object (a structured list).
  2. IR: The yplot object acts as an Intermediate Representation.
  3. Backends:
    • render_base(): Uses graphics::plot, graphics::points.
    • render_grid(): Uses grid::grid.points, grid::grid.lines.
    • render_d3(): Outputs JSON for D3.js (Prototype).

Roadmap

  • Core Architecture & IR
  • Base Graphics Backend
  • Grid Graphics Backend
  • Pipe-based API (|>)
  • Stats & position (histogram, smooth, jitter)
  • Facets & coords (facet_wrap, coord_cartesian)
  • Backend plugin system (register custom renderers)
  • Complete D3/Web Backend
  • Minimal scales & guides (colour/size/alpha)
  • Minimal theme (axis sizes, panel background/border)
  • Themes support
  • Statistical Transformations (stat_*)

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages