Skip to content

Commit 2b09138

Browse files
committed
Account for new changes in ggplot2's internal API, fixes #1561
* Break values of positional scales have moved from to * Text labels of positional scales have moved from to * sf graticule degree labels are now quoted?
1 parent 6f151a7 commit 2b09138

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

R/ggplotly.R

+16-17
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ gg2list <- function(p, width = NULL, height = NULL,
492492
layout$layout$xanchor <- paste0("y", sub("^1$", "", layout$layout$xanchor))
493493
layout$layout$yanchor <- paste0("x", sub("^1$", "", layout$layout$yanchor))
494494
# for some layers2traces computations, we need the range of each panel
495-
layout$layout$x_min <- sapply(layout$panel_params, function(z) min(z$x.range %||% z$x_range))
496-
layout$layout$x_max <- sapply(layout$panel_params, function(z) max(z$x.range %||% z$x_range))
497-
layout$layout$y_min <- sapply(layout$panel_params, function(z) min(z$y.range %||% z$y_range))
498-
layout$layout$y_max <- sapply(layout$panel_params, function(z) max(z$y.range %||% z$y_range))
495+
layout$layout$x_min <- sapply(layout$panel_params, function(z) min(z[["x"]]$dimension %()% z$x.range %||% z$x_range))
496+
layout$layout$x_max <- sapply(layout$panel_params, function(z) max(z[["x"]]$dimension %()% z$x.range %||% z$x_range))
497+
layout$layout$y_min <- sapply(layout$panel_params, function(z) min(z[["y"]]$dimension %()% z$y.range %||% z$y_range))
498+
layout$layout$y_max <- sapply(layout$panel_params, function(z) max(z[["y"]]$dimension %()% z$y.range %||% z$y_range))
499499

500500
# layers -> plotly.js traces
501501
plot$tooltip <- tooltip
@@ -552,7 +552,7 @@ gg2list <- function(p, width = NULL, height = NULL,
552552
)
553553
# allocate enough space for the _longest_ text label
554554
axisTextX <- theme[["axis.text.x"]] %||% theme[["axis.text"]]
555-
labz <- unlist(lapply(layout$panel_params, "[[", "x.labels"))
555+
labz <- unlist(lapply(layout$panel_params, function(pp) pp[["x"]]$get_labels %()% pp$x.labels))
556556
lab <- labz[which.max(nchar(labz))]
557557
panelMarginY <- panelMarginY + axisTicksX +
558558
bbox(lab, axisTextX$angle, unitConvert(axisTextX, "npc", "height"))[["height"]]
@@ -564,7 +564,7 @@ gg2list <- function(p, width = NULL, height = NULL,
564564
)
565565
# allocate enough space for the _longest_ text label
566566
axisTextY <- theme[["axis.text.y"]] %||% theme[["axis.text"]]
567-
labz <- unlist(lapply(layout$panel_params, "[[", "y.labels"))
567+
labz <- unlist(lapply(layout$panel_params, function(pp) pp[["y"]]$get_labels %()% pp$y.labels))
568568
lab <- labz[which.max(nchar(labz))]
569569
panelMarginX <- panelMarginX + axisTicksY +
570570
bbox(lab, axisTextY$angle, unitConvert(axisTextY, "npc", "width"))[["width"]]
@@ -601,7 +601,10 @@ gg2list <- function(p, width = NULL, height = NULL,
601601
idx <- rng$graticule$type == direction & !is.na(rng$graticule$degree_label)
602602
tickData <- rng$graticule[idx, ]
603603
# TODO: how to convert a language object to unicode character string?
604-
rng[[paste0(xy, ".labels")]] <- as.character(tickData[["degree_label"]])
604+
rng[[paste0(xy, ".labels")]] <- sub(
605+
"\\*\\s+degree[ ]?[\\*]?", "&#176;",
606+
sub("\"", "", tickData[["degree_label"]])
607+
)
605608
rng[[paste0(xy, ".major")]] <- tickData[[paste0(xy, "_start")]]
606609

607610
# If it doesn't already exist (for this panel),
@@ -636,14 +639,7 @@ gg2list <- function(p, width = NULL, height = NULL,
636639
tickExists <- with(rng$graticule, sapply(degree_label, is.language))
637640
if (sum(tickExists) == 0) {
638641
theme$axis.ticks.length <- 0
639-
} else{
640-
# convert the special *degree expression in plotmath to HTML entity
641-
# TODO: can this be done more generally for all ?
642-
rng[[paste0(xy, ".labels")]] <- sub(
643-
"\\*\\s+degree[ ]?[\\*]?", "&#176;", rng[[paste0(xy, ".labels")]]
644-
)
645642
}
646-
647643
}
648644

649645
# stuff like layout$panel_params is already flipped, but scales aren't
@@ -673,16 +669,19 @@ gg2list <- function(p, width = NULL, height = NULL,
673669
isDiscrete <- identical(sc$scale_name, "position_d")
674670
isDiscreteType <- isDynamic && isDiscrete
675671

672+
ticktext <- rng[[xy]]$get_labels %()% rng[[paste0(xy, ".labels")]]
673+
tickvals <- rng[[xy]]$break_positions %()% rng[[paste0(xy, ".major")]]
674+
676675
axisObj <- list(
677676
# TODO: log type?
678677
type = if (isDateType) "date" else if (isDiscreteType) "category" else "linear",
679678
autorange = isDynamic,
680679
range = rng[[paste0(xy, ".range")]] %||% rng[[paste0(xy, "_range")]],
681680
tickmode = if (isDynamic) "auto" else "array",
682-
ticktext = rng[[paste0(xy, ".labels")]],
683-
tickvals = rng[[paste0(xy, ".major")]],
681+
ticktext = ticktext,
682+
tickvals = tickvals,
684683
categoryorder = "array",
685-
categoryarray = rng[[paste0(xy, ".labels")]],
684+
categoryarray = ticktext,
686685
nticks = nrow(rng),
687686
ticks = if (is_blank(axisTicks)) "" else "outside",
688687
tickcolor = toRGB(axisTicks$colour),

R/utils.R

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ is.discrete <- function(x) {
3636
if (length(x) > 0 || is_blank(x)) x else y
3737
}
3838

39+
"%()%" <- function(x, y) {
40+
if (is.function(x)) return(x())
41+
y
42+
}
43+
3944
# kind of like %||%, but only respects user-defined defaults
4045
# (instead of defaults provided in the build step)
4146
"%|D|%" <- function(x, y) {

0 commit comments

Comments
 (0)