Skip to content

Commit

Permalink
Merge pull request #112 from animint/fix-space-free-vertical
Browse files Browse the repository at this point in the history
fix space free vertical
  • Loading branch information
tdhock authored Dec 14, 2023
2 parents 2f080db + df90a3f commit aa46d0d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: animint2
Title: Animated Interactive Grammar of Graphics
Version: 2023.11.21
Version: 2023.12.14
URL: https://animint.github.io/animint2/
BugReports: https://github.com/animint/animint2/issues
Authors@R: c(
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Changes in version 2023.12.14 (PR#112)

- bugfix in compiler height_proportion computation, which occured in ggplots with space=free and both vertical/horizontal panels. Before the vertical panels were always the same size, now they can be different sizes.

# Changes in version 2023.11.21

- setDTthreads(1) in CRAN testthat.R (created from build.sh).
Expand Down
15 changes: 9 additions & 6 deletions R/z_facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,17 @@ train_layout <- function(facet, coord, layout, ranges) {
scale.type <- paste0("SCALE_", u.type)
range.type <- paste0(l.type, ".range")
space.type <- paste0("SPACE_", u.type)
vals <- layout[[scale.type]]
uv <- unique(vals)
diffs <- sapply(ranges[uv], function(x) {
diff(x[[range.type]])
scale.vals <- layout[[scale.type]]
uniq.scale.vals <- unique(scale.vals)
diffs <- sapply(uniq.scale.vals, function(scale.value) {
panel.value <- which(scale.vals==scale.value)[1]
diff(ranges[[panel.value]][[range.type]])
})
# decide the proportion of the height/width each scale deserves based on the range
props <- data.frame(tmp1 = uv, tmp2 = diffs / sum(diffs))
names(props) <- c(scale.type, space.type)
props.list <- list()
props.list[[scale.type]] <- uniq.scale.vals
props.list[[space.type]] <- diffs / sum(diffs)
props <- do.call(data.frame, props.list)
layout <- plyr::join(layout, props, by = scale.type)
}
names(layout) <- gsub("SPACE_X", "width_proportion", names(layout), fixed = TRUE)
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-renderer1-facet-space.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,29 @@ test_that("width_proportion is constant or variable", {
expect_true(!both.equal(info$plots$freeBoth$layout$width_proportion))
})

viz <- animint(
freeBothFlip = ggplot(mtcars, aes(wt, mpg)) +
ggtitle("space=free, scales=free") +
scale_x_continuous(breaks=seq(1, 9)) +
geom_point(colour='grey50', size = 4) +
geom_point(aes(colour = cyl)) +
facet_grid(am ~ vs, space = "free", scales = "free", labeller=label_both))
info <- animint2HTML(viz)
test_that("same spacing for vertical and horizontal ticks", {
## scale="free" and space="free" means the distance between ticks
## should be the same across the horizontal and vertical panels.
x.axes <- getNodeSet(
info$html, "//svg[@id='plot_freeBothFlip']//g[contains(@class, 'xaxis')]")
expect_equal(length(x.axes), 2)
xdiff <- lapply(x.axes, getTickDiff, axis="x")
expect_true(both.equal(xdiff))
y.axes <- getNodeSet(
info$html, "//svg[@id='plot_freeBothFlip']//g[contains(@class, 'yaxis')]")
expect_equal(length(y.axes), 2)
ydiff <- lapply(y.axes, getTickDiff, axis="y")
expect_true(both.equal(ydiff))
})

no.panels <- ggplot(mtcars, aes(wt, mpg)) +
geom_point(colour='grey50', size = 4) +
geom_point(aes(colour = cyl))
Expand Down

0 comments on commit aa46d0d

Please sign in to comment.