8
8
# ' displayed as described in ?plotmath
9
9
# ' @param nudge_x,nudge_y Horizontal and vertical adjustment to nudge labels by.
10
10
# ' 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
11
13
# ' @export
12
14
# ' @examples
13
15
# ' p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
14
16
# '
15
17
# ' p + geom_text()
18
+ # ' # Avoid overlaps
19
+ # ' p + geom_text(check_overlap = TRUE)
16
20
# ' # Change size of the label
17
21
# ' p + geom_text(size = 10)
18
22
# '
45
49
# ' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
46
50
geom_text <- function (mapping = NULL , data = NULL , stat = " identity" ,
47
51
position = " identity" , parse = FALSE , ... ,
48
- nudge_x = 0 , nudge_y = 0 ) {
52
+ nudge_x = 0 , nudge_y = 0 , check_overlap = FALSE ) {
49
53
50
54
if (! missing(nudge_x ) || ! missing(nudge_y )) {
51
55
if (! missing(position )) {
@@ -56,15 +60,16 @@ geom_text <- function(mapping = NULL, data = NULL, stat = "identity",
56
60
}
57
61
58
62
GeomText $ new(mapping = mapping , data = data , stat = stat , position = position ,
59
- parse = parse , ... )
63
+ parse = parse , check_overlap = check_overlap , ... )
60
64
}
61
65
62
66
GeomText <- proto(Geom , {
63
67
objname <- " text"
64
68
65
69
draw_groups <- function (. , ... ) . $ draw(... )
66
70
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 ) {
68
73
data <- remove_missing(data , na.rm ,
69
74
c(" x" , " y" , " label" ), name = " geom_text" )
70
75
@@ -85,7 +90,8 @@ GeomText <- proto(Geom, {
85
90
fontfamily = coords $ family ,
86
91
fontface = coords $ fontface ,
87
92
lineheight = coords $ lineheight
88
- )
93
+ ),
94
+ check.overlap = check_overlap
89
95
)
90
96
}
91
97
0 commit comments