8
8
# ' displayed as described in ?plotmath
9
9
# ' @export
10
10
# ' @examples
11
- # ' \donttest{
12
11
# ' p <- ggplot(mtcars, aes(wt, mpg, label = rownames(mtcars)))
13
12
# '
14
13
# ' p + geom_text()
15
14
# ' # Change size of the label
16
15
# ' p + geom_text(size=10)
17
- # ' p <- p + geom_point()
18
16
# '
19
17
# ' # 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)
22
19
# ' p + geom_point() + geom_text(angle = 45)
20
+ # ' p + geom_text(family = "Times New Roman")
23
21
# '
24
22
# ' # 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)
27
26
# '
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))
30
30
# '
31
31
# ' # You can display expressions by setting parse = TRUE. The
32
32
# ' # details of the display are described in ?plotmath, but note that
33
33
# ' # geom_text uses strings, not expressions.
34
34
# ' p + geom_text(aes(label = paste(wt, "^(", cyl, ")", sep = "")),
35
35
# ' parse = TRUE)
36
36
# '
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 , ... ) {
51
43
GeomText $ new(mapping = mapping , data = data , stat = stat , position = position ,
52
- parse = parse , ... )
44
+ parse = parse , ... )
53
45
}
54
46
55
47
GeomText <- proto(Geom , {
56
48
objname <- " text"
57
49
58
50
draw_groups <- function (. , ... ) . $ draw(... )
51
+
59
52
draw <- function (. , data , scales , coordinates , ... , parse = FALSE , na.rm = FALSE ) {
60
53
data <- remove_missing(data , na.rm ,
61
54
c(" x" , " y" , " label" ), name = " geom_text" )
@@ -65,27 +58,40 @@ GeomText <- proto(Geom, {
65
58
lab <- parse(text = lab )
66
59
}
67
60
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
+ )
73
74
)
74
75
}
75
76
76
77
draw_legend <- function (. , data , ... ) {
77
78
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
+ )
81
86
)
82
87
}
83
88
84
89
85
90
default_stat <- function (. ) StatIdentity
86
91
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 )
89
95
guide_geom <- function (x ) " text"
90
96
91
97
})
0 commit comments