@@ -99,7 +99,17 @@ build_legend <- function(name, mapping, layers, default_mapping, theme) {
99
99
nkeys <- nrow(mapping )
100
100
hgap <- vgap <- unit(0.3 , " lines" )
101
101
102
- label_width <- max(stringWidth(mapping $ .label ))
102
+ numeric_labels <- all(sapply(mapping $ .labels , is.language )) || suppressWarnings(all(! is.na(sapply(mapping $ .labels , " as.numeric" ))))
103
+ hpos <- numeric_labels * 1
104
+
105
+ labels <- lapply(mapping $ .label , function (label ) {
106
+ theme_render(theme , " legend.text" , label , hjust = hpos , x = hpos , y = 0.5 )
107
+ })
108
+
109
+ label_width <- do.call(" max" , lapply(labels , grobWidth ))
110
+ label_width <- convertWidth(label_width , " cm" )
111
+ label_heights <- do.call(" unit.c" , lapply(labels , grobHeight ))
112
+ label_heights <- convertHeight(label_heights , " cm" )
103
113
104
114
width <- max(unlist(llply(legend_data , " [[" , " size" )), 0 )
105
115
key_width <- max(theme $ legend.key.size , unit(width , " mm" ))
@@ -112,20 +122,20 @@ build_legend <- function(name, mapping, layers, default_mapping, theme) {
112
122
hgap
113
123
)
114
124
)
115
-
116
- label.heights <- stringHeight(mapping $ .label )
125
+ widths <- convertWidth(widths , " cm" )
117
126
118
127
heights <- unit.c(
119
128
vgap ,
120
129
unit(1 , " grobheight" , title ),
121
130
vgap ,
122
131
unit.pmax(
123
132
theme $ legend.key.size ,
124
- label.heights ,
133
+ label_heights ,
125
134
unit(key_heights , " mm" )
126
135
),
127
136
vgap
128
137
)
138
+ heights <- convertHeight(heights , " cm" )
129
139
130
140
# Layout the legend table
131
141
legend.layout <- grid.layout(
@@ -136,8 +146,6 @@ build_legend <- function(name, mapping, layers, default_mapping, theme) {
136
146
fg <- ggname(" legend" , frameGrob(layout = legend.layout ))
137
147
fg <- placeGrob(fg , theme_render(theme , " legend.background" ))
138
148
139
- numeric_labels <- all(sapply(mapping $ .labels , is.language )) || suppressWarnings(all(! is.na(sapply(mapping $ .labels , " as.numeric" ))))
140
- hpos <- numeric_labels * 1
141
149
142
150
fg <- placeGrob(fg , title , col = 2 : 4 , row = 2 )
143
151
for (i in 1 : nkeys ) {
0 commit comments