@@ -115,6 +115,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
115115 # https://github.com/hadley/ggplot2/blob/0cd0ba/R/plot-build.r#L18-L92
116116 # ------------------------------------------------------------------------
117117
118+ # open a new graphics device, so we can convert relative sizes correctly
119+ # if height/width is not specified, estimate it from the current device
120+ deviceWidth <- width %|| % unitConvert(grid :: unit(1 , " npc" ), " pixels" , " width" )
121+ deviceHeight <- height %|| % unitConvert(grid :: unit(1 , " npc" ), " pixels" , " height" )
122+ tmpPlotFile <- tempfile(fileext = " .png" )
123+ grDevices :: png(tmpPlotFile , width = deviceWidth , height = deviceHeight )
124+
125+
118126 plot <- ggfun(" plot_clone" )(p )
119127 if (length(plot $ layers ) == 0 ) {
120128 plot <- plot + geom_blank()
@@ -480,14 +488,15 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
480488 # npc is on a 0-1 scale of the _entire_ device,
481489 # but these units _should_ be wrt to the plotting region
482490 # multiplying the offset by 2 seems to work, but this is a terrible hack
483- offset <- 1.75 * offset
484491 x <- if (xy == " x" ) 0.5 else offset
485492 y <- if (xy == " x" ) offset else 0.5
486493 gglayout $ annotations <- c(
487494 gglayout $ annotations ,
488495 make_label(
489496 faced(axisTitleText , axisTitle $ face ), x , y , el = axisTitle ,
490- xanchor = " center" , yanchor = " middle" , annotationType = " axis"
497+ xanchor = if (xy == " x" ) " center" else " right" ,
498+ yanchor = if (xy == " x" ) " top" else " center" ,
499+ annotationType = " axis"
491500 )
492501 )
493502 }
@@ -719,6 +728,11 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
719728 gglayout $ width <- width
720729 gglayout $ height <- height
721730
731+ # we're now done with converting units, turn off the device,
732+ # and remove the temporary file
733+ grDevices :: dev.off()
734+ unlink(tmpPlotFile )
735+
722736 l <- list (
723737 data = setNames(traces , NULL ),
724738 layout = compact(gglayout ),
0 commit comments