@@ -277,6 +277,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
277
277
theme [[" strip.text.x" ]] %|| % theme [[" strip.text" ]],
278
278
" npc" , " height"
279
279
)
280
+ panelMarginY <- panelMarginY + stripSize
280
281
# space for ticks/text in free scales
281
282
if (p $ facet $ free $ x ) {
282
283
axisTicksX <- unitConvert(
@@ -307,7 +308,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
307
308
rep(panelMarginX , 2 ),
308
309
rep(panelMarginY , 2 )
309
310
)
310
-
311
311
doms <- get_domains(nPanels , nRows , margins )
312
312
313
313
for (i in seq_len(nPanels )) {
@@ -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" ,
@@ -350,7 +353,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
350
353
ticklen = unitConvert(theme $ axis.ticks.length , " pixels" , type ),
351
354
tickwidth = unitConvert(axisTicks , " pixels" , type ),
352
355
showticklabels = ! is_blank(axisText ),
353
- tickfont = text2font(axisText , " height " ),
356
+ tickfont = text2font(axisText , type ),
354
357
tickangle = - (axisText $ angle %|| % 0 ),
355
358
showline = ! is_blank(axisLine ),
356
359
linecolor = toRGB(axisLine $ colour ),
@@ -360,7 +363,9 @@ 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
+ title = axisTitleText ,
368
+ titlefont = text2font(axisTitle )
364
369
)
365
370
# convert dates to milliseconds (86400000 = 24 * 60 * 60 * 1000)
366
371
# this way both dates/datetimes are on same scale
@@ -380,27 +385,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
380
385
381
386
# do some stuff that should be done once for the entire plot
382
387
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
388
axisTickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
396
389
side <- if (xy == " x" ) " b" else " l"
397
390
# account for axis ticks, ticks text, and titles in plot margins
398
391
# (apparently ggplot2 doesn't support axis.title/axis.text margins)
399
392
gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
400
393
bbox(axisTickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[type ]] +
401
394
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
+
404
396
if (nchar(axisTitleText ) > 0 ) {
405
397
axisTextSize <- unitConvert(axisText , " npc" , type )
406
398
axisTitleSize <- unitConvert(axisTitle , " npc" , type )
@@ -409,22 +401,41 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
409
401
bbox(axisTickText , axisText $ angle , axisTextSize )[[type ]] -
410
402
bbox(axisTitleText , axisTitle $ angle , axisTitleSize )[[type ]] / 2 -
411
403
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
+ )
423
430
)
424
- )
431
+ }
425
432
}
426
433
}
427
-
434
+
435
+ if (has_facet(p )) {
436
+ gglayout [[axisName ]]$ title <- " "
437
+ }
438
+
428
439
} # end of axis loop
429
440
430
441
xdom <- gglayout [[lay [, " xaxis" ]]]$ domain
0 commit comments