Skip to content

Commit 72cb6b8

Browse files
committed
add meta-info to identify axis titles; simplify facet strip drawing logic
1 parent 0dd4d63 commit 72cb6b8

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

R/ggplotly.R

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
420420
gglayout$annotations,
421421
make_label(
422422
faced(axisTitleText, axisTitle$face), x, y, el = axisTitle,
423-
xanchor = "center", yanchor = "middle"
423+
xanchor = "center", yanchor = "middle", annotationType = "axis"
424424
)
425425
)
426426
}
@@ -432,36 +432,52 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
432432
ydom <- gglayout[[lay[, "yaxis"]]]$domain
433433
border <- make_panel_border(xdom, ydom, theme)
434434
gglayout$shapes <- c(gglayout$shapes, border)
435-
436435
# facet strips -> plotly annotations
436+
if (has_facet(p)) {
437+
col_vars <- ifelse(inherits(p$facet, "wrap"), "facets", "cols")
438+
col_txt <- paste(
439+
p$facet$labeller(lay[names(p$facet[[col_vars]])]), collapse = ", "
440+
)
441+
if (nchar(col_txt) > 0) {
442+
col_lab <- make_label(
443+
col_txt, x = mean(xdom), y = max(ydom),
444+
el = theme[["strip.text.x"]] %||% theme[["strip.text"]],
445+
xanchor = "center", yanchor = "bottom"
446+
)
447+
gglayout$annotations <- c(gglayout$annotations, col_lab)
448+
strip <- make_strip_rect(xdom, ydom, theme, "top")
449+
gglayout$shapes <- c(gglayout$shapes, strip)
450+
}
451+
row_txt <- paste(
452+
p$facet$labeller(lay[names(p$facet$rows)]), collapse = ", "
453+
)
454+
if (nchar(row_txt) > 0) {
455+
row_lab <- make_label(
456+
row_txt, x = max(xdom), y = mean(ydom),
457+
el = theme[["strip.text.y"]] %||% theme[["strip.text"]],
458+
xanchor = "left", yanchor = "middle"
459+
)
460+
gglayout$annotations <- c(gglayout$annotations, row_lab)
461+
strip <- make_strip_rect(xdom, ydom, theme, "right")
462+
gglayout$shapes <- c(gglayout$shapes, strip)
463+
}
464+
465+
466+
467+
}
468+
469+
437470
if (!is_blank(theme[["strip.text.x"]]) &&
438471
(inherits(p$facet, "wrap") || inherits(p$facet, "grid") && lay$ROW == 1)) {
439472
vars <- ifelse(inherits(p$facet, "wrap"), "facets", "cols")
440-
txt <- paste(
441-
p$facet$labeller(lay[names(p$facet[[vars]])]), collapse = ", "
442-
)
443-
lab <- make_label(
444-
txt, x = mean(xdom), y = max(ydom),
445-
el = theme[["strip.text.x"]] %||% theme[["strip.text"]],
446-
xanchor = "center", yanchor = "bottom"
447-
)
448-
gglayout$annotations <- c(gglayout$annotations, lab)
449-
strip <- make_strip_rect(xdom, ydom, theme, "top")
450-
gglayout$shapes <- c(gglayout$shapes, strip)
473+
474+
475+
451476
}
452477
if (inherits(p$facet, "grid") && lay$COL == nCols && nRows > 1 &&
453478
!is_blank(theme[["strip.text.y"]])) {
454-
txt <- paste(
455-
p$facet$labeller(lay[names(p$facet$rows)]), collapse = ", "
456-
)
457-
lab <- make_label(
458-
txt, x = max(xdom), y = mean(ydom),
459-
el = theme[["strip.text.y"]] %||% theme[["strip.text"]],
460-
xanchor = "left", yanchor = "middle"
461-
)
462-
gglayout$annotations <- c(gglayout$annotations, lab)
463-
strip <- make_strip_rect(xdom, ydom, theme, "right")
464-
gglayout$shapes <- c(gglayout$shapes, strip)
479+
480+
465481
}
466482

467483
} # end of panel loop

0 commit comments

Comments
 (0)