Skip to content

Commit f795761

Browse files
committed
ggplotly: fix margin calculation
- add space for the title and axis only to one of the L/R or T/B margins - add panel.space/2 to each margin side, not panel.space
1 parent d9e213e commit f795761

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

R/ggplotly.R

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -535,21 +535,24 @@ gg2list <- function(p, width = NULL, height = NULL,
535535

536536
# panel margins must be computed before panel/axis loops
537537
# (in order to use get_domains())
538-
panelMarginX <- unitConvert(
538+
panelMarginL <- 0.5*unitConvert(
539539
theme[["panel.spacing.x"]] %||% theme[["panel.spacing"]],
540540
"npc", "width"
541541
)
542-
panelMarginY <- unitConvert(
542+
panelMarginR <- panelMarginL
543+
panelMarginT <- 0.5*unitConvert(
543544
theme[["panel.spacing.y"]] %||% theme[["panel.spacing"]],
544545
"npc", "height"
545546
)
547+
panelMarginB <- panelMarginT
546548
# space for _interior_ facet strips
547549
if (inherits(plot$facet, "FacetWrap")) {
548550
stripSize <- unitConvert(
549551
theme[["strip.text.x"]] %||% theme[["strip.text"]],
550552
"npc", "height"
551553
)
552-
panelMarginY <- panelMarginY + stripSize
554+
# FIXME add to MarginB if strip position is below?
555+
panelMarginT <- panelMarginT + stripSize
553556
# space for ticks/text in free scales
554557
if (plot$facet$params$free$x) {
555558
axisTicksX <- unitConvert(
@@ -560,7 +563,8 @@ gg2list <- function(p, width = NULL, height = NULL,
560563
axisTextX <- theme[["axis.text.x"]] %||% theme[["axis.text"]]
561564
labz <- unlist(lapply(layout$panel_params, "[[", "x.labels"))
562565
lab <- labz[which.max(nchar(labz))]
563-
panelMarginY <- panelMarginY + axisTicksX +
566+
# FIXME add to MarginT if axis position is above?
567+
panelMarginB <- panelMarginB + axisTicksX +
564568
bbox(lab, axisTextX$angle, unitConvert(axisTextX, "npc", "height"))[["height"]]
565569
}
566570
if (plot$facet$params$free$y) {
@@ -572,14 +576,12 @@ gg2list <- function(p, width = NULL, height = NULL,
572576
axisTextY <- theme[["axis.text.y"]] %||% theme[["axis.text"]]
573577
labz <- unlist(lapply(layout$panel_params, "[[", "y.labels"))
574578
lab <- labz[which.max(nchar(labz))]
575-
panelMarginX <- panelMarginX + axisTicksY +
579+
# FIXME add to MarginR if axis position is on the right?
580+
panelMarginL <- panelMarginL + axisTicksY +
576581
bbox(lab, axisTextY$angle, unitConvert(axisTextY, "npc", "width"))[["width"]]
577582
}
578583
}
579-
margins <- c(
580-
rep(panelMarginX, 2),
581-
rep(panelMarginY, 2)
582-
)
584+
margins <- c(panelMarginL, panelMarginR, panelMarginT, panelMarginB)
583585
doms <- get_domains(nPanels, nRows, margins)
584586

585587
for (i in seq_len(nPanels)) {

0 commit comments

Comments
 (0)