-
Notifications
You must be signed in to change notification settings - Fork 633
Add ggalluvial
support
#2044
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ggalluvial
support
#2044
Conversation
transform_alluvium <- function(data) { | ||
data <- data[order(data$x), ] | ||
|
||
if(unique(data$colour) == 0) data$colour <- NULL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure what this is doing, but it seems you want length()
, not unique()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
geom_alluvium
by default generates a data.frame with a colour
column and sets it to 0
. This behaviour results in an error when trying to get the colour from the number and grid::col2rgb
complains that colors must be positive integers. I got around this by removing the colour column if no color aesthetic is passed by the user.
Error in grDevices::col2rgb(x, alpha = TRUE) :
numerical color values must be positive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check needs to be more careful. If unique(data$colour)
is not of length 1, the if
will warn/error:
> if(c("a", "b") == 0) print("foo")
Warning message:
In if (c("a", "b") == 0) print("foo") :
the condition has length > 1 and only the first element will be used
Co-authored-by: Carson Sievert <[email protected]>
Co-authored-by: Carson Sievert <[email protected]>
test_that("using `geom_stratum` gives the correct output", { | ||
p <- ggplot(as.data.frame(Titanic), | ||
aes(y = Freq, | ||
axis1 = Survived, axis2 = Sex, axis3 = Class)) + | ||
geom_stratum(width = 1/8, reverse = FALSE) + | ||
geom_text(stat = "stratum", aes(label = after_stat(stratum)), | ||
reverse = FALSE) + | ||
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) + | ||
coord_flip() + | ||
ggtitle("Titanic survival by class and sex") | ||
#write_plotly_svg(p, "tests/testthat/_snaps/ggalluvial/stratum.svg") | ||
expect_doppelganger(ggplotly(p), "stratum") | ||
}) | ||
|
||
test_that("using `geom_alluvium` gives the correct output", { | ||
p <- ggplot(as.data.frame(Titanic), | ||
aes(y = Freq, | ||
axis1 = Survived, axis2 = Sex, axis3 = Class)) + | ||
geom_alluvium(aes(fill = Class), | ||
width = 0, knot.pos = 0, reverse = FALSE) + | ||
guides(fill = "none") + | ||
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) + | ||
coord_flip() + | ||
ggtitle("Titanic survival by class and sex") | ||
#write_plotly_svg(p, "tests/testthat/_snaps/ggalluvial/alluvium.svg") | ||
expect_doppelganger(ggplotly(p), "alluvium") | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests seems a bit redundant given you have them essentially combined in the plot above. It'd be better to have some coverage that we're handling colour
/fill
correctly when they're missing (or not a variable, like fill = "red"
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK so you want me to set fill to a static value in geom_alluvium. I think it works out of the box. Also for the checks to pass you need to add Suggests ggalluvial
to NAMESPACE. so that pak
would install it.
I'll take this over in #2061 |
Closes #1614.
I added tests and everything.