Skip to content

Commit 3c3062d

Browse files
njtierneythomasp85
authored andcommitted
add an "outside" argument to geom_rug() to allow for moving rug tasse… (#3085)
1 parent 4c999f2 commit 3c3062d

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
* `coord_map()` now can have axes on the top and right (@karawoo, #3042).
1010

11+
* `geom_rug()` gains an "outside" option to allow for moving the rug tassels to outside the plot area. (@njtierney, #3085)
12+
1113
* `geom_rug()` now works with `coord_flip()` (@has2k1, #2987).
1214

1315
* Layers now have a new member function `setup_layer()` which is called at the

R/geom-rug.r

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#' @param sides A string that controls which sides of the plot the rugs appear on.
1515
#' It can be set to a string containing any of `"trbl"`, for top, right,
1616
#' bottom, and left.
17+
#' @param outside logical that controls whether to move the rug tassels outside of the plot area. Default is off (FALSE). You will also need to use `coord_cartesian(clip = "off")`. When set to TRUE, also consider changing the sides argument to "tr". See examples.
1718
#' @export
1819
#' @examples
1920
#' p <- ggplot(mtcars, aes(wt, mpg)) +
@@ -31,9 +32,21 @@
3132
#' ggplot(mpg, aes(displ, cty)) +
3233
#' geom_jitter() +
3334
#' geom_rug(alpha = 1/2, position = "jitter")
35+
#'
36+
#' # move the rug tassels to outside the plot
37+
#' # remember to set clip = "off".
38+
#' p + geom_rug(outside = TRUE) +
39+
#' coord_cartesian(clip = "off")
40+
#'
41+
#' # set sides to top right, and then move the margins
42+
#' p + geom_rug(outside = TRUE, sides = "tr") +
43+
#' coord_cartesian(clip = "off") +
44+
#' theme(plot.margin = margin(1, 1, 1, 1, "cm"))
45+
#'
3446
geom_rug <- function(mapping = NULL, data = NULL,
3547
stat = "identity", position = "identity",
3648
...,
49+
outside = FALSE,
3750
sides = "bl",
3851
na.rm = FALSE,
3952
show.legend = NA,
@@ -47,6 +60,7 @@ geom_rug <- function(mapping = NULL, data = NULL,
4760
show.legend = show.legend,
4861
inherit.aes = inherit.aes,
4962
params = list(
63+
outside = outside,
5064
sides = sides,
5165
na.rm = na.rm,
5266
...
@@ -62,7 +76,7 @@ geom_rug <- function(mapping = NULL, data = NULL,
6276
GeomRug <- ggproto("GeomRug", Geom,
6377
optional_aes = c("x", "y"),
6478

65-
draw_panel = function(data, panel_params, coord, sides = "bl") {
79+
draw_panel = function(data, panel_params, coord, sides = "bl", outside) {
6680
rugs <- list()
6781
data <- coord$transform(data, panel_params)
6882

@@ -72,20 +86,27 @@ GeomRug <- ggproto("GeomRug", Geom,
7286
sides <- chartr('tblr', 'rlbt', sides)
7387
}
7488

89+
# move the rug to outside the main plot space
90+
rug_length <- if (!outside) {
91+
list(min = 0.03, max = 0.97)
92+
} else {
93+
list(min = -0.03, max = 1.03)
94+
}
95+
7596
gp <- gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
7697
if (!is.null(data$x)) {
7798
if (grepl("b", sides)) {
7899
rugs$x_b <- segmentsGrob(
79100
x0 = unit(data$x, "native"), x1 = unit(data$x, "native"),
80-
y0 = unit(0, "npc"), y1 = unit(0.03, "npc"),
101+
y0 = unit(0, "npc"), y1 = unit(rug_length$min, "npc"),
81102
gp = gp
82103
)
83104
}
84105

85106
if (grepl("t", sides)) {
86107
rugs$x_t <- segmentsGrob(
87108
x0 = unit(data$x, "native"), x1 = unit(data$x, "native"),
88-
y0 = unit(1, "npc"), y1 = unit(0.97, "npc"),
109+
y0 = unit(1, "npc"), y1 = unit(rug_length$max, "npc"),
89110
gp = gp
90111
)
91112
}
@@ -95,15 +116,15 @@ GeomRug <- ggproto("GeomRug", Geom,
95116
if (grepl("l", sides)) {
96117
rugs$y_l <- segmentsGrob(
97118
y0 = unit(data$y, "native"), y1 = unit(data$y, "native"),
98-
x0 = unit(0, "npc"), x1 = unit(0.03, "npc"),
119+
x0 = unit(0, "npc"), x1 = unit(rug_length$min, "npc"),
99120
gp = gp
100121
)
101122
}
102123

103124
if (grepl("r", sides)) {
104125
rugs$y_r <- segmentsGrob(
105126
y0 = unit(data$y, "native"), y1 = unit(data$y, "native"),
106-
x0 = unit(1, "npc"), x1 = unit(0.97, "npc"),
127+
x0 = unit(1, "npc"), x1 = unit(rug_length$max, "npc"),
107128
gp = gp
108129
)
109130
}

man/geom_rug.Rd

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)