Skip to content

Commit 7fa656f

Browse files
committed
Modernise geom_text
1 parent 069abcb commit 7fa656f

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

R/geom-text.r

Lines changed: 39 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,47 @@
88
#' displayed as described in ?plotmath
99
#' @export
1010
#' @examples
11-
#' \donttest{
1211
#' p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
1312
#'
1413
#' p + geom_text()
1514
#' # Change size of the label
1615
#' p + geom_text(size=10)
17-
#' p <- p + geom_point()
1816
#'
1917
#' # Set aesthetics to fixed value
20-
#' p + geom_text()
21-
#' p + geom_point() + geom_text(hjust=0, vjust=0)
18+
#' p + geom_point() + geom_text(hjust = 0, vjust = 0)
2219
#' p + geom_point() + geom_text(angle = 45)
20+
#' p + geom_text(family = "Times New Roman")
2321
#'
2422
#' # Add aesthetic mappings
25-
#' p + geom_text(aes(colour=factor(cyl)))
26-
#' p + geom_text(aes(colour=factor(cyl))) + scale_colour_discrete(l=40)
23+
#' p + geom_text(aes(colour = factor(cyl)))
24+
#' p + geom_text(aes(colour = factor(cyl))) +
25+
#' scale_colour_discrete(l = 40)
2726
#'
28-
#' p + geom_text(aes(size=wt))
29-
#' p + geom_text(aes(size=wt)) + scale_size(range=c(3,6))
27+
#' p + geom_text(aes(size = wt))
28+
#' # Scale height of text, rather than sqrt(height)
29+
#' p + geom_text(aes(size = wt)) + scale_radius(range = c(3,6))
3030
#'
3131
#' # You can display expressions by setting parse = TRUE. The
3232
#' # details of the display are described in ?plotmath, but note that
3333
#' # geom_text uses strings, not expressions.
3434
#' p + geom_text(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
3535
#' parse = TRUE)
3636
#'
37-
#' # Add an annotation not from a variable source
38-
#' c <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
39-
#' c + geom_text(data = NULL, x = 5, y = 30, label = "plot mpg vs. wt")
40-
#' # Or, you can use annotate
41-
#' c + annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
42-
#'
43-
#' # You can specify family, fontface and lineheight
44-
#' p <- ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars)))
45-
#' p + geom_text(fontface=3)
46-
#' p + geom_text(aes(fontface=am+1))
47-
#' p + geom_text(aes(family=c("serif", "mono")[am+1]))
48-
#' }
49-
geom_text <- function (mapping = NULL, data = NULL, stat = "identity", position = "identity",
50-
parse = FALSE, ...) {
37+
#' # Add a text annotation
38+
#' p +
39+
#' geom_text() +
40+
#' annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
41+
geom_text <- function(mapping = NULL, data = NULL, stat = "identity",
42+
position = "identity", parse = FALSE, ...) {
5143
GeomText$new(mapping = mapping, data = data, stat = stat, position = position,
52-
parse = parse, ...)
44+
parse = parse, ...)
5345
}
5446

5547
GeomText <- proto(Geom, {
5648
objname <- "text"
5749

5850
draw_groups <- function(., ...) .$draw(...)
51+
5952
draw <- function(., data, scales, coordinates, ..., parse = FALSE, na.rm = FALSE) {
6053
data <- remove_missing(data, na.rm,
6154
c("x", "y", "label"), name = "geom_text")
@@ -65,27 +58,40 @@ GeomText <- proto(Geom, {
6558
lab <- parse(text = lab)
6659
}
6760

68-
with(coord_transform(coordinates, data, scales),
69-
textGrob(lab, x, y, default.units="native",
70-
hjust=hjust, vjust=vjust, rot=angle,
71-
gp = gpar(col = alpha(colour, alpha), fontsize = size * .pt,
72-
fontfamily = family, fontface = fontface, lineheight = lineheight))
61+
coords <- coord_transform(coordinates, data, scales)
62+
textGrob(
63+
lab,
64+
coords$x, coords$y, default.units = "native",
65+
hjust = coords$hjust, vjust = coords$vjust,
66+
rot = coords$angle,
67+
gp = gpar(
68+
col = alpha(coords$colour, coords$alpha),
69+
fontsize = coords$size * .pt,
70+
fontfamily = coords$family,
71+
fontface = coords$fontface,
72+
lineheight = coords$lineheight
73+
)
7374
)
7475
}
7576

7677
draw_legend <- function(., data, ...) {
7778
data <- aesdefaults(data, .$default_aes(), list(...))
78-
with(data,
79-
textGrob("a", 0.5, 0.5, rot = angle,
80-
gp=gpar(col=alpha(colour, alpha), fontsize = size * .pt))
79+
textGrob(
80+
"a", 0.5, 0.5,
81+
rot = data$angle,
82+
gp = gpar(
83+
col = alpha(data$colour, data$alpha),
84+
fontsize = data$size * .pt
85+
)
8186
)
8287
}
8388

8489

8590
default_stat <- function(.) StatIdentity
8691
required_aes <- c("x", "y", "label")
87-
default_aes <- function(.) aes(colour="black", size=5 , angle=0, hjust=0.5,
88-
vjust=0.5, alpha = NA, family="", fontface=1, lineheight=1.2)
92+
default_aes <- function(.) aes(colour = "black", size = 5, angle = 0,
93+
hjust = 0.5, vjust = 0.5, alpha = NA, family = "", fontface = 1,
94+
lineheight = 1.2)
8995
guide_geom <- function(x) "text"
9096

9197
})

man/geom_text.Rd

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,43 +36,35 @@ Textual annotations.
3636
\Sexpr[results=rd,stage=build]{ggplot2:::rd_aesthetics("geom", "text")}
3737
}
3838
\examples{
39-
\donttest{
4039
p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
4140

4241
p + geom_text()
4342
# Change size of the label
4443
p + geom_text(size=10)
45-
p <- p + geom_point()
4644

4745
# Set aesthetics to fixed value
48-
p + geom_text()
49-
p + geom_point() + geom_text(hjust=0, vjust=0)
46+
p + geom_point() + geom_text(hjust = 0, vjust = 0)
5047
p + geom_point() + geom_text(angle = 45)
48+
p + geom_text(family = "Times New Roman")
5149

5250
# Add aesthetic mappings
53-
p + geom_text(aes(colour=factor(cyl)))
54-
p + geom_text(aes(colour=factor(cyl))) + scale_colour_discrete(l=40)
51+
p + geom_text(aes(colour = factor(cyl)))
52+
p + geom_text(aes(colour = factor(cyl))) +
53+
scale_colour_discrete(l = 40)
5554

56-
p + geom_text(aes(size=wt))
57-
p + geom_text(aes(size=wt)) + scale_size(range=c(3,6))
55+
p + geom_text(aes(size = wt))
56+
# Scale height of text, rather than sqrt(height)
57+
p + geom_text(aes(size = wt)) + scale_radius(range = c(3,6))
5858

5959
# You can display expressions by setting parse = TRUE. The
6060
# details of the display are described in ?plotmath, but note that
6161
# geom_text uses strings, not expressions.
6262
p + geom_text(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
6363
parse = TRUE)
6464

65-
# Add an annotation not from a variable source
66-
c <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
67-
c + geom_text(data = NULL, x = 5, y = 30, label = "plot mpg vs. wt")
68-
# Or, you can use annotate
69-
c + annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
70-
71-
# You can specify family, fontface and lineheight
72-
p <- ggplot(mtcars, aes(x=wt, y=mpg, label=rownames(mtcars)))
73-
p + geom_text(fontface=3)
74-
p + geom_text(aes(fontface=am+1))
75-
p + geom_text(aes(family=c("serif", "mono")[am+1]))
76-
}
65+
# Add a text annotation
66+
p +
67+
geom_text() +
68+
annotate("text", label = "plot mpg vs. wt", x = 2, y = 15, size = 8, colour = "red")
7769
}
7870

0 commit comments

Comments
 (0)