Skip to content

Commit c5f38c5

Browse files
committed
boilerplate geom_text()
1 parent 015cdc1 commit c5f38c5

File tree

1 file changed

+53
-78
lines changed

1 file changed

+53
-78
lines changed

R/geom-text.R

Lines changed: 53 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,55 @@
1+
#' @rdname ggplot2-ggproto
2+
#' @format NULL
3+
#' @usage NULL
4+
#' @export
5+
GeomText <- ggproto(
6+
"GeomText", Geom,
7+
required_aes = c("x", "y", "label"),
8+
9+
non_missing_aes = "angle",
10+
11+
default_aes = aes(
12+
colour = from_theme(colour %||% ink),
13+
family = from_theme(family),
14+
size = from_theme(fontsize),
15+
angle = 0, hjust = 0.5,
16+
vjust = 0.5, alpha = NA, fontface = 1, lineheight = 1.2
17+
),
18+
19+
draw_panel = function(data, panel_params, coord, parse = FALSE,
20+
na.rm = FALSE, check_overlap = FALSE,
21+
size.unit = "mm") {
22+
lab <- data$label
23+
if (parse) {
24+
lab <- parse_safe(as.character(lab))
25+
}
26+
27+
data <- coord$transform(data, panel_params)
28+
29+
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
30+
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
31+
32+
size.unit <- resolve_text_unit(size.unit)
33+
34+
textGrob(
35+
lab,
36+
data$x, data$y, default.units = "native",
37+
hjust = data$hjust, vjust = data$vjust,
38+
rot = data$angle,
39+
gp = gg_par(
40+
col = alpha(data$colour, data$alpha),
41+
fontsize = data$size * size.unit,
42+
fontfamily = data$family,
43+
fontface = data$fontface,
44+
lineheight = data$lineheight
45+
),
46+
check.overlap = check_overlap
47+
)
48+
},
49+
50+
draw_key = draw_key_text
51+
)
52+
153
#' Text
254
#'
355
#' Text geoms are useful for labeling plots. They can be used by themselves as
@@ -152,84 +204,7 @@
152204
#' ggplot(df, aes(x, y)) +
153205
#' geom_text(aes(label = text), vjust = "inward", hjust = "inward")
154206
#' }
155-
geom_text <- function(mapping = NULL, data = NULL,
156-
stat = "identity", position = "nudge",
157-
...,
158-
parse = FALSE,
159-
check_overlap = FALSE,
160-
size.unit = "mm",
161-
na.rm = FALSE,
162-
show.legend = NA,
163-
inherit.aes = TRUE)
164-
{
165-
layer(
166-
data = data,
167-
mapping = mapping,
168-
stat = stat,
169-
geom = GeomText,
170-
position = position,
171-
show.legend = show.legend,
172-
inherit.aes = inherit.aes,
173-
params = list2(
174-
parse = parse,
175-
check_overlap = check_overlap,
176-
size.unit = size.unit,
177-
na.rm = na.rm,
178-
...
179-
)
180-
)
181-
}
182-
183-
#' @rdname ggplot2-ggproto
184-
#' @format NULL
185-
#' @usage NULL
186-
#' @export
187-
GeomText <- ggproto("GeomText", Geom,
188-
required_aes = c("x", "y", "label"),
189-
190-
non_missing_aes = "angle",
191-
192-
default_aes = aes(
193-
colour = from_theme(colour %||% ink),
194-
family = from_theme(family),
195-
size = from_theme(fontsize),
196-
angle = 0, hjust = 0.5,
197-
vjust = 0.5, alpha = NA, fontface = 1, lineheight = 1.2
198-
),
199-
200-
draw_panel = function(data, panel_params, coord, parse = FALSE,
201-
na.rm = FALSE, check_overlap = FALSE,
202-
size.unit = "mm") {
203-
lab <- data$label
204-
if (parse) {
205-
lab <- parse_safe(as.character(lab))
206-
}
207-
208-
data <- coord$transform(data, panel_params)
209-
210-
data$vjust <- compute_just(data$vjust, data$y, data$x, data$angle)
211-
data$hjust <- compute_just(data$hjust, data$x, data$y, data$angle)
212-
213-
size.unit <- resolve_text_unit(size.unit)
214-
215-
textGrob(
216-
lab,
217-
data$x, data$y, default.units = "native",
218-
hjust = data$hjust, vjust = data$vjust,
219-
rot = data$angle,
220-
gp = gg_par(
221-
col = alpha(data$colour, data$alpha),
222-
fontsize = data$size * size.unit,
223-
fontfamily = data$family,
224-
fontface = data$fontface,
225-
lineheight = data$lineheight
226-
),
227-
check.overlap = check_overlap
228-
)
229-
},
230-
231-
draw_key = draw_key_text
232-
)
207+
geom_text <- make_constructor(GeomText, position = "nudge")
233208

234209
compute_just <- function(just, a = 0.5, b = a, angle = 0) {
235210
if (!is.character(just)) {

0 commit comments

Comments
 (0)