@@ -300,6 +300,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
300300 theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
301301 " npc" , " height"
302302 )
303+ panelMarginY <- panelMarginY + stripSize
303304 # space for ticks/text in free scales
304305 if (p $ facet $ free $ x ) {
305306 axisTicksX <- unitConvert(
@@ -330,7 +331,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
330331 rep(panelMarginX , 2 ),
331332 rep(panelMarginY , 2 )
332333 )
333-
334334 doms <- get_domains(nPanels , nRows , margins )
335335
336336 for (i in seq_len(nPanels )) {
@@ -358,6 +358,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
358358 }
359359 # type of unit conversion
360360 type <- if (xy == " x" ) " height" else " width"
361+ # get axis title
362+ axisTitleText <- sc $ name %|| % p $ labels [[xy ]] %|| % " "
363+ if (is_blank(axisTitle )) axisTitleText <- " "
361364 # https://plot.ly/r/reference/#layout-xaxis
362365 axisObj <- list (
363366 type = " linear" ,
@@ -373,7 +376,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
373376 ticklen = unitConvert(theme $ axis.ticks.length , " pixels" , type ),
374377 tickwidth = unitConvert(axisTicks , " pixels" , type ),
375378 showticklabels = ! is_blank(axisText ),
376- tickfont = text2font(axisText , " height " ),
379+ tickfont = text2font(axisText , type ),
377380 tickangle = - (axisText $ angle %|| % 0 ),
378381 showline = ! is_blank(axisLine ),
379382 linecolor = toRGB(axisLine $ colour ),
@@ -383,7 +386,9 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
383386 gridcolor = toRGB(panelGrid $ colour ),
384387 gridwidth = unitConvert(panelGrid , " pixels" , type ),
385388 zeroline = FALSE ,
386- anchor = anchor
389+ anchor = anchor ,
390+ title = axisTitleText ,
391+ titlefont = text2font(axisTitle )
387392 )
388393 # convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
389394 # this way both dates/datetimes are on same scale
@@ -403,27 +408,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
403408
404409 # do some stuff that should be done once for the entire plot
405410 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 <- " "
418411 axisTickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
419412 side <- if (xy == " x" ) " b" else " l"
420413 # account for axis ticks, ticks text, and titles in plot margins
421414 # (apparently ggplot2 doesn't support axis.title/axis.text margins)
422415 gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
423416 bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
424417 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+
427419 if (nchar(axisTitleText ) > 0 ) {
428420 axisTextSize <- unitConvert(axisText , " npc" , type )
429421 axisTitleSize <- unitConvert(axisTitle , " npc" , type )
@@ -432,22 +424,37 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
432424 bbox(axisTickText , axisText $ angle , axisTextSize )[[type ]] -
433425 bbox(axisTitleText , axisTitle $ angle , axisTitleSize )[[type ]] / 2 -
434426 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+ )
446453 )
447- )
454+ }
448455 }
449456 }
450-
457+ if (has_facet( p )) gglayout [[ axisName ]] $ title <- " "
451458 } # end of axis loop
452459
453460 # theme(panel.border = ) -> plotly rect shape
0 commit comments