Skip to content
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

Bug fix. Account for multi_line=FALSE when facet rows/cols are missing. Closes #2341. #2373

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
54 changes: 33 additions & 21 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,15 @@ gg2list <- function(p, width = NULL, height = NULL,
# facet strips -> plotly annotations
if (has_facet(plot)) {
col_vars <- ifelse(inherits(plot$facet, "FacetWrap"), "facets", "cols")
col_txt <- paste(
plot$facet$params$labeller(
lay[names(plot$facet$params[[col_vars]])]
), collapse = br()
)
col_txt <- if (!length(names(plot$facet$params[[col_vars]])) == 0) {
paste(
plot$facet$params$labeller(
lay[names(plot$facet$params[[col_vars]])]
), collapse = br()
)
} else {
""
}
trekonom marked this conversation as resolved.
Show resolved Hide resolved
if (is_blank(theme[["strip.text.x"]])) col_txt <- ""
if (inherits(plot$facet, "FacetGrid") && lay$ROW != 1) col_txt <- ""
if (robust_nchar(col_txt) > 0) {
Expand All @@ -934,22 +938,30 @@ gg2list <- function(p, width = NULL, height = NULL,
strip <- make_strip_rect(xdom, ydom, theme, "top")
gglayout$shapes <- c(gglayout$shapes, strip)
}
row_txt <- paste(
plot$facet$params$labeller(
lay[names(plot$facet$params$rows)]
), collapse = br()
)
if (is_blank(theme[["strip.text.y"]])) row_txt <- ""
if (inherits(plot$facet, "FacetGrid") && lay$COL != nCols) row_txt <- ""
if (robust_nchar(row_txt) > 0) {
row_lab <- make_label(
row_txt, x = max(xdom), y = mean(ydom),
el = theme[["strip.text.y"]] %||% theme[["strip.text"]],
xanchor = "left", yanchor = "middle"
)
gglayout$annotations <- c(gglayout$annotations, row_lab)
strip <- make_strip_rect(xdom, ydom, theme, "right")
gglayout$shapes <- c(gglayout$shapes, strip)
# Only FacetGrid has no cols
if (inherits(plot$facet, "FacetGrid")) {
row_txt <- if (!length(names(plot$facet$params$rows)) == 0) {
paste(
plot$facet$params$labeller(
lay[names(plot$facet$params$rows)]
),
collapse = br()
)
} else {
""
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx for the review and the suggestions. Just committed both suggestions.

if (is_blank(theme[["strip.text.y"]])) row_txt <- ""
if (lay$COL != nCols) row_txt <- ""
if (robust_nchar(row_txt) > 0) {
row_lab <- make_label(
row_txt, x = max(xdom), y = mean(ydom),
el = theme[["strip.text.y"]] %||% theme[["strip.text"]],
xanchor = "left", yanchor = "middle"
)
gglayout$annotations <- c(gglayout$annotations, row_lab)
strip <- make_strip_rect(xdom, ydom, theme, "right")
gglayout$shapes <- c(gglayout$shapes, strip)
}
}
}
} # end of panel loop
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-ggplot-facets.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,47 @@ test_that("facet_grid translates simple labeller function", {
)
})

g <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() +
facet_wrap( ~ vs + am, labeller = function(x) label_both(x, multi_line = FALSE))

test_that("facet_wrap accounts for multi_line=FALSE", {
info <- expect_doppelganger_built(g, "facet_wrap-labeller-no-multi-line")
txt <- sapply(info$layout$annotations, "[[", "text")
expect_true(all(!grepl("expression(list())", txt, fixed = TRUE)))
expect_true(
all(c("vs, am: 0, 0", "vs, am: 0, 1", "vs, am: 1, 0", "vs, am: 1, 1") %in% txt)
)
expect_identical(length(txt), 6L)
})

g <- ggplot(mtcars, aes(mpg, wt)) +
geom_point()

g_no_col <- g +
facet_grid(vs + am ~ ., labeller = function(x) label_both(x, multi_line = FALSE))

g_no_row <- g +
facet_grid(. ~ vs + am, labeller = function(x) label_both(x, multi_line = FALSE))

test_that("facet_grid accounts for multi_line=FALSE", {
info <- expect_doppelganger_built(g_no_col, "facet_grid-labeller-no-col")
txt <- sapply(info$layout$annotations, "[[", "text")
expect_true(all(!grepl("expression(list())", txt, fixed = TRUE)))
expect_true(
all(c("vs, am: 0, 0", "vs, am: 0, 1", "vs, am: 1, 0", "vs, am: 1, 1") %in% txt)
)
expect_identical(length(txt), 6L)

info <- expect_doppelganger_built(g_no_row, "facet_grid-labeller-no-col")
txt <- sapply(info$layout$annotations, "[[", "text")
expect_true(all(!grepl("expression(list())", txt, fixed = TRUE)))
expect_true(
all(c("vs, am: 0, 0", "vs, am: 0, 1", "vs, am: 1, 0", "vs, am: 1, 1") %in% txt)
)
expect_identical(length(txt), 6L)
})

p <- economics %>% tidyr::gather(variable, value, -date) %>%
qplot(data = ., date, value) +
facet_wrap(~variable, scale = "free_y", ncol = 2)
Expand Down
Loading