| Title: | An Implementation of the 'SGL' Graphics Language |
|---|---|
| Description: | Generates plots from a database connection and a 'SGL' statement. 'SGL' is a graphics language designed to look and feel like 'SQL'. It is especially useful for those familiar with 'SQL' who want to specify plots in a similar manner. The 'SGL' language is described in Chapman (2025) <doi:10.48550/arXiv.2505.14690>. |
| Authors: | Jon Chapman [aut, cre, cph], Free Software Foundation, Inc. [cph] (Bison parser skeleton) |
| Maintainer: | Jon Chapman <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-08 20:36:13 UTC |
| Source: | https://github.com/cran/rsgl |
The avg function returns the average of a column
within each group.
avg
The name of a numerical column to average (required).
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize origin as x, avg(horsepower) as y from cars group by origin using bars ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize origin as x, avg(horsepower) as y from cars group by origin using bars ")
Documents the aliases, aesthetics, and qualifiers for the bar geom.
bar
bars
x
y
theta
r
color
horizontal: orients the bars horizontally.
unstacked: doesn't stack overlapping bars.
vertical: orients the bars vertically.
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize bin(miles_per_gallon) as x, count(*) as y from cars group by bin(miles_per_gallon) using bars ") dbGetPlot(con, " visualize bin(miles_per_gallon) as y, count(*) as x from cars group by bin(miles_per_gallon) using horizontal bars ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize bin(miles_per_gallon) as x, count(*) as y from cars group by bin(miles_per_gallon) using bars ") dbGetPlot(con, " visualize bin(miles_per_gallon) as y, count(*) as x from cars group by bin(miles_per_gallon) using horizontal bars ")
The bin function creates equal-width bins for a
column; original values are transformed into bin-center values.
bin
The name of a numerical column to bin (required).
The number of bins (optional, default 30).
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize bin(miles_per_gallon) as x, count(*) as y from cars group by bin(miles_per_gallon) using bars ") dbGetPlot(con, " visualize bin(miles_per_gallon, 10) as x, count(*) as y from cars group by bin(miles_per_gallon, 10) using bars ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize bin(miles_per_gallon) as x, count(*) as y from cars group by bin(miles_per_gallon) using bars ") dbGetPlot(con, " visualize bin(miles_per_gallon, 10) as x, count(*) as y from cars group by bin(miles_per_gallon, 10) using bars ")
Documents the aliases, aesthetics, and qualifiers for the box geom.
box
boxes
x
y
theta
r
color
horizontal: orients the boxes horizontally.
vertical: orients the boxes vertically.
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize origin as x, miles_per_gallon as y from cars using boxes ") dbGetPlot(con, " visualize origin as y, miles_per_gallon as x from cars using horizontal boxes ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize origin as x, miles_per_gallon as y from cars using boxes ") dbGetPlot(con, " visualize origin as y, miles_per_gallon as x from cars using horizontal boxes ")
A sample dataset used throughout rsgl documentation and examples
containing attributes for a collection of cars.
Loading rsgl masks datasets::cars; refer to the built-in version as
datasets::cars if you need it.
carscars
A data frame with 406 rows and 5 variables:
Integer identifier for the car.
Engine horsepower.
Fuel economy in miles per US gallon.
Country of origin (USA, Europe, Japan).
Model year (1970–1982).
Derived from the cars dataset in the vega-datasets collection
(https://github.com/vega/vega-datasets/blob/main/data/cars.json). The
Miles_per_Gallon, Horsepower, and Origin columns were kept and
renamed, year is the year taken from the original Year date, and
car_id is a row identifier.
The count function returns the number of rows in each
group.
count
* (required).
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize origin as x, count(*) as y from cars group by origin using bars ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize origin as x, count(*) as y from cars group by origin using bars ")
dbGetPlot takes a database connection and a SGL statement
and returns the corresponding plot.
dbGetPlot(con, sgl_stmt)dbGetPlot(con, sgl_stmt)
con |
A database connection (as returned by |
sgl_stmt |
A SGL statement (string) |
The plot defined by the SGL statement (a sgl_plot object)
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) p <- dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points ") print(p)library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) p <- dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points ") print(p)
The linear scale linearly maps data values to a visual property.
It is the default scale for numerical aesthetic mappings.
linear
The name of an aesthetic to scale (required).
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) # explicit linear scales dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points scale by linear(x), linear(y) ") # default linear scales dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) # explicit linear scales dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points scale by linear(x), linear(y) ") # default linear scales dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points ")
Documents the aliases, aesthetics, and qualifiers for the line geom.
line
lines
x
y
theta
r
color
horizontal: orients the line horizontally;
points are connected in order of increasing x/theta values.
regression: fits a linear regression line to the data.
vertical: orients the line vertically;
points are connected in order of increasing y/r values.
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "trees", trees) dbGetPlot(con, " visualize age as x, circumference as y from trees collect by tree_id using lines ") dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using ( points layer regression line ) scale by log(x), log(y) ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "trees", trees) dbGetPlot(con, " visualize age as x, circumference as y from trees collect by tree_id using lines ") dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using ( points layer regression line ) scale by log(x), log(y) ")
The ln scale maps data values to a visual property
through a natural logarithm transformation.
ln
The name of an aesthetic to scale (required).
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points scale by ln(x), ln(y) ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points scale by ln(x), ln(y) ")
The log scale maps data values to a visual property
through a base-10 logarithm transformation.
log
The name of an aesthetic to scale (required).
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points scale by log(x), log(y) ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points scale by log(x), log(y) ")
Documents the aliases, aesthetics, and qualifiers for the point geom.
point
points
x
y
theta
r
color
size
jittered: adds a small amount of random noise to each point's position.
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points ") set.seed(0) dbGetPlot(con, " visualize origin as x, miles_per_gallon as y from cars using jittered points ")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "cars", cars) dbGetPlot(con, " visualize horsepower as x, miles_per_gallon as y from cars using points ") set.seed(0) dbGetPlot(con, " visualize origin as x, miles_per_gallon as y from cars using jittered points ")
A dataset used throughout rsgl documentation and examples
containing repeated measurements of tree growth over time.
Loading rsgl masks datasets::trees; refer to the built-in version as
datasets::trees if you need it.
treestrees
A data frame with 35 rows and 3 variables:
Integer identifier for the tree.
Age of the tree at the time of measurement.
Trunk circumference at the time of measurement.
Derived from datasets::Orange (R Core Team), with the Tree
column renamed to tree_id.
type_classifications takes a database connection and a table name
and returns the SGL type classifications (numerical, categorical,
or temporal) of the table's columns.
type_classifications(con, table_name)type_classifications(con, table_name)
con |
A database connection (as returned by |
table_name |
The name of a table |
A dataframe listing the SGL type classification of each column.
library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "iris", iris) type_classifications(con, "iris")library(duckdb) con <- dbConnect(duckdb()) dbWriteTable(con, "iris", iris) type_classifications(con, "iris")