@@ -242,6 +242,11 @@ ggplot_gtable.ggplot_built <- function(data) {
242
242
subtitle <- element_render(theme , " plot.subtitle" , plot $ labels $ subtitle , margin_y = TRUE )
243
243
subtitle_height <- grobHeight(subtitle )
244
244
245
+ # Tag
246
+ tag <- element_render(theme , " plot.tag" , plot $ labels $ tag , margin_y = TRUE , margin_x = TRUE )
247
+ tag_height <- grobHeight(tag )
248
+ tag_width <- grobWidth(tag )
249
+
245
250
# whole plot annotation
246
251
caption <- element_render(theme , " plot.caption" , plot $ labels $ caption , margin_y = TRUE )
247
252
caption_height <- grobHeight(caption )
@@ -261,6 +266,75 @@ ggplot_gtable.ggplot_built <- function(data) {
261
266
plot_table <- gtable_add_grob(plot_table , caption , name = " caption" ,
262
267
t = - 1 , b = - 1 , l = min(pans $ l ), r = max(pans $ r ), clip = " off" )
263
268
269
+ plot_table <- gtable_add_rows(plot_table , unit(0 , ' pt' ), pos = 0 )
270
+ plot_table <- gtable_add_cols(plot_table , unit(0 , ' pt' ), pos = 0 )
271
+ plot_table <- gtable_add_rows(plot_table , unit(0 , ' pt' ), pos = - 1 )
272
+ plot_table <- gtable_add_cols(plot_table , unit(0 , ' pt' ), pos = - 1 )
273
+
274
+ tag_pos <- theme $ plot.tag.position
275
+ if (length(tag_pos ) == 2 ) tag_pos <- " manual"
276
+ valid_pos <- c(" topleft" , " top" , " topright" , " left" , " right" , " bottomleft" ,
277
+ " bottom" , " bottomright" )
278
+ if (! (tag_pos == " manual" || tag_pos %in% valid_pos )) {
279
+ stop(" plot.tag.position should be a coordinate or one of " ,
280
+ paste(valid_pos , collapse = ' , ' ), call. = FALSE )
281
+ }
282
+
283
+ if (tag_pos == " manual" ) {
284
+ xpos <- theme $ plot.tag.position [1 ]
285
+ ypos <- theme $ plot.tag.position [2 ]
286
+ tag_parent <- justify_grobs(tag , x = xpos , y = ypos ,
287
+ hjust = theme $ plot.tag $ hjust ,
288
+ vjust = theme $ plot.tag $ vjust ,
289
+ debug = theme $ plot.tag $ debug )
290
+ plot_table <- gtable_add_grob(plot_table , tag_parent , name = " tag" , t = 1 ,
291
+ b = nrow(plot_table ), l = 1 ,
292
+ r = ncol(plot_table ), clip = " off" )
293
+ } else {
294
+ # Widths and heights are reassembled below instead of assigning into them
295
+ # in order to avoid bug in grid 3.2 and below.
296
+ if (tag_pos == " topleft" ) {
297
+ plot_table $ widths <- unit.c(tag_width , plot_table $ widths [- 1 ])
298
+ plot_table $ heights <- unit.c(tag_height , plot_table $ heights [- 1 ])
299
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
300
+ t = 1 , l = 1 , clip = " off" )
301
+ } else if (tag_pos == " top" ) {
302
+ plot_table $ heights <- unit.c(tag_height , plot_table $ heights [- 1 ])
303
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
304
+ t = 1 , l = 1 , r = ncol(plot_table ),
305
+ clip = " off" )
306
+ } else if (tag_pos == " topright" ) {
307
+ plot_table $ widths <- unit.c(plot_table $ widths [- ncol(plot_table )], tag_width )
308
+ plot_table $ heights <- unit.c(tag_height , plot_table $ heights [- 1 ])
309
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
310
+ t = 1 , l = ncol(plot_table ), clip = " off" )
311
+ } else if (tag_pos == " left" ) {
312
+ plot_table $ widths <- unit.c(tag_width , plot_table $ widths [- 1 ])
313
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
314
+ t = 1 , b = nrow(plot_table ), l = 1 ,
315
+ clip = " off" )
316
+ } else if (tag_pos == " right" ) {
317
+ plot_table $ widths <- unit.c(plot_table $ widths [- ncol(plot_table )], tag_width )
318
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
319
+ t = 1 , b = nrow(plot_table ), l = ncol(plot_table ),
320
+ clip = " off" )
321
+ } else if (tag_pos == " bottomleft" ) {
322
+ plot_table $ widths <- unit.c(tag_width , plot_table $ widths [- 1 ])
323
+ plot_table $ heights <- unit.c(plot_table $ heights [- nrow(plot_table )], tag_height )
324
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
325
+ t = nrow(plot_table ), l = 1 , clip = " off" )
326
+ } else if (tag_pos == " bottom" ) {
327
+ plot_table $ heights <- unit.c(plot_table $ heights [- nrow(plot_table )], tag_height )
328
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
329
+ t = nrow(plot_table ), l = 1 , r = ncol(plot_table ), clip = " off" )
330
+ } else if (tag_pos == " bottomright" ) {
331
+ plot_table $ widths <- unit.c(plot_table $ widths [- ncol(plot_table )], tag_width )
332
+ plot_table $ heights <- unit.c(plot_table $ heights [- nrow(plot_table )], tag_height )
333
+ plot_table <- gtable_add_grob(plot_table , tag , name = " tag" ,
334
+ t = nrow(plot_table ), l = ncol(plot_table ), clip = " off" )
335
+ }
336
+ }
337
+
264
338
# Margins
265
339
plot_table <- gtable_add_rows(plot_table , theme $ plot.margin [1 ], pos = 0 )
266
340
plot_table <- gtable_add_cols(plot_table , theme $ plot.margin [2 ])
0 commit comments