Skip to content

Commit 67ab63d

Browse files
has2k1clauswilke
authored andcommitted
Fix geom_rug + coord_flip (#2988)
* Fix geom_rug + coord_flip The rugs in `geom_rug` are drawn along an axis depending the `sides` parameter. Since the coordinate system does not alter the `geom` parameters, the `geom` has to alter any parameters that determine where the geoms are plotted. fixes #2987 * Explain why geom_rug flips the sides
1 parent 6786969 commit 67ab63d

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 3.1.0.9000
22

3+
* `geom_rug()` now works with `coord_flip()` (@has2k1, #2987).
4+
5+
36
# ggplot2 3.1.0
47

58
## Breaking changes

R/geom-rug.r

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ GeomRug <- ggproto("GeomRug", Geom,
6666
rugs <- list()
6767
data <- coord$transform(data, panel_params)
6868

69+
# For coord_flip, coord$tranform does not flip the sides where to
70+
# draw the rugs. We have to flip them.
71+
if (inherits(coord, 'CoordFlip')) {
72+
sides <- chartr('tblr', 'rlbt', sides)
73+
}
74+
6975
gp <- gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
7076
if (!is.null(data$x)) {
7177
if (grepl("b", sides)) {

tests/testthat/test-geom-rug.R

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
context("geom_rug")
2+
3+
n = 10
4+
df <- data.frame(x = 1:n, y = (1:n)^3)
5+
p <- ggplot(df, aes(x, y)) + geom_point() + geom_rug(sides = 'l')
6+
7+
test_that("coord_flip flips the rugs", {
8+
a <- layer_grob(p, 2)
9+
b <- layer_grob(p + coord_flip(), 2)
10+
11+
# Rugs along y-axis, all x coordinates are the same
12+
expect_equal(length(a[[1]]$children[[1]]$x0), 1)
13+
expect_equal(length(a[[1]]$children[[1]]$x1), 1)
14+
expect_equal(length(a[[1]]$children[[1]]$y0), n)
15+
expect_equal(length(a[[1]]$children[[1]]$y1), n)
16+
17+
# Rugs along x-axis, all y coordinates are the same
18+
expect_equal(length(b[[1]]$children[[1]]$x0), n)
19+
expect_equal(length(b[[1]]$children[[1]]$x1), n)
20+
expect_equal(length(b[[1]]$children[[1]]$y0), 1)
21+
expect_equal(length(b[[1]]$children[[1]]$y1), 1)
22+
})

0 commit comments

Comments
 (0)