@@ -300,6 +300,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
300
300
theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
301
301
" npc" , " height"
302
302
)
303
+ panelMarginY <- panelMarginY + stripSize
303
304
# space for ticks/text in free scales
304
305
if (p $ facet $ free $ x ) {
305
306
axisTicksX <- unitConvert(
@@ -330,7 +331,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
330
331
rep(panelMarginX , 2 ),
331
332
rep(panelMarginY , 2 )
332
333
)
333
-
334
334
doms <- get_domains(nPanels , nRows , margins )
335
335
336
336
for (i in seq_len(nPanels )) {
@@ -358,6 +358,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
358
358
}
359
359
# type of unit conversion
360
360
type <- if (xy == " x" ) " height" else " width"
361
+ # get axis title
362
+ axisTitleText <- sc $ name %|| % p $ labels [[xy ]] %|| % " "
363
+ if (is_blank(axisTitle )) axisTitleText <- " "
361
364
# https://plot.ly/r/reference/#layout-xaxis
362
365
axisObj <- list (
363
366
type = " linear" ,
@@ -373,7 +376,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
373
376
ticklen = unitConvert(theme $ axis.ticks.length , " pixels" , type ),
374
377
tickwidth = unitConvert(axisTicks , " pixels" , type ),
375
378
showticklabels = ! is_blank(axisText ),
376
- tickfont = text2font(axisText , " height " ),
379
+ tickfont = text2font(axisText , type ),
377
380
tickangle = - (axisText $ angle %|| % 0 ),
378
381
showline = ! is_blank(axisLine ),
379
382
linecolor = toRGB(axisLine $ colour ),
@@ -383,7 +386,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
383
386
gridcolor = toRGB(panelGrid $ colour ),
384
387
gridwidth = unitConvert(panelGrid , " pixels" , type ),
385
388
zeroline = FALSE ,
386
- anchor = anchor
389
+ anchor = anchor ,
390
+ title = axisTitleText ,
391
+ titlefont = text2font(axisTitle )
387
392
)
388
393
# convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
389
394
# this way both dates/datetimes are on same scale
@@ -403,27 +408,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
403
408
404
409
# do some stuff that should be done once for the entire plot
405
410
if (i == 1 ) {
406
- # add space for exterior facet strips in `layout.margin`
407
- if (has_facet(p )) {
408
- stripSize <- unitConvert(stripText , " pixels" , type )
409
- if (xy == " x" ) {
410
- gglayout $ margin $ t <- gglayout $ margin $ t + stripSize
411
- }
412
- if (xy == " y" && inherits(p $ facet , " grid" )) {
413
- gglayout $ margin $ r <- gglayout $ margin $ r + stripSize
414
- }
415
- }
416
- axisTitleText <- sc $ name %|| % p $ labels [[xy ]] %|| % " "
417
- if (is_blank(axisTitle )) axisTitleText <- " "
418
411
axisTickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
419
412
side <- if (xy == " x" ) " b" else " l"
420
413
# account for axis ticks, ticks text, and titles in plot margins
421
414
# (apparently ggplot2 doesn't support axis.title/axis.text margins)
422
415
gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
423
416
bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
424
417
bbox(axisTitleText , axisTitle $ angle , unitConvert(axisTitle , " pixels" , type ))[[type ]]
425
- # draw axis titles as annotations
426
- # (plotly.js axis titles aren't smart enough to dodge ticks & text)
418
+
427
419
if (nchar(axisTitleText ) > 0 ) {
428
420
axisTextSize <- unitConvert(axisText , " npc" , type )
429
421
axisTitleSize <- unitConvert(axisTitle , " npc" , type )
@@ -432,22 +424,37 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
432
424
bbox(axisTickText , axisText $ angle , axisTextSize )[[type ]] -
433
425
bbox(axisTitleText , axisTitle $ angle , axisTitleSize )[[type ]] / 2 -
434
426
unitConvert(theme $ axis.ticks.length , " npc" , type ))
435
- # npc is on a 0-1 scale of the _entire_ device,
436
- # but these units _should_ be wrt to the plotting region
437
- # multiplying the offset by 2 seems to work, but this is a terrible hack
438
- offset <- 1.75 * offset
439
- x <- if (xy == " x" ) 0.5 else offset
440
- y <- if (xy == " x" ) offset else 0.5
441
- gglayout $ annotations <- c(
442
- gglayout $ annotations ,
443
- make_label(
444
- faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
445
- xanchor = " center" , yanchor = " middle" , annotationType = " axis"
427
+ }
428
+
429
+ # add space for exterior facet strips in `layout.margin`
430
+ if (has_facet(p )) {
431
+ stripSize <- unitConvert(stripText , " pixels" , type )
432
+ if (xy == " x" ) {
433
+ gglayout $ margin $ t <- gglayout $ margin $ t + stripSize
434
+ }
435
+ if (xy == " y" && inherits(p $ facet , " grid" )) {
436
+ gglayout $ margin $ r <- gglayout $ margin $ r + stripSize
437
+ }
438
+ # facets have multiple axis objects, but only one title for the plot,
439
+ # so we empty the titles and try to draw the title as an annotation
440
+ if (nchar(axisTitleText ) > 0 ) {
441
+ # npc is on a 0-1 scale of the _entire_ device,
442
+ # but these units _should_ be wrt to the plotting region
443
+ # multiplying the offset by 2 seems to work, but this is a terrible hack
444
+ offset <- 1.75 * offset
445
+ x <- if (xy == " x" ) 0.5 else offset
446
+ y <- if (xy == " x" ) offset else 0.5
447
+ gglayout $ annotations <- c(
448
+ gglayout $ annotations ,
449
+ make_label(
450
+ faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
451
+ xanchor = " center" , yanchor = " middle"
452
+ )
446
453
)
447
- )
454
+ }
448
455
}
449
456
}
450
-
457
+ if (has_facet( p )) gglayout [[ axisName ]] $ title <- " "
451
458
} # end of axis loop
452
459
453
460
# theme(panel.border = ) -> plotly rect shape
0 commit comments