@@ -335,6 +335,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
335
335
}
336
336
# type of unit conversion
337
337
type <- if (xy == " x" ) " height" else " width"
338
+ # get axis title
339
+ axisTitleText <- sc $ name %|| % p $ labels [[xy ]] %|| % " "
340
+ if (is_blank(axisTitle )) axisTitleText <- " "
338
341
# https://plot.ly/r/reference/#layout-xaxis
339
342
axisObj <- list (
340
343
type = " linear" ,
@@ -360,7 +363,10 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
360
363
gridcolor = toRGB(panelGrid $ colour ),
361
364
gridwidth = unitConvert(panelGrid , " pixels" , type ),
362
365
zeroline = FALSE ,
363
- anchor = anchor
366
+ anchor = anchor ,
367
+ # if not facets then use Plotly axis titling mechanism
368
+ # see https://github.com/ropensci/plotly/issues/510
369
+ title = axisTitleText
364
370
)
365
371
# convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
366
372
# this way both dates/datetimes are on same scale
@@ -380,27 +386,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
380
386
381
387
# do some stuff that should be done once for the entire plot
382
388
if (i == 1 ) {
383
- # add space for exterior facet strips in `layout.margin`
384
- if (has_facet(p )) {
385
- stripSize <- unitConvert(stripText , " pixels" , type )
386
- if (xy == " x" ) {
387
- gglayout $ margin $ t <- gglayout $ margin $ t + stripSize
388
- }
389
- if (xy == " y" && inherits(p $ facet , " grid" )) {
390
- gglayout $ margin $ r <- gglayout $ margin $ r + stripSize
391
- }
392
- }
393
- axisTitleText <- sc $ name %|| % p $ labels [[xy ]] %|| % " "
394
- if (is_blank(axisTitle )) axisTitleText <- " "
395
389
axisTickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
396
390
side <- if (xy == " x" ) " b" else " l"
397
391
# account for axis ticks, ticks text, and titles in plot margins
398
392
# (apparently ggplot2 doesn't support axis.title/axis.text margins)
399
393
gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
400
394
bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
401
395
bbox(axisTitleText , axisTitle $ angle , unitConvert(axisTitle , " pixels" , type ))[[type ]]
402
- # draw axis titles as annotations
403
- # (plotly.js axis titles aren't smart enough to dodge ticks & text)
396
+
404
397
if (nchar(axisTitleText ) > 0 ) {
405
398
axisTextSize <- unitConvert(axisText , " npc" , type )
406
399
axisTitleSize <- unitConvert(axisTitle , " npc" , type )
@@ -409,21 +402,42 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
409
402
bbox(axisTickText , axisText $ angle , axisTextSize )[[type ]] -
410
403
bbox(axisTitleText , axisTitle $ angle , axisTitleSize )[[type ]] / 2 -
411
404
unitConvert(theme $ axis.ticks.length , " npc" , type ))
412
- # npc is on a 0-1 scale of the _entire_ device,
413
- # but these units _should_ be wrt to the plotting region
414
- # multiplying the offset by 2 seems to work, but this is a terrible hack
415
- offset <- 1.75 * offset
416
- x <- if (xy == " x" ) 0.5 else offset
417
- y <- if (xy == " x" ) offset else 0.5
418
- gglayout $ annotations <- c(
419
- gglayout $ annotations ,
420
- make_label(
421
- faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
422
- xanchor = " center" , yanchor = " middle"
405
+ }
406
+
407
+ # add space for exterior facet strips in `layout.margin`
408
+ if (has_facet(p )) {
409
+ stripSize <- unitConvert(stripText , " pixels" , type )
410
+ if (xy == " x" ) {
411
+ gglayout $ margin $ t <- gglayout $ margin $ t + stripSize
412
+ }
413
+ if (xy == " y" && inherits(p $ facet , " grid" )) {
414
+ gglayout $ margin $ r <- gglayout $ margin $ r + stripSize
415
+ }
416
+ # draw axis titles as annotations
417
+ # (plotly.js axis titles aren't smart enough to dodge ticks & text)
418
+ if (nchar(axisTitleText ) > 0 ) {
419
+ # npc is on a 0-1 scale of the _entire_ device,
420
+ # but these units _should_ be wrt to the plotting region
421
+ # multiplying the offset by 2 seems to work, but this is a terrible hack
422
+ offset <- 1.75 * offset
423
+ x <- if (xy == " x" ) 0.5 else offset
424
+ y <- if (xy == " x" ) offset else 0.5
425
+ gglayout $ annotations <- c(
426
+ gglayout $ annotations ,
427
+ make_label(
428
+ faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
429
+ xanchor = " center" , yanchor = " middle"
430
+ )
423
431
)
424
- )
432
+ }
425
433
}
426
434
}
435
+
436
+ if (has_facet(p )) {
437
+ # turn off plotly axis titles
438
+ # since we need special treatment for facets
439
+ gglayout [[axisName ]]$ title <- " "
440
+ }
427
441
428
442
} # end of axis loop
429
443
0 commit comments