|
6 | 6 | #' @inheritParams geom_point
|
7 | 7 | #' @param parse If TRUE, the labels will be parsed into expressions and
|
8 | 8 | #' 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. |
9 | 11 | #' @export
|
10 | 12 | #' @examples
|
11 | 13 | #' p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
|
12 | 14 | #'
|
13 | 15 | #' p + geom_text()
|
14 | 16 | #' # Change size of the label
|
15 |
| -#' p + geom_text(size=10) |
| 17 | +#' p + geom_text(size = 10) |
16 | 18 | #'
|
17 | 19 | #' # 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) |
19 | 22 | #' p + geom_point() + geom_text(angle = 45)
|
| 23 | +#' \dontrun{ |
20 | 24 | #' p + geom_text(family = "Times New Roman")
|
| 25 | +#' } |
21 | 26 | #'
|
22 | 27 | #' # Add aesthetic mappings
|
23 | 28 | #' p + geom_text(aes(colour = factor(cyl)))
|
|
39 | 44 | #' geom_text() +
|
40 | 45 | #' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
|
41 | 46 | 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 | + |
43 | 58 | GeomText$new(mapping = mapping, data = data, stat = stat, position = position,
|
44 | 59 | parse = parse, ...)
|
45 | 60 | }
|
|
0 commit comments