Skip to content

Accommodate breaking changes in ggplot2 3.4.0 #2200

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 17 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,5 @@ LazyData: true
RoxygenNote: 7.2.1
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Remotes:
tidyverse/ggplot2
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ S3method(highlight_key,plotly)
S3method(layout,matrix)
S3method(layout,plotly)
S3method(layout,shiny.tag.list)
S3method(linewidth_or_size,Geom)
S3method(linewidth_or_size,element)
S3method(plotly_build,"NULL")
S3method(plotly_build,gg)
S3method(plotly_build,list)
Expand Down
31 changes: 24 additions & 7 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ gg2list <- function(p, width = NULL, height = NULL,
d[["y_plotlyDomain"]] <- d[["y"]]
d
})
# And since we're essentially adding an "unknown" (to ggplot2)
# aesthetic, add it to the dropped_aes field to avoid fals positive
# warnings (https://github.com/tidyverse/ggplot2/pull/4866)
layers <- lapply(layers, function(l) {
l$stat$dropped_aes <- c(l$stat$dropped_aes, "x_plotlyDomain")
l$stat$dropped_aes <- c(l$stat$dropped_aes, "y_plotlyDomain")
l
})

# Transform all scales
data <- lapply(data, ggfun("scales_transform_df"), scales = scales)
Expand Down Expand Up @@ -676,9 +684,10 @@ gg2list <- function(p, width = NULL, height = NULL,
d$y <- scales::rescale(d$y, rng$y_range, from = c(0, 1))
params <- list(
colour = panelGrid$colour,
size = panelGrid$size,
linetype = panelGrid$linetype
)
nm <- linewidth_or_size(panelGrid)
params[[nm]] <- panelGrid[[nm]]
grill <- geom2trace.GeomPath(d, params)
grill$hoverinfo <- "none"
grill$showlegend <- FALSE
Expand Down Expand Up @@ -723,8 +732,12 @@ gg2list <- function(p, width = NULL, height = NULL,
isDiscrete <- identical(sc$scale_name, "position_d")
isDiscreteType <- isDynamic && isDiscrete

ticktext <- rng[[xy]]$get_labels %()% rng[[paste0(xy, ".labels")]]
tickvals <- rng[[xy]]$break_positions %()% rng[[paste0(xy, ".major")]]
# In 3.2.x .major disappeared in favor of break_positions()
# (tidyverse/ggplot2#3436), but with 3.4.x break_positions() no longer
# yields the actual final positions on a 0-1 scale, but .major does
# (tidyverse/ggplot2#5029)
ticktext <- rng[[paste0(xy, ".labels")]] %||% rng[[xy]]$get_labels()
tickvals <- rng[[paste0(xy, ".major")]] %||% rng[[xy]]$break_positions()

# https://github.com/tidyverse/ggplot2/pull/3566#issuecomment-565085809
hasTickText <- !(is.na(ticktext) | is.na(tickvals))
Expand All @@ -735,7 +748,7 @@ gg2list <- function(p, width = NULL, height = NULL,
# TODO: log type?
type = if (isDateType) "date" else if (isDiscreteType) "category" else "linear",
autorange = isDynamic,
range = rng[[paste0(xy, ".range")]] %||% rng[[paste0(xy, "_range")]],
range = rng[[xy]]$dimension %()% rng[[paste0(xy, ".range")]] %||% rng[[paste0(xy, "_range")]],
tickmode = if (isDynamic) "auto" else "array",
ticktext = ticktext,
tickvals = tickvals,
Expand Down Expand Up @@ -958,7 +971,10 @@ gg2list <- function(p, width = NULL, height = NULL,
gglayout$legend <- list(
bgcolor = toRGB(theme$legend.background$fill),
bordercolor = toRGB(theme$legend.background$colour),
borderwidth = unitConvert(theme$legend.background$size, "pixels", "width"),
borderwidth = unitConvert(
theme$legend.background[[linewidth_or_size(theme$legend.background)]],
"pixels", "width"
),
font = text2font(theme$legend.text)
)

Expand Down Expand Up @@ -1191,7 +1207,7 @@ verifyUnit <- function(u) {

## the default unit in ggplot2 is millimeters (unless it's element_text())
if (inherits(u, "element")) {
grid::unit(u$size %||% 0, "points")
grid::unit(u[[linewidth_or_size(u)]] %||% 0, "points")
} else {
grid::unit(u %||% 0, "mm")
}
Expand Down Expand Up @@ -1411,7 +1427,8 @@ gdef2trace <- function(gdef, theme, gglayout) {
bgcolor = toRGB(theme$legend.background$fill),
bordercolor = toRGB(theme$legend.background$colour),
borderwidth = unitConvert(
theme$legend.background$size, "pixels", "width"
theme$legend.background[[linewidth_or_size(theme$legend.background)]],
"pixels", "width"
),
thickness = unitConvert(
theme$legend.key.width, "pixels", "width"
Expand Down
59 changes: 42 additions & 17 deletions R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,11 @@ to_basic.GeomHex <- function(data, prestats_data, layout, params, p, ...) {
dy <- resolution(data[["y"]], FALSE)/sqrt(3)/2 * 1.15
hexC <- hexbin::hexcoords(dx, dy, n = 1)
n <- nrow(data)
data$size <- ifelse(data$size < 1, data$size ^ (1 / 6), data$size ^ 6)
x <- rep.int(hexC[["x"]], n) * rep(data$size, each = 6) + rep(data[["x"]], each = 6)
y <- rep.int(hexC[["y"]], n) * rep(data$size, each = 6) + rep(data[["y"]], each = 6)
nm <- linewidth_or_size(GeomHex)
size <- data[[nm]]
data[[nm]] <- ifelse(size < 1, size ^ (1 / 6), size ^ 6)
x <- rep.int(hexC[["x"]], n) * rep(data[[nm]], each = 6) + rep(data[["x"]], each = 6)
y <- rep.int(hexC[["y"]], n) * rep(data[[nm]], each = 6) + rep(data[["y"]], each = 6)
data <- data[rep(seq_len(n), each = 6), ]
data[["x"]] <- x
data[["y"]] <- y
Expand Down Expand Up @@ -558,13 +560,15 @@ to_basic.GeomSpoke <- function(data, prestats_data, layout, params, p, ...) {
#' @export
to_basic.GeomCrossbar <- function(data, prestats_data, layout, params, p, ...) {
# from GeomCrossbar$draw_panel()
middle <- base::transform(data, x = xmin, xend = xmax, yend = y, size = size * params$fatten, alpha = NA)
middle <- base::transform(data, x = xmin, xend = xmax, yend = y, alpha = NA)
nm <- linewidth_or_size(GeomCrossbar)
data[[nm]] <- data[[nm]] * params$fatten
list(
prefix_class(to_basic.GeomRect(data), "GeomCrossbar"),
prefix_class(to_basic.GeomSegment(middle), "GeomCrossbar")
)
}
utils::globalVariables(c("xmin", "xmax", "y", "size", "COL", "PANEL", "ROW", "yaxis"))
utils::globalVariables(c("xmin", "xmax", "y", "size", "linewidth", "COL", "PANEL", "ROW", "yaxis"))

#' @export
to_basic.GeomRug <- function(data, prestats_data, layout, params, p, ...) {
Expand Down Expand Up @@ -710,7 +714,7 @@ geom2trace.GeomPath <- function(data, params, p) {
name = if (inherits(data, "GeomSmooth")) "fitted values",
line = list(
# TODO: line width array? -- https://github.com/plotly/plotly.js/issues/147
width = aes2plotly(data, params, "size")[1],
width = aes2plotly(data, params, linewidth_or_size(GeomPath))[1],
color = toRGB(
aes2plotly(data, params, "colour"),
aes2plotly(data, params, "alpha")
Expand Down Expand Up @@ -803,7 +807,7 @@ geom2trace.GeomBar <- function(data, params, p) {
aes2plotly(data, params, "alpha")
),
line = list(
width = aes2plotly(data, params, "size"),
width = aes2plotly(data, params, linewidth_or_size(GeomBar)),
color = aes2plotly(data, params, "colour")
)
)
Expand All @@ -812,7 +816,7 @@ geom2trace.GeomBar <- function(data, params, p) {

#' @export
geom2trace.GeomPolygon <- function(data, params, p) {

data <- group2NA(data)

L <- list(
Expand All @@ -826,7 +830,7 @@ geom2trace.GeomPolygon <- function(data, params, p) {
type = "scatter",
mode = "lines",
line = list(
width = aes2plotly(data, params, "size"),
width = aes2plotly(data, params, linewidth_or_size(GeomPolygon)),
color = toRGB(
aes2plotly(data, params, "colour"),
aes2plotly(data, params, "alpha")
Expand Down Expand Up @@ -873,7 +877,7 @@ geom2trace.GeomBoxplot <- function(data, params, p) {
),
line = list(
color = aes2plotly(data, params, "colour"),
width = aes2plotly(data, params, "size")
width = aes2plotly(data, params, linewidth_or_size(GeomBoxplot))
)
))
}
Expand Down Expand Up @@ -976,11 +980,11 @@ geom2trace.default <- function(data, params, p) {
# since plotly.js can't draw two polygons with different fill in a single trace
split_on <- function(dat) {
lookup <- list(
GeomHline = c("linetype", "colour", "size"),
GeomVline = c("linetype", "colour", "size"),
GeomAbline = c("linetype", "colour", "size"),
GeomPath = c("fill", "colour", "size"),
GeomPolygon = c("fill", "colour", "size"),
GeomHline = c("linetype", "colour", "size", "linewidth"),
GeomVline = c("linetype", "colour", "size", "linewidth"),
GeomAbline = c("linetype", "colour", "size", "linewidth"),
GeomPath = c("fill", "colour", "size", "linewidth"),
GeomPolygon = c("fill", "colour", "size", "linewidth"),
GeomBar = "fill",
GeomBoxplot = c("colour", "fill", "size"),
GeomErrorbar = "colour",
Expand Down Expand Up @@ -1079,7 +1083,7 @@ aes2plotly <- function(data, params, aes = "size") {
# Hack to support this geom_sf hack
# https://github.com/tidyverse/ggplot2/blob/505e4bfb/R/sf.R#L179-L187
defaults <- if (inherits(data, "GeomSf")) {
type <- if (any(grepl("point", class(data)))) "point" else if (any(grepl("line", class(data)))) "line" else ""
type <- if (any(grepl("[P-p]oint", class(data)))) "point" else if (any(grepl("[L-l]ine", class(data)))) "line" else ""
ggfun("default_aesthetics")(type)
} else {
geom_obj <- ggfun(geom)
Expand All @@ -1093,7 +1097,8 @@ aes2plotly <- function(data, params, aes = "size") {
vals <- uniq(data[[aes]]) %||% params[[aes]] %||% defaults[[aes]] %||% NA
converter <- switch(
aes,
size = mm2pixels,
size = mm2pixels,
linewidth = mm2pixels,
stroke = mm2pixels,
colour = toRGB,
fill = toRGB,
Expand All @@ -1112,6 +1117,26 @@ aes2plotly <- function(data, params, aes = "size") {
converter(vals)
}


# ggplot2 3.4.0 deprecated size in favor of linewidth in line-based geoms (e.g.,
# GeomLine, GeomRect, etc) and elements (e.g., element_line(), element_rect(),
# etc). Note that, some geoms (e.g., GeomBoxplot, GeomSf) can have both
# linewidth and size
linewidth_or_size <- function(x) {
UseMethod("linewidth_or_size")
}

#' @export
linewidth_or_size.Geom <- function(x) {
if ("linewidth" %in% x$aesthetics()) "linewidth" else "size"
}

#' @export
linewidth_or_size.element <- function(x) {
if ("linewidth" %in% names(x)) "linewidth" else "size"
}


# Convert R pch point codes to plotly "symbol" codes.
pch2symbol <- function(x) {
lookup <- list(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading