Skip to content

Fixes/workaround for upcoming (> v3.4.3) ggplot2 release #2301

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 12 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
18 changes: 14 additions & 4 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,13 @@ gg2list <- function(p, width = NULL, height = NULL,
call. = FALSE
)
}
# determine axis types (note: scale_name may go away someday)
# determine axis types (note: scale_name went away in ggplot2 v3.4.3)
# https://github.com/hadley/ggplot2/issues/1312
isDate <- isTRUE(sc$scale_name %in% c("date", "datetime"))
isDate <- isTRUE(sc$scale_name %in% c("date", "datetime")) ||
inherits(sc, c("ScaleContinuousDatetime", "ScaleContinuousDate"))
isDateType <- isDynamic && isDate
isDiscrete <- identical(sc$scale_name, "position_d")
isDiscrete <- identical(sc$scale_name, "position_d") ||
inherits(sc, "ScaleDiscretePosition")
isDiscreteType <- isDynamic && isDiscrete

# In 3.2.x .major disappeared in favor of break_positions()
Expand Down Expand Up @@ -1512,7 +1514,15 @@ scales_add_missing <- function(plot, aesthetics) {
# towards ggproto methods attached to `plot$guides`
# -------------------------------------------------------------------------
get_gdefs_ggproto <- function(scales, theme, plot, layers) {
guides <- plot$guides$setup(scales)

# Proposed change to accomodate
# https://github.com/tidyverse/ggplot2/pull/5428
# Ensure a 1:1 mapping between aesthetics and scales
aesthetics <- lapply(scales, `[[`, "aesthetics")
scales <- rep.int(scales, lengths(aesthetics))
aesthetics <- unlist(aesthetics, recursive = FALSE, use.names = FALSE)

guides <- plot$guides$setup(scales, aesthetics = aesthetics)
guides$train(scales, theme$legend.direction, plot$labels)
if (length(guides$guides) > 0) {
guides$merge()
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-ggplot-lines.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ test_that("Milliseconds are preserved with dynamic ticks", {
p <- ggplotly(gg, dynamicTicks = TRUE)
j <- plotly_json(p, jsonedit = FALSE)
t2 <- jsonlite::fromJSON(j)$data$x[[1]] %>%
as.POSIXct(format = "%Y-%m-%d %H:%M:%OS")
as.POSIXct(format = "%Y-%m-%d %H:%M:%OS", origin = "1970-01-01 00:00:00")
expect_equal(as.numeric(mean(diff(t2))), 0.1, tolerance = 0.01)
expect_doppelganger_built(p, "line-milliseconds")
})
Expand Down