-
Notifications
You must be signed in to change notification settings - Fork 2.1k
add length argument to geom_rug to allow different lengths #3109
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
Changes from 13 commits
de51017
c861d83
7c7db06
9caa182
017b5ed
f14492f
1f9c124
628db16
8f952f5
e8919eb
f867694
4ddc907
9149838
da35c26
ec86925
05237a8
0544d57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,14 @@ | |
#' with the two 1d marginal distributions. Rug plots display individual | ||
#' cases so are best used with smaller datasets. | ||
#' | ||
#' The rug lines are drawn with a fixed size (3\% of the total plot size) so | ||
#' are dependent on the overall scale expansion in order not to overplot | ||
#' existing data. | ||
#' | ||
#' @eval rd_aesthetics("geom", "rug") | ||
#' @inheritParams layer | ||
#' @inheritParams geom_point | ||
#' @param sides A string that controls which sides of the plot the rugs appear on. | ||
#' It can be set to a string containing any of `"trbl"`, for top, right, | ||
#' bottom, and left. | ||
#' @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. | ||
#' @param length A [grid::unit()] object that sets the length of the rug lines. Use scale expansion to avoid overplotting of data. | ||
#' @export | ||
#' @examples | ||
#' p <- ggplot(mtcars, aes(wt, mpg)) + | ||
|
@@ -43,11 +40,17 @@ | |
#' coord_cartesian(clip = "off") + | ||
#' theme(plot.margin = margin(1, 1, 1, 1, "cm")) | ||
#' | ||
#' # increase the line length and | ||
#' # expand axis to avoid overplotting | ||
#' p + geom_rug(length = unit(0.05, "npc")) + | ||
#' scale_y_continuous(expand = c(0.1, 0.1)) | ||
#' | ||
geom_rug <- function(mapping = NULL, data = NULL, | ||
stat = "identity", position = "identity", | ||
..., | ||
outside = FALSE, | ||
sides = "bl", | ||
length = unit(0.03, "npc"), | ||
na.rm = FALSE, | ||
show.legend = NA, | ||
inherit.aes = TRUE) { | ||
|
@@ -62,6 +65,7 @@ geom_rug <- function(mapping = NULL, data = NULL, | |
params = list( | ||
outside = outside, | ||
sides = sides, | ||
length = length, | ||
na.rm = na.rm, | ||
... | ||
) | ||
|
@@ -76,7 +80,10 @@ geom_rug <- function(mapping = NULL, data = NULL, | |
GeomRug <- ggproto("GeomRug", Geom, | ||
optional_aes = c("x", "y"), | ||
|
||
draw_panel = function(data, panel_params, coord, sides = "bl", outside) { | ||
draw_panel = function(data, panel_params, coord, sides = "bl", length, outside) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's generally better to add new arguments (here, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I change the ordering in the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. In Does this answer your question? (Sorry that I didn't notice this on my review...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I see thanks! I've implemented the three new suggestions. |
||
if (!inherits(length, "unit")) { | ||
stop("'length' must be a 'unit' object.", call. = FALSE) | ||
} | ||
rugs <- list() | ||
data <- coord$transform(data, panel_params) | ||
|
||
|
@@ -88,25 +95,25 @@ GeomRug <- ggproto("GeomRug", Geom, | |
|
||
# move the rug to outside the main plot space | ||
rug_length <- if (!outside) { | ||
list(min = 0.03, max = 0.97) | ||
list(min = length, max = unit(1, "npc") - length) | ||
} else { | ||
list(min = -0.03, max = 1.03) | ||
list(min = -1 * length, max = unit(1, "npc") + length) | ||
} | ||
|
||
gp <- gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt) | ||
if (!is.null(data$x)) { | ||
if (grepl("b", sides)) { | ||
rugs$x_b <- segmentsGrob( | ||
x0 = unit(data$x, "native"), x1 = unit(data$x, "native"), | ||
y0 = unit(0, "npc"), y1 = unit(rug_length$min, "npc"), | ||
y0 = unit(0, "npc"), y1 = rug_length$min, | ||
gp = gp | ||
) | ||
} | ||
|
||
if (grepl("t", sides)) { | ||
rugs$x_t <- segmentsGrob( | ||
x0 = unit(data$x, "native"), x1 = unit(data$x, "native"), | ||
y0 = unit(1, "npc"), y1 = unit(rug_length$max, "npc"), | ||
y0 = unit(1, "npc"), y1 = rug_length$max, | ||
gp = gp | ||
) | ||
} | ||
|
@@ -116,15 +123,15 @@ GeomRug <- ggproto("GeomRug", Geom, | |
if (grepl("l", sides)) { | ||
rugs$y_l <- segmentsGrob( | ||
y0 = unit(data$y, "native"), y1 = unit(data$y, "native"), | ||
x0 = unit(0, "npc"), x1 = unit(rug_length$min, "npc"), | ||
x0 = unit(0, "npc"), x1 = rug_length$min, | ||
gp = gp | ||
) | ||
} | ||
|
||
if (grepl("r", sides)) { | ||
rugs$y_r <- segmentsGrob( | ||
y0 = unit(data$y, "native"), y1 = unit(data$y, "native"), | ||
x0 = unit(1, "npc"), x1 = unit(rug_length$max, "npc"), | ||
x0 = unit(1, "npc"), x1 = rug_length$max, | ||
gp = gp | ||
) | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,26 @@ test_that("coord_flip flips the rugs", { | |
expect_equal(length(b[[1]]$children[[1]]$y0), 1) | ||
expect_equal(length(b[[1]]$children[[1]]$y1), 1) | ||
}) | ||
|
||
test_that("Rug length needs unit object", { | ||
p <- ggplot(df, aes(x,y)) | ||
expect_is(p + geom_rug(length=grid::unit(0.01, "npc")), "ggplot") | ||
yutannihilation marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, one more comment here. This |
||
expect_error(print(p + geom_rug(length=0.01))) | ||
}) | ||
|
||
test_that("Rug lengths are correct", { | ||
a <- layer_grob(p, 2) | ||
|
||
# Check default lengths | ||
expect_equal(a[[1]]$children[[1]]$x0, unit(0, "npc")) | ||
expect_equal(a[[1]]$children[[1]]$x1, unit(0.03, "npc")) | ||
|
||
p <- ggplot(df, aes(x, y)) + geom_point() + geom_rug(sides = 'l', length = unit(12, "pt")) | ||
b <- layer_grob(p, 2) | ||
|
||
# Check default length is changed | ||
expect_equal(a[[1]]$children[[1]]$x0, unit(0, "npc")) | ||
expect_equal(b[[1]]$children[[1]]$x1, unit(12, "pt")) | ||
|
||
}) | ||
|
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 think the deleted lines 7-9 contain important information and should be kept. But edit so it's clear that the default size can be modified.
Example: