Skip to content

Commit 1946a75

Browse files
committed
Pass check_overlap on to textGrob. #1039
1 parent 6bf7366 commit 1946a75

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

NEWS

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ ggplot2 1.0.1.9000
22
----------------------------------------------------------------
33

44
* `geom_text()` gains `nudge_x` and `nudge_y` arguments to offset labels from
5-
the x & y position (#1120)
5+
the x & y position (#1120). `check_overlap = TRUE` provides a simple
6+
resolution to label overplotting (#1039).
67

78
* `geom_crossbar()` sets grouping correctly so you can display multiple
89
crossbars on one plot. It also makes the default `fatten` argument a little

R/geom-text.r

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
#' displayed as described in ?plotmath
99
#' @param nudge_x,nudge_y Horizontal and vertical adjustment to nudge labels by.
1010
#' Useful for offseting text from points, particularly on discrete scales.
11+
#' @param check_overlap If \code{TRUE}, text that overlaps previous text in the
12+
#' same layer will not be plotted. A quick and dirty way
1113
#' @export
1214
#' @examples
1315
#' p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
1416
#'
1517
#' p + geom_text()
18+
#' # Avoid overlaps
19+
#' p + geom_text(check_overlap = TRUE)
1620
#' # Change size of the label
1721
#' p + geom_text(size = 10)
1822
#'
@@ -45,7 +49,7 @@
4549
#' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
4650
geom_text <- function(mapping = NULL, data = NULL, stat = "identity",
4751
position = "identity", parse = FALSE, ...,
48-
nudge_x = 0, nudge_y = 0) {
52+
nudge_x = 0, nudge_y = 0, check_overlap = FALSE) {
4953

5054
if (!missing(nudge_x) || !missing(nudge_y)) {
5155
if (!missing(position)) {
@@ -56,15 +60,16 @@ geom_text <- function(mapping = NULL, data = NULL, stat = "identity",
5660
}
5761

5862
GeomText$new(mapping = mapping, data = data, stat = stat, position = position,
59-
parse = parse, ...)
63+
parse = parse, check_overlap = check_overlap, ...)
6064
}
6165

6266
GeomText <- proto(Geom, {
6367
objname <- "text"
6468

6569
draw_groups <- function(., ...) .$draw(...)
6670

67-
draw <- function(., data, scales, coordinates, ..., parse = FALSE, na.rm = FALSE) {
71+
draw <- function(., data, scales, coordinates, ..., parse = FALSE,
72+
na.rm = FALSE, check_overlap = FALSE) {
6873
data <- remove_missing(data, na.rm,
6974
c("x", "y", "label"), name = "geom_text")
7075

@@ -85,7 +90,8 @@ GeomText <- proto(Geom, {
8590
fontfamily = coords$family,
8691
fontface = coords$fontface,
8792
lineheight = coords$lineheight
88-
)
93+
),
94+
check.overlap = check_overlap
8995
)
9096
}
9197

0 commit comments

Comments
 (0)