-
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In issue #129 @hmhensen reports that ggrepel is inconsistent with ggplot2 in the way it handles `nudge_x` and `nudge_y`. This commit introduces a new internal function `position_dodge2()` (not exported) to address the issue.
- Loading branch information
Showing
3 changed files
with
104 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#' Nudge points a fixed distance | ||
#' | ||
#' `position_nudge` is generally useful for adjusting the position of | ||
#' items on discrete scales by a small amount. Nudging is built in to | ||
#' [geom_text()] because it's so useful for moving labels a small | ||
#' distance from what they're labelling. | ||
#' | ||
#' @family position adjustments | ||
#' @param x,y Amount of vertical and horizontal distance to move. | ||
#' @examples | ||
#' df <- data.frame( | ||
#' x = c(1,3,2,5), | ||
#' y = c("a","c","d","c") | ||
#' ) | ||
#' | ||
#' ggplot(df, aes(x, y)) + | ||
#' geom_point() + | ||
#' geom_text(aes(label = y)) | ||
#' | ||
#' ggplot(df, aes(x, y)) + | ||
#' geom_point() + | ||
#' geom_text(aes(label = y), position = position_nudge(y = -0.1)) | ||
#' | ||
#' # Or, in brief | ||
#' ggplot(df, aes(x, y)) + | ||
#' geom_point() + | ||
#' geom_text(aes(label = y), nudge_y = -0.1) | ||
position_nudge2 <- function(x = 0, y = 0) { | ||
ggproto(NULL, PositionNudge2, | ||
x = x, | ||
y = y | ||
) | ||
} | ||
|
||
#' @rdname ggplot2-ggproto | ||
#' @format NULL | ||
#' @usage NULL | ||
PositionNudge2 <- ggproto("PositionNudge2", Position, | ||
x = 0, | ||
y = 0, | ||
|
||
setup_params = function(self, data) { | ||
list(x = self$x, y = self$y) | ||
}, | ||
|
||
compute_layer = function(data, params, panel) { | ||
x_orig <- data$x | ||
y_orig <- data$y | ||
# transform only the dimensions for which non-zero nudging is requested | ||
if (any(params$x != 0)) { | ||
if (any(params$y != 0)) { | ||
data <- transform_position(data, function(x) x + params$x, function(y) y + params$y) | ||
} else { | ||
data <- transform_position(data, function(x) x + params$x, NULL) | ||
} | ||
} else if (any(params$y != 0)) { | ||
data <- transform_position(data, NULL, function(y) y + params$y) | ||
} | ||
data$nudge_x <- data$x | ||
data$nudge_y <- data$y | ||
data$x <- x_orig | ||
data$y <- y_orig | ||
data | ||
} | ||
) |