Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: gglite
Title: Lightweight Data Visualization via the Grammar of Graphics
Version: 0.0.6
Version: 0.0.7
Authors@R: person("Yihui", "Xie", role = c("aut", "cre"), email = "xie@yihui.name",
comment = c(ORCID = "0000-0003-0645-5666"))
Description: A lightweight R interface to the AntV G2 JavaScript visualization
Expand Down
16 changes: 16 additions & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,22 @@ build_config = function(chart) {

# Chart-wide config
if (length(chart$scales)) config$scale = chart$scales

# Work around G2 legend bug: when shape is the only categorical encoding
# (no color), the legend renders wrong shapes. Explicitly set the shape
# scale range to exactly N shapes so ordinal wrapping is correct.
shape_col = chart$aesthetics$shape
if (is.character(shape_col) && length(shape_col) == 1 &&
!is.null(chart$data) && shape_col %in% names(chart$data) &&
is.null(chart$scales$shape$range)) {
n = length(unique(chart$data[[shape_col]]))
shapes = c('point', 'plus', 'diamond', 'square', 'triangle',
'hexagon', 'cross', 'bowtie')
if (is.null(config$scale)) config$scale = list()
config$scale$shape = modifyList(
as.list(config$scale$shape), list(range = rep_len(shapes, n))
)
}
if (!is.null(chart$coords)) config$coordinate = chart$coords
if (length(chart$interactions)) config$interaction = chart$interactions
if (length(chart$axes)) config$axis = chart$axes
Expand Down
18 changes: 18 additions & 0 deletions tests/testit/test-gglite.R
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,24 @@ assert('slider_x() and scrollbar_y() are shortcuts', {
(isTRUE(chart$scrollbars$y))
})

# build_config() auto-sets shape scale range to fix legend mismatch
assert('build_config() sets shape scale range when shape maps to column', {
chart = g2(iris, x = 'Sepal.Width', y = 'Sepal.Length',
shape = 'Species') |> mark_point()
config = build_config(chart)
# 3 unique Species -> range of length 3
(length(config$scale$shape$range) %==% 3L)
(config$scale$shape$range %==% c('point', 'plus', 'diamond'))
})

assert('build_config() does not override user-set shape range', {
chart = g2(iris, x = 'Sepal.Width', y = 'Sepal.Length',
shape = 'Species') |> mark_point() |>
scale_shape(range = c('circle', 'square', 'triangle'))
config = build_config(chart)
(config$scale$shape$range %==% c('circle', 'square', 'triangle'))
})

assert('build_config includes layout options', {
chart = g2(data.frame(x = 1, y = 2), x = 'x', y = 'y',
padding = 20, inset = c(5, NA, 5, NA)) |>
Expand Down
Loading