Skip to content

add param to alter width of rug segments in geom_rug #662

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
27 changes: 15 additions & 12 deletions R/geom-rug.r
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#' Marginal rug plots.
#'
#' @section Aesthetics:
#' @section Aesthetics:
#' \Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "rug")}
#'
#' @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 \code{"trbl"}, for top, right,
#' bottom, and left.
#' @param rugwidth The width the rug segments. This should be a number in [0,1]
#' in "npc" units. See the help for \code{\line{unit}}.
#'
#' @export
#' @examples
#' p <- ggplot(mtcars, aes(x=wt, y=mpg))
Expand All @@ -16,54 +19,54 @@
#' p + geom_point() + geom_rug(sides="trbl") # All four sides
#' p + geom_point() + geom_rug(position='jitter')
geom_rug <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", sides = "bl", ...) {
Copy link
Member

Choose a reason for hiding this comment

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

rugwidth should be here, in the definition for geom_rug, instead of in GeomRug$new.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it doesn't need to appear in both locations? rugwidth will be passed to GeomRug$draw in the triple-dots, and there is no need for a default? I was going off how geom_boxplot was written.

Copy link
Member

Choose a reason for hiding this comment

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

It should be something like:

geom_rug <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity", sides = "bl", rugwidth = 0.03, ...) {
  GeomRug$new(mapping = mapping, data = data, stat = stat, position = position, sides = sides, rugwidth = rugwidth, ...)
}

GeomRug$new(mapping = mapping, data = data, stat = stat, position = position, sides = sides, ...)
GeomRug$new(mapping = mapping, data = data, stat = stat, position = position, sides = sides, rugwidth=0.03, ...)
}

GeomRug <- proto(Geom, {
objname <- "rug"

draw <- function(., data, scales, coordinates, sides, ...) {
draw <- function(., data, scales, coordinates, sides, rugwidth=0.03, ...) {
rugs <- list()
data <- coord_transform(coordinates, data, scales)
data <- coord_transform(coordinates, data, scales)
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(0.03, "npc"),
y0 = unit(0, "npc"), y1 = unit(rugwidth, "npc"),
gp = gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
)
}

if(grepl("t", sides)) {
rugs$x_t <- segmentsGrob(
x0 = unit(data$x, "native"), x1 = unit(data$x, "native"),
y0 = unit(1, "npc"), y1 = unit(0.97, "npc"),
y0 = unit(1, "npc"), y1 = unit(1 - rugwidth, "npc"),
gp = gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
)
}
}
}

if (!is.null(data$y)) {
if(grepl("l", sides)) {
rugs$y_l <- segmentsGrob(
y0 = unit(data$y, "native"), y1 = unit(data$y, "native"),
x0 = unit(0, "npc"), x1 = unit(0.03, "npc"),
x0 = unit(0, "npc"), x1 = unit(rugwidth, "npc"),
gp = gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
)
}

if(grepl("r", sides)) {
rugs$y_r <- segmentsGrob(
y0 = unit(data$y, "native"), y1 = unit(data$y, "native"),
x0 = unit(1, "npc"), x1 = unit(0.97, "npc"),
x0 = unit(1, "npc"), x1 = unit(1 - rugwidth, "npc"),
gp = gpar(col = alpha(data$colour, data$alpha), lty = data$linetype, lwd = data$size * .pt)
)
}
}
}

gTree(children = do.call("gList", rugs))
}

default_stat <- function(.) StatIdentity
default_aes <- function(.) aes(colour="black", size=0.5, linetype=1, alpha = NA)
guide_geom <- function(.) "path"
Expand Down
4 changes: 4 additions & 0 deletions man/geom_rug.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
containing any of \code{"trbl"}, for top, right, bottom,
and left.}

\item{rugwidth}{The width the rug segments. This should
be a number in [0,1] in "npc" units. See the help for
\code{\line{unit}}.}

\item{mapping}{The aesthetic mapping, usually constructed
with \code{\link{aes}} or \code{\link{aes_string}}. Only
needs to be set at the layer level if you are overriding
Expand Down