@@ -277,6 +277,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
277277 theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
278278 " npc" , " height"
279279 )
280+ panelMarginY <- panelMarginY + stripSize
280281 # space for ticks/text in free scales
281282 if (p $ facet $ free $ x ) {
282283 axisTicksX <- unitConvert(
@@ -307,7 +308,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
307308 rep(panelMarginX , 2 ),
308309 rep(panelMarginY , 2 )
309310 )
310-
311311 doms <- get_domains(nPanels , nRows , margins )
312312
313313 for (i in seq_len(nPanels )) {
@@ -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" ,
@@ -350,7 +353,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
350353 ticklen = unitConvert(theme $ axis.ticks.length , " pixels" , type ),
351354 tickwidth = unitConvert(axisTicks , " pixels" , type ),
352355 showticklabels = ! is_blank(axisText ),
353- tickfont = text2font(axisText , " height " ),
356+ tickfont = text2font(axisText , type ),
354357 tickangle = - (axisText $ angle %|| % 0 ),
355358 showline = ! is_blank(axisLine ),
356359 linecolor = toRGB(axisLine $ colour ),
@@ -360,7 +363,9 @@ 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+ title = axisTitleText ,
368+ titlefont = text2font(axisTitle )
364369 )
365370 # convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
366371 # this way both dates/datetimes are on same scale
@@ -380,27 +385,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
380385
381386 # do some stuff that should be done once for the entire plot
382387 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 <- " "
395388 axisTickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
396389 side <- if (xy == " x" ) " b" else " l"
397390 # account for axis ticks, ticks text, and titles in plot margins
398391 # (apparently ggplot2 doesn't support axis.title/axis.text margins)
399392 gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
400393 bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
401394 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)
395+
404396 if (nchar(axisTitleText ) > 0 ) {
405397 axisTextSize <- unitConvert(axisText , " npc" , type )
406398 axisTitleSize <- unitConvert(axisTitle , " npc" , type )
@@ -409,22 +401,41 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
409401 bbox(axisTickText , axisText $ angle , axisTextSize )[[type ]] -
410402 bbox(axisTitleText , axisTitle $ angle , axisTitleSize )[[type ]] / 2 -
411403 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"
404+ }
405+
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+ # facets have multiple axis objects, but only one title for the plot,
416+ # so we empty the titles and try to draw the title as an annotation
417+ if (nchar(axisTitleText ) > 0 ) {
418+ # npc is on a 0-1 scale of the _entire_ device,
419+ # but these units _should_ be wrt to the plotting region
420+ # multiplying the offset by 2 seems to work, but this is a terrible hack
421+ offset <- 1.75 * offset
422+ x <- if (xy == " x" ) 0.5 else offset
423+ y <- if (xy == " x" ) offset else 0.5
424+ gglayout $ annotations <- c(
425+ gglayout $ annotations ,
426+ make_label(
427+ faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
428+ xanchor = " center" , yanchor = " middle"
429+ )
423430 )
424- )
431+ }
425432 }
426433 }
427-
434+
435+ if (has_facet(p )) {
436+ gglayout [[axisName ]]$ title <- " "
437+ }
438+
428439 } # end of axis loop
429440
430441 xdom <- gglayout [[lay [, " xaxis" ]]]$ domain
0 commit comments