@@ -208,7 +208,6 @@ gg2list <- function(p, width = NULL, height = NULL) {
208208 theme [[" panel.margin.y" ]] %|| % theme [[" panel.margin" ]],
209209 " npc" , " height"
210210 )
211-
212211 # space for _interior_ facet strips
213212 if (inherits(p $ facet , " wrap" )) {
214213 stripSize <- unitConvert(
@@ -219,26 +218,26 @@ gg2list <- function(p, width = NULL, height = NULL) {
219218 panelMarginY <- panelMarginY + 1.5 * stripSize
220219 # space for ticks/text in free scales
221220 if (p $ facet $ free $ x ) {
222- axisTextX <- unitConvert(
223- theme [[" axis.text.x" ]] %|| % theme [[" axis.text" ]],
224- " npc" , " height"
225- )
226221 axisTicksX <- unitConvert(
227222 theme [[" axis.ticks.x" ]] %|| % theme [[" axis.ticks" ]],
228223 " npc" , " height"
229224 )
230- panelMarginY <- panelMarginY + axisTextX + axisTicksX
225+ axisTextX <- theme [[" axis.text.x" ]] %|| % theme [[" axis.text" ]]
226+ labz <- unlist(lapply(panel $ ranges , " [[" , " x.labels" ))
227+ lab <- labz [which.max(nchar(labz ))]
228+ panelMarginY <- panelMarginY + axisTicksX +
229+ bbox(lab , axisTextX $ angle , unitConvert(axisTextX , " npc" , " height" ))$ v
231230 }
232231 if (p $ facet $ free $ y ) {
233- axisTextY <- unitConvert(
234- theme [[" axis.text.y" ]] %|| % theme [[" axis.text" ]],
235- " npc" , " width"
236- )
237232 axisTicksY <- unitConvert(
238233 theme [[" axis.ticks.y" ]] %|| % theme [[" axis.ticks" ]],
239234 " npc" , " width"
240235 )
241- panelMarginX <- panelMarginX + axisTextY + axisTicksY
236+ axisTextY <- theme [[" axis.text.y" ]] %|| % theme [[" axis.text" ]]
237+ labz <- unlist(lapply(panel $ ranges , " [[" , " y.labels" ))
238+ lab <- labz [which.max(nchar(labz ))]
239+ panelMarginX <- panelMarginX + axisTicksY +
240+ bbox(lab , axisTextY $ angle , unitConvert(axisTextY , " npc" , " width" ))$ h
242241 }
243242 }
244243 margins <- c(
@@ -317,12 +316,12 @@ gg2list <- function(p, width = NULL, height = NULL) {
317316 # account for (exterior) axis/strip text in plot margins
318317 side <- if (xy == " x" ) " b" else " l"
319318 way <- if (xy == " x" ) " v" else " h"
319+ tickText <- axisObj $ ticktext [which.max(nchar(axisObj $ ticktext ))]
320320 # apparently ggplot2 doesn't support axis.title/axis.text margins
321- gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] +
321+ gglayout $ margin [[side ]] <- gglayout $ margin [[side ]] + axisObj $ ticklen +
322+ # account for rotated title (just like we've done for ticks?)
322323 axisObj $ titlefont $ size +
323- axisObj $ tickfont $ size +
324- axisObj $ ticklen +
325- with(axisObj , bbox(ticktext , tickangle , tickfont $ size ))[[way ]]
324+ bbox(tickText , axisObj $ tickangle , axisObj $ tickfont $ size )[[way ]]
326325
327326 if (has_facet(p )) {
328327 # draw axis titles as annotations
@@ -615,7 +614,7 @@ strip_axis <- function(x, y = c("title", "titlefont")) {
615614 x
616615}
617616
618- # ' Conservative estimate of height/width of a string
617+ # ' Estimate bounding box of a rotated string
619618# '
620619# ' @param txt a character string of length 1
621620# ' @param angle sets the angle of the tick labels with respect to the
@@ -627,6 +626,9 @@ strip_axis <- function(x, y = c("title", "titlefont")) {
627626bbox <- function (txt = " foo" , angle = 0 , size = 12 ) {
628627 # assuming the horizontal size of a character is roughly half of the vertical
629628 w <- size * (nchar(txt ) / 2 )
629+ # do the sensible thing in the majority of cases
630+ if (angle == 0 ) return (list (v = size , h = w ))
631+ if (abs(angle ) == 90 ) return (list (v = w , h = size ))
630632 # first, compute the hypotenus
631633 hyp <- sqrt(size ^ 2 + w ^ 2 )
632634 list (
0 commit comments