Skip to content

Commit 6bf7366

Browse files
committed
Add nudge_x and nudge_y params to geom_text
1 parent 7fa656f commit 6bf7366

File tree

3 files changed

+30
-6
lines changed

3 files changed

+30
-6
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
ggplot2 1.0.1.9000
22
----------------------------------------------------------------
33

4+
* `geom_text()` gains `nudge_x` and `nudge_y` arguments to offset labels from
5+
the x & y position (#1120)
6+
47
* `geom_crossbar()` sets grouping correctly so you can display multiple
58
crossbars on one plot. It also makes the default `fatten` argument a little
69
bigger to make the middle line more obvious (#1125).

R/geom-text.r

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,23 @@
66
#' @inheritParams geom_point
77
#' @param parse If TRUE, the labels will be parsed into expressions and
88
#' displayed as described in ?plotmath
9+
#' @param nudge_x,nudge_y Horizontal and vertical adjustment to nudge labels by.
10+
#' Useful for offseting text from points, particularly on discrete scales.
911
#' @export
1012
#' @examples
1113
#' p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
1214
#'
1315
#' p + geom_text()
1416
#' # Change size of the label
15-
#' p + geom_text(size=10)
17+
#' p + geom_text(size = 10)
1618
#'
1719
#' # Set aesthetics to fixed value
18-
#' p + geom_point() + geom_text(hjust = 0, vjust = 0)
20+
#' p + geom_point() + geom_text(hjust = 0, nudge_x = 0.05)
21+
#' p + geom_point() + geom_text(vjust = 0, nudge_y = 0.5)
1922
#' p + geom_point() + geom_text(angle = 45)
23+
#' \dontrun{
2024
#' p + geom_text(family = "Times New Roman")
25+
#' }
2126
#'
2227
#' # Add aesthetic mappings
2328
#' p + geom_text(aes(colour = factor(cyl)))
@@ -39,7 +44,17 @@
3944
#' geom_text() +
4045
#' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
4146
geom_text <- function(mapping = NULL, data = NULL, stat = "identity",
42-
position = "identity", parse = FALSE, ...) {
47+
position = "identity", parse = FALSE, ...,
48+
nudge_x = 0, nudge_y = 0) {
49+
50+
if (!missing(nudge_x) || !missing(nudge_y)) {
51+
if (!missing(position)) {
52+
stop("Specify either `position` or `nudge_x`/`nudge_y`", call. = FALSE)
53+
}
54+
55+
position <- position_nudge(nudge_x, nudge_y)
56+
}
57+
4358
GeomText$new(mapping = mapping, data = data, stat = stat, position = position,
4459
parse = parse, ...)
4560
}

man/geom_text.Rd

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
\title{Textual annotations.}
66
\usage{
77
geom_text(mapping = NULL, data = NULL, stat = "identity",
8-
position = "identity", parse = FALSE, ...)
8+
position = "identity", parse = FALSE, ..., nudge_x = 0, nudge_y = 0)
99
}
1010
\arguments{
1111
\item{mapping}{The aesthetic mapping, usually constructed with
@@ -27,6 +27,9 @@ displayed as described in ?plotmath}
2727
\item{...}{other arguments passed on to \code{\link{layer}}. This can
2828
include aesthetics whose values you want to set, not map. See
2929
\code{\link{layer}} for more details.}
30+
31+
\item{nudge_x,nudge_y}{Horizontal and vertical adjustment to nudge labels by.
32+
Useful for offseting text from points, particularly on discrete scales.}
3033
}
3134
\description{
3235
Textual annotations.
@@ -40,12 +43,15 @@ p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
4043

4144
p + geom_text()
4245
# Change size of the label
43-
p + geom_text(size=10)
46+
p + geom_text(size = 10)
4447

4548
# Set aesthetics to fixed value
46-
p + geom_point() + geom_text(hjust = 0, vjust = 0)
49+
p + geom_point() + geom_text(hjust = 0, nudge_x = 0.05)
50+
p + geom_point() + geom_text(vjust = 0, nudge_y = 0.5)
4751
p + geom_point() + geom_text(angle = 45)
52+
\dontrun{
4853
p + geom_text(family = "Times New Roman")
54+
}
4955

5056
# Add aesthetic mappings
5157
p + geom_text(aes(colour = factor(cyl)))

0 commit comments

Comments
 (0)