Skip to content

Commit

Permalink
Fixes/workaround for upcoming (> v3.4.3) ggplot2 release (#2301)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert authored Oct 11, 2023
1 parent 270d20e commit 22d715d
Show file tree
Hide file tree
Showing 13 changed files with 325 additions and 298 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 4.10.2.9000

## Improvements

* `ggplotly()` now works better with the development version of ggplot2 (> v3.4.3). (#2301)

## Bug fixes

* Closed #1947: `ggplotly()` now correctly handles `geom_vline`/`geom_hline` with empty data. Previously, if `geom_vline`/`geom_hline` was passed an empty data frame, it would result in an error. The plot is drawn even if no lines are found; this is the same behavior as `ggplot2`.
Expand Down
17 changes: 12 additions & 5 deletions R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -725,11 +725,11 @@ gg2list <- function(p, width = NULL, height = NULL,
call. = FALSE
)
}
# determine axis types (note: scale_name may go away someday)
# https://github.com/hadley/ggplot2/issues/1312
isDate <- isTRUE(sc$scale_name %in% c("date", "datetime"))

# determine axis types
isDate <- inherits(sc, c("ScaleContinuousDatetime", "ScaleContinuousDate"))
isDateType <- isDynamic && isDate
isDiscrete <- identical(sc$scale_name, "position_d")
isDiscrete <- inherits(sc, "ScaleDiscretePosition")
isDiscreteType <- isDynamic && isDiscrete

# In 3.2.x .major disappeared in favor of break_positions()
Expand Down Expand Up @@ -1512,7 +1512,14 @@ 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)

# Unfortunate duplication of logic in tidyverse/ggplot2#5428
# which ensures 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
3 changes: 2 additions & 1 deletion R/layers2traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ layers2traces <- function(data, prestats_data, layout, p) {
discreteScales <- list()
for (sc in p$scales$non_position_scales()$scales) {
if (sc$is_discrete()) {
discreteScales[[sc$aesthetics]] <- sc
nm <- paste(sc$aesthetics, collapse = "_")
discreteScales[[nm]] <- sc
}
}
# Convert "high-level" geoms to their "low-level" counterpart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,70 +14,70 @@
"x": [
21,
21,
22.8,
21.4,
18.7,
18.1,
14.3,
24.4,
22.8,
19.2,
17.8,
16.4,
17.3,
15.2,
22.800000000000001,
21.399999999999999,
18.699999999999999,
18.100000000000001,
14.300000000000001,
24.399999999999999,
22.800000000000001,
19.199999999999999,
17.800000000000001,
16.399999999999999,
17.300000000000001,
15.199999999999999,
10.4,
10.4,
14.7,
32.4,
30.4,
33.9,
14.699999999999999,
32.399999999999999,
30.399999999999999,
33.899999999999999,
21.5,
15.5,
15.2,
13.3,
19.2,
27.3,
15.199999999999999,
13.300000000000001,
19.199999999999999,
27.300000000000001,
26,
30.4,
15.8,
19.7,
30.399999999999999,
15.800000000000001,
19.699999999999999,
15,
21.4
21.399999999999999
],
"y": [
2.62,
2.6200000000000001,
2.875,
2.32,
3.215,
3.44,
2.3199999999999998,
3.2149999999999999,
3.4399999999999999,
3.46,
3.57,
3.19,
3.15,
3.44,
3.44,
4.07,
3.5699999999999998,
3.1899999999999999,
3.1499999999999999,
3.4399999999999999,
3.4399999999999999,
4.0700000000000003,
3.73,
3.78,
3.7799999999999998,
5.25,
5.424,
5.345,
2.2,
5.4240000000000004,
5.3449999999999998,
2.2000000000000002,
1.615,
1.835,
2.465,
2.4649999999999999,
3.52,
3.435,
3.84,
3.845,
1.935,
2.14,
1.513,
3.17,
3.4350000000000001,
3.8399999999999999,
3.8450000000000002,
1.9350000000000001,
2.1400000000000001,
1.5129999999999999,
3.1699999999999999,
2.77,
3.57,
2.78
3.5699999999999998,
2.7799999999999998
],
"text": [
"mpg: 21.0<br />wt: 2.620<br />nms: Mazda RX4",
Expand Down Expand Up @@ -153,10 +153,10 @@
"autocolorscale": false,
"color": "rgba(0,0,0,1)",
"opacity": 1,
"size": 5.66929133858268,
"size": 5.6692913385826778,
"symbol": "circle",
"line": {
"width": 1.88976377952756,
"width": 1.8897637795275593,
"color": "rgba(0,0,0,1)"
}
},
Expand All @@ -170,17 +170,17 @@
],
"layout": {
"margin": {
"t": 25.7412480974125,
"r": 7.30593607305936,
"b": 39.6955859969559,
"l": 31.4155251141553
"t": 25.74124809741248,
"r": 7.3059360730593621,
"b": 39.69558599695587,
"l": 31.415525114155255
},
"plot_bgcolor": "rgba(235,235,235,1)",
"paper_bgcolor": "rgba(255,255,255,1)",
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
"size": 14.6118721461187
"size": 14.611872146118724
},
"xaxis": {
"domain": [
Expand All @@ -191,8 +191,8 @@
"type": "linear",
"autorange": false,
"range": [
9.225,
35.075
9.2249999999999996,
35.074999999999996
],
"tickmode": "array",
"ticktext": [
Expand Down Expand Up @@ -223,29 +223,29 @@
"nticks": null,
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.65296803652968,
"tickwidth": 0.66417600664176,
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
"family": "",
"size": 11.689497716895
"size": 11.68949771689498
},
"tickangle": 0,
"showline": false,
"linecolor": null,
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176,
"gridwidth": 0.66417600664176002,
"zeroline": false,
"anchor": "y",
"title": {
"text": "mpg",
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
"size": 14.6118721461187
"size": 14.611872146118724
}
},
"hoverformat": ".2f"
Expand All @@ -259,8 +259,8 @@
"type": "linear",
"autorange": false,
"range": [
1.31745,
5.61955
1.3174499999999998,
5.6195500000000003
],
"tickmode": "array",
"ticktext": [
Expand All @@ -285,29 +285,29 @@
"nticks": null,
"ticks": "outside",
"tickcolor": "rgba(51,51,51,1)",
"ticklen": 3.65296803652968,
"tickwidth": 0.66417600664176,
"ticklen": 3.6529680365296811,
"tickwidth": 0.66417600664176002,
"showticklabels": true,
"tickfont": {
"color": "rgba(77,77,77,1)",
"family": "",
"size": 11.689497716895
"size": 11.68949771689498
},
"tickangle": 0,
"showline": false,
"linecolor": null,
"linewidth": 0,
"showgrid": true,
"gridcolor": "rgba(255,255,255,1)",
"gridwidth": 0.66417600664176,
"gridwidth": 0.66417600664176002,
"zeroline": false,
"anchor": "x",
"title": {
"text": "wt",
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
"size": 14.6118721461187
"size": 14.611872146118724
}
},
"hoverformat": ".2f"
Expand Down Expand Up @@ -335,11 +335,11 @@
"legend": {
"bgcolor": "rgba(255,255,255,1)",
"bordercolor": "transparent",
"borderwidth": 1.88976377952756,
"borderwidth": 1.8897637795275593,
"font": {
"color": "rgba(0,0,0,1)",
"family": "",
"size": 11.689497716895
"size": 11.68949771689498
}
},
"hovermode": "closest",
Expand Down Expand Up @@ -374,7 +374,7 @@
"persistent": false,
"dynamic": false,
"selectize": false,
"opacityDim": 0.2,
"opacityDim": 0.20000000000000001,
"selected": {
"opacity": 1
},
Expand Down
Loading

0 comments on commit 22d715d

Please sign in to comment.