8000 Better positioning of facet axis annotations by cpsievert · Pull Request #1975 · plotly/plotly.R · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Next Next commit
Better positioning of facet axis annotations
  • Loading branch information
cpsievert committed Jul 13, 2021
commit a1c9b48a302b288d1801be24b48150923190846b
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[c(1, 3)])
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
0