Skip to content

Commit 35f9039

Browse files
Add ggalluvial support (#2061)
Co-authored-by: Carson Sievert <[email protected]> Co-authored-by: Abdessabour Moutik <[email protected]>
1 parent 15807cf commit 35f9039

File tree

7 files changed

+64
-8
lines changed

7 files changed

+64
-8
lines changed

.github/workflows/R-CMD-check.yaml

-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ jobs:
3535
- {os: ubuntu-18.04, r: 'oldrel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
3636
- {os: ubuntu-18.04, r: '3.6', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
3737
- {os: ubuntu-18.04, r: '3.5', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
38-
- {os: ubuntu-18.04, r: '3.4', rspm: "https://packagemanager.rstudio.com/cran/__linux__/bionic/latest"}
3938

4039
env:
4140
RSPM: ${{ matrix.config.rspm }}
@@ -99,13 +98,6 @@ jobs:
9998
Rscript -e "reticulate::conda_install('r-reticulate', 'python-kaleido')"
10099
Rscript -e "reticulate::conda_install('r-reticulate', 'plotly', channel = 'plotly')"
101100
Rscript -e "reticulate::use_miniconda('r-reticulate')"
102-
103-
- name: Session info
104-
run: |
105-
options(width = 100)
106-
pkgs <- installed.packages()[, "Package"]
107-
sessioninfo::session_info(pkgs, include_base = TRUE)
108-
shell: Rscript {0}
109101
110102
- name: Install shinytest deps
111103
if: matrix.config.shinytest == true

DESCRIPTION

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Suggests:
5353
hexbin,
5454
ggthemes,
5555
GGally,
56+
ggalluvial,
5657
testthat,
5758
knitr,
5859
devtools,

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## Improvements
44

5+
* `ggplotly()` now supports the `{ggalluvial}` package. (#2061, @moutikabdessabour)
56
* `ggplotly()` does not issue warnings with `options(warnPartialMatchArgs = TRUE)` any longer. (#2046, @bersbersbers)
67

78
# 4.10.0

R/layers2traces.R

+29
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,35 @@ to_basic.GeomQuantile <- function(data, prestats_data, layout, params, p, ...){
622622
dat
623623
}
624624

625+
# ggalluvial::GeomStratum
626+
#' @export
627+
to_basic.GeomStratum <- function(data, ...) {
628+
to_basic.GeomRect(data, ...)
629+
}
630+
631+
# ggalluvial::GeomAlluvium
632+
#' @export
633+
to_basic.GeomAlluvium <- function(data, ...) {
634+
# geom_alluvium by default generates a data.frame with a colour column and sets it to 0, which leads to an error when trying to get the colour from the number and grid::col2rgb complains that colors must be positive integers.
635+
cols <- unique(data$colour)
636+
if (length(cols) == 1 && cols[1] == 0) {
637+
data$colour <- NULL
638+
}
639+
640+
data <- data[order(data$x), ]
641+
row_number <- nrow(data)
642+
data_rev <- data[rev(seq_len(row_number)), ]
643+
unused_aes <- setdiff(names(data), c("x", "y", "ymin", "ymax"))
644+
645+
d <- structure(rbind(
646+
cbind(x = data$x, y = data$ymin, data[unused_aes]),
647+
cbind(x = data$x[row_number], y = data$ymin[row_number], data[row_number, unused_aes]),
648+
cbind(x = data_rev$x, y = data_rev$ymax, data_rev[unused_aes])
649+
), class = class(data))
650+
651+
prefix_class(d, "GeomPolygon")
652+
}
653+
625654
#' @export
626655
to_basic.default <- function(data, prestats_data, layout, params, p, ...) {
627656
data

tests/testthat/_snaps/ggalluvial/stratum-alluvium-color.svg

+1
Loading

tests/testthat/_snaps/ggalluvial/stratum-alluvium.svg

+1
Loading

tests/testthat/test-ggalluvial.R

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
skip_if_not_installed("ggalluvial")
2+
library(ggalluvial)
3+
4+
test_that("using both of `geom_alluvium` and `geom_stratum` gives the correct output", {
5+
p <- ggplot(as.data.frame(Titanic),
6+
aes(y = Freq, axis1 = Survived, axis2 = Sex, axis3 = Class)) +
7+
geom_alluvium(aes(fill = Class),
8+
width = 0, knot.pos = 0, reverse = FALSE) +
9+
guides(fill = "none") +
10+
geom_stratum(width = 1/8, reverse = FALSE) +
11+
geom_text(stat = "stratum", aes(label = after_stat(stratum)),
12+
reverse = FALSE) +
13+
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
14+
coord_flip() +
15+
ggtitle("Titanic survival by class and sex")
16+
expect_doppelganger(ggplotly(p), "stratum-alluvium")
17+
})
18+
19+
test_that("color aesthetic works", {
20+
p <- ggplot(as.data.frame(Titanic),
21+
aes(y = Freq, axis1 = Survived, axis2 = Sex, axis3 = Class)) +
22+
geom_alluvium(aes(color = Class), width = 0, knot.pos = 0, reverse = FALSE, alpha = 0.3) +
23+
geom_stratum(width = 1/8, reverse = FALSE) +
24+
geom_text(stat = "stratum", aes(label = after_stat(stratum)),
25+
reverse = FALSE) +
26+
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
27+
coord_flip() +
28+
ggtitle("Titanic survival by class and sex")
29+
expect_doppelganger(ggplotly(p), "stratum-alluvium-color")
30+
})
31+

0 commit comments

Comments
 (0)