Skip to content

Commit 9352fd4

Browse files
committed
Bug fix. Account for multi_line=FALSE when facet rows/cols are missing.
1 parent 3cf17c0 commit 9352fd4

File tree

2 files changed

+74
-21
lines changed

2 files changed

+74
-21
lines changed

Diff for: R/ggplotly.R

+33-21
Original file line numberDiff line numberDiff line change
@@ -917,11 +917,15 @@ gg2list <- function(p, width = NULL, height = NULL,
917917
# facet strips -> plotly annotations
918918
if (has_facet(plot)) {
919919
col_vars <- ifelse(inherits(plot$facet, "FacetWrap"), "facets", "cols")
920-
col_txt <- paste(
921-
plot$facet$params$labeller(
922-
lay[names(plot$facet$params[[col_vars]])]
923-
), collapse = br()
924-
)
920+
col_txt <- if (!length(names(plot$facet$params[[col_vars]])) == 0) {
921+
paste(
922+
plot$facet$params$labeller(
923+
lay[names(plot$facet$params[[col_vars]])]
924+
), collapse = br()
925+
)
926+
} else {
927+
""
928+
}
925929
if (is_blank(theme[["strip.text.x"]])) col_txt <- ""
926930
if (inherits(plot$facet, "FacetGrid") && lay$ROW != 1) col_txt <- ""
927931
if (robust_nchar(col_txt) > 0) {
@@ -934,22 +938,30 @@ gg2list <- function(p, width = NULL, height = NULL,
934938
strip <- make_strip_rect(xdom, ydom, theme, "top")
935939
gglayout$shapes <- c(gglayout$shapes, strip)
936940
}
937-
row_txt <- paste(
938-
plot$facet$params$labeller(
939-
lay[names(plot$facet$params$rows)]
940-
), collapse = br()
941-
)
942-
if (is_blank(theme[["strip.text.y"]])) row_txt <- ""
943-
if (inherits(plot$facet, "FacetGrid") && lay$COL != nCols) row_txt <- ""
944-
if (robust_nchar(row_txt) > 0) {
945-
row_lab <- make_label(
946-
row_txt, x = max(xdom), y = mean(ydom),
947-
el = theme[["strip.text.y"]] %||% theme[["strip.text"]],
948-
xanchor = "left", yanchor = "middle"
949-
)
950-
gglayout$annotations <- c(gglayout$annotations, row_lab)
951-
strip <- make_strip_rect(xdom, ydom, theme, "right")
952-
gglayout$shapes <- c(gglayout$shapes, strip)
941+
# Only FacetGrid has no cols
942+
if (inherits(plot$facet, "FacetGrid")) {
943+
row_txt <- if (!length(names(plot$facet$params$rows)) == 0) {
944+
paste(
945+
plot$facet$params$labeller(
946+
lay[names(plot$facet$params$rows)]
947+
),
948+
collapse = br()
949+
)
950+
} else {
951+
""
952+
}
953+
if (is_blank(theme[["strip.text.y"]])) row_txt <- ""
954+
if (lay$COL != nCols) row_txt <- ""
955+
if (robust_nchar(row_txt) > 0) {
956+
row_lab <- make_label(
957+
row_txt, x = max(xdom), y = mean(ydom),
958+
el = theme[["strip.text.y"]] %||% theme[["strip.text"]],
959+
xanchor = "left", yanchor = "middle"
960+
)
961+
gglayout$annotations <- c(gglayout$annotations, row_lab)
962+
strip <- make_strip_rect(xdom, ydom, theme, "right")
963+
gglayout$shapes <- c(gglayout$shapes, strip)
964+
}
953965
}
954966
}
955967
} # end of panel loop

Diff for: tests/testthat/test-ggplot-facets.R

+41
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,47 @@ test_that("facet_grid translates simple labeller function", {
131131
)
132132
})
133133

134+
g <- ggplot(mtcars, aes(mpg, wt)) +
135+
geom_point() +
136+
facet_wrap( ~ vs + am, labeller = function(x) label_both(x, multi_line = FALSE))
137+
138+
test_that("facet_wrap accounts for multi_line=FALSE", {
139+
info <- expect_doppelganger_built(g, "facet_wrap-labeller-no-multi-line")
140+
txt <- sapply(info$layout$annotations, "[[", "text")
141+
expect_true(all(!grepl("expression(list())", txt, fixed = TRUE)))
142+
expect_true(
143+
all(c("vs, am: 0, 0", "vs, am: 0, 1", "vs, am: 1, 0", "vs, am: 1, 1") %in% txt)
144+
)
145+
expect_identical(length(txt), 6L)
146+
})
147+
148+
g <- ggplot(mtcars, aes(mpg, wt)) +
149+
geom_point()
150+
151+
g_no_col <- g +
152+
facet_grid(vs + am ~ ., labeller = function(x) label_both(x, multi_line = FALSE))
153+
154+
g_no_row <- g +
155+
facet_grid(. ~ vs + am, labeller = function(x) label_both(x, multi_line = FALSE))
156+
157+
test_that("facet_grid accounts for multi_line=FALSE", {
158+
info <- expect_doppelganger_built(g_no_col, "facet_grid-labeller-no-col")
159+
txt <- sapply(info$layout$annotations, "[[", "text")
160+
expect_true(all(!grepl("expression(list())", txt, fixed = TRUE)))
161+
expect_true(
162+
all(c("vs, am: 0, 0", "vs, am: 0, 1", "vs, am: 1, 0", "vs, am: 1, 1") %in% txt)
163+
)
164+
expect_identical(length(txt), 6L)
165+
166+
info <- expect_doppelganger_built(g_no_row, "facet_grid-labeller-no-col")
167+
txt <- sapply(info$layout$annotations, "[[", "text")
168+
expect_true(all(!grepl("expression(list())", txt, fixed = TRUE)))
169+
expect_true(
170+
all(c("vs, am: 0, 0", "vs, am: 0, 1", "vs, am: 1, 0", "vs, am: 1, 1") %in% txt)
171+
)
172+
expect_identical(length(txt), 6L)
173+
})
174+
134175
p <- economics %>% tidyr::gather(variable, value, -date) %>%
135176
qplot(data = ., date, value) +
136177
facet_wrap(~variable, scale = "free_y", ncol = 2)

0 commit comments

Comments
 (0)