@@ -239,47 +239,45 @@ guide_gengrob.colorbar <- function(guide, theme) {
239
239
label.position <- guide $ label.position %|| % " bottom"
240
240
if (! label.position %in% c(" top" , " bottom" )) stop(" label position \" " , label.position , " \" is invalid" )
241
241
242
- barwidth <- convertWidth (guide $ barwidth %|| % (theme $ legend.key.width * 5 ), " mm " )
243
- barheight <- convertHeight (guide $ barheight %|| % theme $ legend.key.height , " mm " )
242
+ barwidth <- width_cm (guide $ barwidth %|| % (theme $ legend.key.width * 5 ))
243
+ barheight <- height_cm (guide $ barheight %|| % theme $ legend.key.height )
244
244
},
245
245
" vertical" = {
246
246
label.position <- guide $ label.position %|| % " right"
247
247
if (! label.position %in% c(" left" , " right" )) stop(" label position \" " , label.position , " \" is invalid" )
248
248
249
- barwidth <- convertWidth (guide $ barwidth %|| % theme $ legend.key.width , " mm " )
250
- barheight <- convertHeight (guide $ barheight %|| % (theme $ legend.key.height * 5 ), " mm " )
249
+ barwidth <- width_cm (guide $ barwidth %|| % theme $ legend.key.width )
250
+ barheight <- height_cm (guide $ barheight %|| % (theme $ legend.key.height * 5 ))
251
251
})
252
252
253
- barwidth.c <- c(barwidth )
254
- barheight.c <- c(barheight )
255
- barlength.c <- switch (guide $ direction , " horizontal" = barwidth.c , " vertical" = barheight.c )
253
+ barlength <- switch (guide $ direction , " horizontal" = barwidth , " vertical" = barheight )
256
254
nbreak <- nrow(guide $ key )
257
255
258
256
grob.bar <-
259
257
if (guide $ raster ) {
260
258
image <- switch (guide $ direction , horizontal = t(guide $ bar $ colour ), vertical = rev(guide $ bar $ colour ))
261
- rasterGrob(image = image , width = barwidth.c , height = barheight.c , default.units = " mm " , gp = gpar(col = NA ), interpolate = TRUE )
259
+ rasterGrob(image = image , width = barwidth , height = barheight , default.units = " cm " , gp = gpar(col = NA ), interpolate = TRUE )
262
260
} else {
263
261
switch (guide $ direction ,
264
262
horizontal = {
265
- bw <- barwidth.c / nrow(guide $ bar )
263
+ bw <- barwidth / nrow(guide $ bar )
266
264
bx <- (seq(nrow(guide $ bar )) - 1 ) * bw
267
- rectGrob(x = bx , y = 0 , vjust = 0 , hjust = 0 , width = bw , height = barheight.c , default.units = " mm " ,
265
+ rectGrob(x = bx , y = 0 , vjust = 0 , hjust = 0 , width = bw , height = barheight , default.units = " cm " ,
268
266
gp = gpar(col = NA , fill = guide $ bar $ colour ))
269
267
},
270
268
vertical = {
271
- bh <- barheight.c / nrow(guide $ bar )
269
+ bh <- barheight / nrow(guide $ bar )
272
270
by <- (seq(nrow(guide $ bar )) - 1 ) * bh
273
- rectGrob(x = 0 , y = by , vjust = 0 , hjust = 0 , width = barwidth.c , height = bh , default.units = " mm " ,
271
+ rectGrob(x = 0 , y = by , vjust = 0 , hjust = 0 , width = barwidth , height = bh , default.units = " cm " ,
274
272
gp = gpar(col = NA , fill = guide $ bar $ colour ))
275
273
})
276
274
}
277
275
278
276
# tick and label position
279
- tic_pos.c <- rescale(guide $ key $ .value , c(0.5 , guide $ nbin - 0.5 ), guide $ bar $ value [c(1 , nrow(guide $ bar ))]) * barlength.c / guide $ nbin
280
- label_pos <- unit(tic_pos.c , " mm " )
281
- if (! guide $ draw.ulim ) tic_pos.c <- tic_pos.c [- 1 ]
282
- if (! guide $ draw.llim ) tic_pos.c <- tic_pos.c [- length(tic_pos.c )]
277
+ tick_pos <- rescale(guide $ key $ .value , c(0.5 , guide $ nbin - 0.5 ), guide $ bar $ value [c(1 , nrow(guide $ bar ))]) * barlength / guide $ nbin
278
+ label_pos <- unit(tick_pos , " cm " )
279
+ if (! guide $ draw.ulim ) tick_pos <- tick_pos [- 1 ]
280
+ if (! guide $ draw.llim ) tick_pos <- tick_pos [- length(tick_pos )]
283
281
284
282
# title
285
283
@@ -296,19 +294,16 @@ guide_gengrob.colorbar <- function(guide, theme) {
296
294
)
297
295
)
298
296
299
-
300
- title_width <- convertWidth(grobWidth(grob.title ), " mm" )
301
- title_width.c <- c(title_width )
302
- title_height <- convertHeight(grobHeight(grob.title ), " mm" )
303
- title_height.c <- c(title_height )
297
+ title_width <- width_cm(grob.title )
298
+ title_height <- height_cm(grob.title )
304
299
title_fontsize <- title.theme $ size
305
300
if (is.null(title_fontsize )) title_fontsize <- 0
306
301
307
302
# gap between keys etc
308
- hgap <- width_cm( theme $ legend.spacing.x % || % unit( 0.3 , " line " ))
309
- # multiply by 5 instead of 0.5 due to unit error below. this needs to be fixed
310
- # separately (pull request pending).
311
- vgap <- height_cm(theme $ legend.spacing.y %|| % (5 * unit(title_fontsize , " pt" )))
303
+ # the default horizontal and vertical gap need to be the same to avoid strange
304
+ # effects for certain guide layouts
305
+ hgap <- width_cm( theme $ legend.spacing.x % || % ( 0.5 * unit( title_fontsize , " pt " )))
306
+ vgap <- height_cm(theme $ legend.spacing.y %|| % (0. 5 * unit(title_fontsize , " pt" )))
312
307
313
308
# label
314
309
label.theme <- guide $ label.theme %|| % calc_element(" legend.text" , theme )
@@ -338,60 +333,58 @@ guide_gengrob.colorbar <- function(guide, theme) {
338
333
}
339
334
}
340
335
341
- label_width <- convertWidth(grobWidth(grob.label ), " mm" )
342
- label_width.c <- c(label_width )
343
- label_height <- convertHeight(grobHeight(grob.label ), " mm" )
344
- label_height.c <- c(label_height )
336
+ label_width <- width_cm(grob.label )
337
+ label_height <- height_cm(grob.label )
345
338
346
339
# ticks
347
340
grob.ticks <-
348
341
if (! guide $ ticks ) zeroGrob()
349
342
else {
350
343
switch (guide $ direction ,
351
344
" horizontal" = {
352
- x0 = rep(tic_pos.c , 2 )
353
- y0 = c(rep(0 , nbreak ), rep(barheight.c * (4 / 5 ), nbreak ))
354
- x1 = rep(tic_pos.c , 2 )
355
- y1 = c(rep(barheight.c * (1 / 5 ), nbreak ), rep(barheight.c , nbreak ))
345
+ x0 = rep(tick_pos , 2 )
346
+ y0 = c(rep(0 , nbreak ), rep(barheight * (4 / 5 ), nbreak ))
347
+ x1 = rep(tick_pos , 2 )
348
+ y1 = c(rep(barheight * (1 / 5 ), nbreak ), rep(barheight , nbreak ))
356
349
},
357
350
" vertical" = {
358
- x0 = c(rep(0 , nbreak ), rep(barwidth.c * (4 / 5 ), nbreak ))
359
- y0 = rep(tic_pos.c , 2 )
360
- x1 = c(rep(barwidth.c * (1 / 5 ), nbreak ), rep(barwidth.c , nbreak ))
361
- y1 = rep(tic_pos.c , 2 )
351
+ x0 = c(rep(0 , nbreak ), rep(barwidth * (4 / 5 ), nbreak ))
352
+ y0 = rep(tick_pos , 2 )
353
+ x1 = c(rep(barwidth * (1 / 5 ), nbreak ), rep(barwidth , nbreak ))
354
+ y1 = rep(tick_pos , 2 )
362
355
})
363
356
segmentsGrob(x0 = x0 , y0 = y0 , x1 = x1 , y1 = y1 ,
364
- default.units = " mm " , gp = gpar(col = " white" , lwd = 0.5 , lineend = " butt" ))
357
+ default.units = " cm " , gp = gpar(col = " white" , lwd = 0.5 , lineend = " butt" ))
365
358
}
366
359
367
360
# layout of bar and label
368
361
switch (guide $ direction ,
369
362
" horizontal" = {
370
363
switch (label.position ,
371
364
" top" = {
372
- bl_widths <- barwidth.c
373
- bl_heights <- c(label_height.c , vgap , barheight.c )
365
+ bl_widths <- barwidth
366
+ bl_heights <- c(label_height , vgap , barheight )
374
367
vps <- list (bar.row = 3 , bar.col = 1 ,
375
368
label.row = 1 , label.col = 1 )
376
369
},
377
370
" bottom" = {
378
- bl_widths <- barwidth.c
379
- bl_heights <- c(barheight.c , vgap , label_height.c )
371
+ bl_widths <- barwidth
372
+ bl_heights <- c(barheight , vgap , label_height )
380
373
vps <- list (bar.row = 1 , bar.col = 1 ,
381
374
label.row = 3 , label.col = 1 )
382
375
})
383
376
},
384
377
" vertical" = {
385
378
switch (label.position ,
386
379
" left" = {
387
- bl_widths <- c(label_width.c , vgap , barwidth.c )
388
- bl_heights <- barheight.c
380
+ bl_widths <- c(label_width , hgap , barwidth )
381
+ bl_heights <- barheight
389
382
vps <- list (bar.row = 1 , bar.col = 3 ,
390
383
label.row = 1 , label.col = 1 )
391
384
},
392
385
" right" = {
393
- bl_widths <- c(barwidth.c , vgap , label_width.c )
394
- bl_heights <- barheight.c
386
+ bl_widths <- c(barwidth , hgap , label_width )
387
+ bl_heights <- barheight
395
388
vps <- list (bar.row = 1 , bar.col = 1 ,
396
389
label.row = 1 , label.col = 3 )
397
390
})
@@ -400,32 +393,32 @@ guide_gengrob.colorbar <- function(guide, theme) {
400
393
# layout of title and bar+label
401
394
switch (guide $ title.position ,
402
395
" top" = {
403
- widths <- c(bl_widths , max(0 , title_width.c - sum(bl_widths )))
404
- heights <- c(title_height.c , vgap , bl_heights )
396
+ widths <- c(bl_widths , max(0 , title_width - sum(bl_widths )))
397
+ heights <- c(title_height , vgap , bl_heights )
405
398
vps <- with(vps ,
406
399
list (bar.row = bar.row + 2 , bar.col = bar.col ,
407
400
label.row = label.row + 2 , label.col = label.col ,
408
401
title.row = 1 , title.col = 1 : length(widths )))
409
402
},
410
403
" bottom" = {
411
- widths <- c(bl_widths , max(0 , title_width.c - sum(bl_widths )))
412
- heights <- c(bl_heights , vgap , title_height.c )
404
+ widths <- c(bl_widths , max(0 , title_width - sum(bl_widths )))
405
+ heights <- c(bl_heights , vgap , title_height )
413
406
vps <- with(vps ,
414
407
list (bar.row = bar.row , bar.col = bar.col ,
415
408
label.row = label.row , label.col = label.col ,
416
409
title.row = length(heights ), title.col = 1 : length(widths )))
417
410
},
418
411
" left" = {
419
- widths <- c(title_width.c , hgap , bl_widths )
420
- heights <- c(bl_heights , max(0 , title_height.c - sum(bl_heights )))
412
+ widths <- c(title_width , hgap , bl_widths )
413
+ heights <- c(bl_heights , max(0 , title_height - sum(bl_heights )))
421
414
vps <- with(vps ,
422
415
list (bar.row = bar.row , bar.col = bar.col + 2 ,
423
416
label.row = label.row , label.col = label.col + 2 ,
424
417
title.row = 1 : length(heights ), title.col = 1 ))
425
418
},
426
419
" right" = {
427
- widths <- c(bl_widths , hgap , title_width.c )
428
- heights <- c(bl_heights , max(0 , title_height.c - sum(bl_heights )))
420
+ widths <- c(bl_widths , hgap , title_width )
421
+ heights <- c(bl_heights , max(0 , title_height - sum(bl_heights )))
429
422
vps <- with(vps ,
430
423
list (bar.row = bar.row , bar.col = bar.col ,
431
424
label.row = label.row , label.col = label.col ,
@@ -436,11 +429,11 @@ guide_gengrob.colorbar <- function(guide, theme) {
436
429
grob.background <- element_render(theme , " legend.background" )
437
430
438
431
# padding
439
- padding <- convertUnit(theme $ legend.margin %|| % margin(), " mm " )
432
+ padding <- convertUnit(theme $ legend.margin %|| % margin(), " cm " )
440
433
widths <- c(padding [4 ], widths , padding [2 ])
441
434
heights <- c(padding [1 ], heights , padding [3 ])
442
435
443
- gt <- gtable(widths = unit(widths , " mm " ), heights = unit(heights , " mm " ))
436
+ gt <- gtable(widths = unit(widths , " cm " ), heights = unit(heights , " cm " ))
444
437
gt <- gtable_add_grob(gt , grob.background , name = " background" , clip = " off" ,
445
438
t = 1 , r = - 1 , b = - 1 , l = 1 )
446
439
gt <- gtable_add_grob(gt , grob.bar , name = " bar" , clip = " off" ,
0 commit comments