@@ -335,6 +335,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
335335 }
336336 # type of unit conversion
337337 type <- if (xy == " x" ) " height" else " width"
338+ # get axis title
339+ axisTitleText <- sc $ name %|| % p $ labels [[xy ]] %|| % " "
340+ if (is_blank(axisTitle )) axisTitleText <- " "
338341 # https://plot.ly/r/reference/#layout-xaxis
339342 axisObj <- list (
340343 type = " linear" ,
@@ -360,7 +363,10 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
360363 gridcolor = toRGB(panelGrid $ colour ),
361364 gridwidth = unitConvert(panelGrid , " pixels" , type ),
362365 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
364370 )
365371 # convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
366372 # this way both dates/datetimes are on same scale
@@ -380,27 +386,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
380386
381387 # do some stuff that should be done once for the entire plot
382388 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 <- " "
395389 axisTickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
396390 side <- if (xy == " x" ) " b" else " l"
397391 # account for axis ticks, ticks text, and titles in plot margins
398392 # (apparently ggplot2 doesn't support axis.title/axis.text margins)
399393 gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
400394 bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
401395 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+
404397 if (nchar(axisTitleText ) > 0 ) {
405398 axisTextSize <- unitConvert(axisText , " npc" , type )
406399 axisTitleSize <- unitConvert(axisTitle , " npc" , type )
@@ -409,21 +402,42 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
409402 bbox(axisTickText , axisText $ angle , axisTextSize )[[type ]] -
410403 bbox(axisTitleText , axisTitle $ angle , axisTitleSize )[[type ]] / 2 -
411404 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+ )
423431 )
424- )
432+ }
425433 }
426434 }
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+ }
427441
428442 } # end of axis loop
429443
0 commit comments