The matrix-ggplot module provides a ggplot2-compatible API for Matrix data.
It delegates rendering to the Charm engine in matrix-charts, while preserving a familiar R-style workflow.
Add matrix-ggplot to your dependencies:
implementation platform('se.alipsa.matrix:matrix-bom:2.5.1')
implementation 'se.alipsa.matrix:matrix-ggplot'
implementation 'se.alipsa.matrix:matrix-datasets'import static se.alipsa.matrix.gg.GgPlot.*
import se.alipsa.matrix.datasets.Dataset
def mtcars = Dataset.mtcars()
def chart = ggplot(mtcars, aes(x: 'mpg', y: 'wt', color: 'cyl')) +
geom_point() +
geom_smooth(method: 'lm') +
labs(title: 'MPG vs Weight')
ggsave('ggplot-basic.svg', chart)You can avoid quoting common column names in new code:
import static se.alipsa.matrix.gg.GgPlot.*
import se.alipsa.matrix.datasets.Dataset
def mtcars = Dataset.mtcars()
def chart = ggplot(mtcars, aes { x = mpg; y = wt; color = cyl }) +
geom_point(alpha: 0.7) +
geom_smooth(method: 'lm')
ggsave('ggplot-closure-aes.svg', chart)Use endpoint and range aesthetics in aes() when a geom needs more than x and y.
xend and yend define segment endpoints. ymin and ymax define vertical ranges for
error bars and ribbons, while xmin and xmax define horizontal ranges for rectangular geoms.
import static se.alipsa.matrix.gg.GgPlot.*
import se.alipsa.matrix.core.Matrix
def data = Matrix.builder()
.columnNames(['x', 'y', 'xend', 'yend', 'lower', 'upper'])
.rows([
[1, 10, 2, 13, 8, 12],
[2, 14, 3, 16, 11, 17],
[3, 9, 4, 11, 7, 11]
])
.build()
def chart = ggplot(data, aes(x: 'x', y: 'y')) +
geom_segment(mapping: aes(xend: 'xend', yend: 'yend'), linewidth: 1.2) +
geom_errorbar(mapping: aes(ymin: 'lower', ymax: 'upper'), width: 0.2) +
geom_point(size: 3)
ggsave('ggplot-positional-ranges.svg', chart)labs() can set chart labels and independent legend titles for each mapped aesthetic.
import static se.alipsa.matrix.gg.GgPlot.*
import se.alipsa.matrix.core.Matrix
def data = Matrix.builder()
.columnNames(['category', 'value', 'kind', 'source'])
.rows([
['A', 10, 'baseline', 'observed'],
['B', 14, 'target', 'model'],
['C', 9, 'baseline', 'observed']
])
.build()
def chart = ggplot(data, aes(x: 'category', y: 'value')) +
geom_col(aes(fill: 'kind')) +
geom_point(mapping: aes(color: 'source'), size: 4) +
labs(
title: 'Grouped results',
x: 'Category',
y: 'Value',
color: 'Source',
fill: 'Kind'
)
ggsave('ggplot-labels-and-legends.svg', chart)import static se.alipsa.matrix.gg.GgPlot.*
import se.alipsa.matrix.datasets.Dataset
def mtcars = Dataset.mtcars()
def scatter = qplot(data: mtcars, x: 'mpg', y: 'wt', color: 'cyl')
def hist = qplot(data: mtcars, x: 'mpg', bins: 20, title: 'MPG distribution')
ggsave('qplot-scatter.svg', scatter)
ggsave('qplot-hist.svg', hist)import static se.alipsa.matrix.gg.GgPlot.*
import se.alipsa.matrix.datasets.Dataset
def mtcars = Dataset.mtcars()
def c = cols(mtcars)
def chart = ggplot(mtcars, aes(x: c.mpg, y: c.wt, color: c.cyl)) + geom_point()
ggsave('ggplot-cols.svg', chart)Go to previous section | Go to next section | Back to outline