Skip to content

Better positioning of facet axis annotations #1975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ Suggests:
webshot,
listviewer,
dendextend,
sf,
maptools,
rgeos,
png,
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

* `ggplotly()` now uses the `layout.legend.title` (instead of `layout.annotations`) plotly.js API to convert guides for discrete scales. (#1961)

## Improvements

* `ggplotly()` now better positions axis titles for `facet_wrap()`/`facet_grid()`. (#1975)

# 4.9.4.1

## BUG FIXES
Expand Down
48 changes: 27 additions & 21 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -833,11 +833,12 @@ gg2list <- function(p, width = NULL, height = NULL,
gglayout[[axisName]] <- axisObj

# do some stuff that should be done once for the entire plot
is_x <- xy == "x"
if (i == 1) {
# Split ticktext elements by "\n" to account for linebreaks
axisTickText <- strsplit(as.character(axisObj$ticktext), split = "\n", fixed = TRUE)
axisTickText <- longest_element(unlist(axisTickText))
side <- if (xy == "x") "b" else "l"
side <- if (is_x) "b" else "l"
# account for axis ticks, ticks text, and titles in plot margins
# (apparently ggplot2 doesn't support axis.title/axis.text margins)
gglayout$margin[[side]] <- gglayout$margin[[side]] + axisObj$ticklen +
Expand All @@ -847,40 +848,45 @@ gg2list <- function(p, width = NULL, height = NULL,
if (robust_nchar(axisTitleText) > 0) {
axisTextSize <- unitConvert(axisText, "npc", type)
axisTitleSize <- unitConvert(axisTitle, "npc", type)
offset <-
(0 -
bbox(axisTickText, axisText$angle, axisTextSize)[[type]] -
bbox(axisTitleText, axisTitle$angle, axisTitleSize)[[type]] / 2 -
unitConvert(theme$axis.ticks.length, "npc", type))
}

# add space for exterior facet strips in `layout.margin`

if (has_facet(plot)) {
stripSize <- unitConvert(stripText, "pixels", type)
if (xy == "x") {
if (is_x) {
gglayout$margin$t <- gglayout$margin$t + stripSize
}
if (xy == "y" && inherits(plot$facet, "FacetGrid")) {
if (is_x && inherits(plot$facet, "FacetGrid")) {
gglayout$margin$r <- gglayout$margin$r + stripSize
}
# facets have multiple axis objects, but only one title for the plot,
# so we empty the titles and try to draw the title as an annotation
if (robust_nchar(axisTitleText) > 0) {
# npc is on a 0-1 scale of the _entire_ device,
# but these units _should_ be wrt to the plotting region
# multiplying the offset by 2 seems to work, but this is a terrible hack
x <- if (xy == "x") 0.5 else offset
y <- if (xy == "x") offset else 0.5
gglayout$annotations <- c(
gglayout$annotations,
make_label(
faced(axisTitleText, axisTitle$face), x, y, el = axisTitle,
xanchor = if (xy == "x") "center" else "right",
yanchor = if (xy == "x") "top" else "center",
annotationType = "axis"
)
axisAnn <- make_label(
faced(axisTitleText, axisTitle$face),
el = axisTitle,
x = if (is_x) 0.5 else 0,
y = if (is_x) 0 else 0.5,
xanchor = if (is_x) "center" else "right",
yanchor = if (is_x) "top" else "center",
annotationType = "axis"
)

textMargin <- sum(axisText$margin[if (is_x) c(1, 3) else c(2, 4)])
class(textMargin) <- setdiff(class(textMargin), "margin")
titleMargin <- axisTitle$margin[if (is_x) 1 else 2]
class(titleMargin) <- setdiff(class(titleMargin), "margin")
offset <- bbox(axisTickText, axisText$angle, axisTextSize)[[type]] +
unitConvert(theme$axis.ticks.length, "npc", type) +
unitConvert(textMargin, "npc", type) +
unitConvert(titleMargin, "npc", type)

offset <- unitConvert(grid::unit(offset, "npc"), "pixels", type)

shift <- if (is_x) "yshift" else "xshift"
axisAnn[[1]][[shift]] <- -1 * offset
gglayout$annotations <- c(gglayout$annotations, axisAnn)
}
}
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/3-panels.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/barley.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/facet-grid-free-y.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/facet-grid-free.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/facet-wrap-free-x.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/facet-wrap-free-y.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/testthat/_snaps/ggplot-facets/facet-wrap-free.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.