Skip to contents

rsgl implements SGL (Structured Graphics Language), a declarative language for specifying statistical graphics that is designed to feel like SQL. You write a SGL statement, pass it to dbGetPlot() with a DuckDB connection, and get back a ggplot2 plot.

Installation

You can install rsgl from GitHub with:

# install.packages("remotes")
remotes::install_github("sgl-projects/rsgl")

Examples

Scatterplot

dbGetPlot(con, "
  visualize
    hp as x,
    mpg as y,
    cyl as color
  from cars
  using points
")

Histogram

dbGetPlot(con, "
  visualize
    bin(mpg) as x,
    count(*) as y
  from cars
  group by
    bin(mpg)
  using bars
")

Scatterplot with regression line

dbGetPlot(con, "
  visualize
    hp as x,
    mpg as y
  from cars
  using (
    points
    layer
    regression line
  )
  scale by
    log(x),
    log(y)
")

Faceted box plot

dbGetPlot(con, "
  visualize
    cyl_cat as x,
    mpg as y
  from (
    select mpg, cast(cyl as varchar) as cyl_cat, am
    from cars
  )
  using boxes
  facet by
    am
")

Learn more