Skip to content

Fix geom_rug + coord_flip #2988

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

Merged
merged 2 commits into from
Nov 11, 2018
Merged
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 3.1.0.9000

* `geom_rug()` now works with `coord_flip()` (@has2k1, #2987).


# ggplot2 3.1.0

## Breaking changes
Expand Down
6 changes: 6 additions & 0 deletions R/geom-rug.r
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ GeomRug <- ggproto("GeomRug", Geom,
rugs <- list()
data <- coord$transform(data, panel_params)

# For coord_flip, coord$tranform does not flip the sides where to
# draw the rugs. We have to flip them.
if (inherits(coord, 'CoordFlip')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we wouldn't test for capabilities by testing for inheritance, but this is how it's done for CoordFlip all over the ggplot2 code base, so I guess it's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first instinct was to use is(), but it was not used anywhere in the coord base.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was other coord features are queried by asking the coord, e.g. coord$is_free(). So it would probably make sense to introduce coord$is_flip(). But that's for a different PR.

sides <- chartr('tblr', 'rlbt', sides)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a comment to this line explaining what's happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added a comment to the block.

}

gp <- gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
if (!is.null(data$x)) {
if (grepl("b", sides)) {
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-geom-rug.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
context("geom_rug")

n = 10
df <- data.frame(x = 1:n, y = (1:n)^3)
p <- ggplot(df, aes(x, y)) + geom_point() + geom_rug(sides = 'l')

test_that("coord_flip flips the rugs", {
a <- layer_grob(p, 2)
b <- layer_grob(p + coord_flip(), 2)

# Rugs along y-axis, all x coordinates are the same
expect_equal(length(a[[1]]$children[[1]]$x0), 1)
expect_equal(length(a[[1]]$children[[1]]$x1), 1)
expect_equal(length(a[[1]]$children[[1]]$y0), n)
expect_equal(length(a[[1]]$children[[1]]$y1), n)

# Rugs along x-axis, all y coordinates are the same
expect_equal(length(b[[1]]$children[[1]]$x0), n)
expect_equal(length(b[[1]]$children[[1]]$x1), n)
expect_equal(length(b[[1]]$children[[1]]$y0), 1)
expect_equal(length(b[[1]]$children[[1]]$y1), 1)
})