Skip to content

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

Merged
merged 17 commits into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from 13 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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# ggplot2 3.1.0.9000

* `geom_rug()` gains a `length` option to allow for changing the length of the rug lines. (@daniel-wells, #3109)

* `coord_sf()` graticule lines are now drawn in the same thickness as
panel grid lines in `coord_cartesian()`, and seting panel grid
lines to `element_blank()` now also works in `coord_sf()`
Expand Down
29 changes: 18 additions & 11 deletions R/geom-rug.r
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Member

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:

By default, the rug lines are drawn with a length that corresponds to 3% of the total plot size. Since the default scale expansion of for continuous variables is 5% at both ends of the scale, the rug will not overlap with any data points under the default settings.

#'
#' @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)) +
Expand Down Expand Up @@ -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) {
Expand All @@ -62,6 +65,7 @@ geom_rug <- function(mapping = NULL, data = NULL,
params = list(
outside = outside,
sides = sides,
length = length,
na.rm = na.rm,
...
)
Expand All @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

It's generally better to add new arguments (here, length) after previously existing arguments, in case some downstream code depends on the order of the arguments. Also, better to provide a default length here (and maybe also add a default outside value).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I change the ordering in the geom_rug function as well as, or instead of, draw_panel? It seems changing the order or defaults for draw_panel makes no difference when adding geom_rug() to a plot.

Copy link
Member

Choose a reason for hiding this comment

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

No. In geom_rug(), length is placed after ..., which means the user need to pass the argument with its name. So, there's no chance for length to be mistaken. But, in draw_panel(), outside can be specified without the name. If some existing code uses outside positionally, it might fail due to this change.

Does this answer your question?

(Sorry that I didn't notice this on my review...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)

Expand All @@ -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
)
}
Expand All @@ -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
)
}
Expand Down
15 changes: 9 additions & 6 deletions man/geom_rug.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions tests/testthat/test-geom-rug.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

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

Sorry, one more comment here. This expect_is() seems no more needed. If geom_rug() raises an error with a unit object, the test case below will fail so that we can notice something is wrong.

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"))

})