Skip to content

Commit 7149f06

Browse files
committed
Refactor/consolidate core logic
1 parent 9f67584 commit 7149f06

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ S3method(highlight_key,plotly)
2626
S3method(layout,matrix)
2727
S3method(layout,plotly)
2828
S3method(layout,shiny.tag.list)
29+
S3method(linewidth_or_size,Geom)
30+
S3method(linewidth_or_size,element)
2931
S3method(plotly_build,"NULL")
3032
S3method(plotly_build,gg)
3133
S3method(plotly_build,list)

R/ggplotly.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,10 @@ gg2list <- function(p, width = NULL, height = NULL,
684684
d$y <- scales::rescale(d$y, rng$y_range, from = c(0, 1))
685685
params <- list(
686686
colour = panelGrid$colour,
687-
linewidth = panelGrid$linewidth,
688-
size = panelGrid$size,
689687
linetype = panelGrid$linetype
690688
)
689+
nm <- linewidth_or_size(panelGrid)
690+
params[[nm]] <- panelGrid[[nm]]
691691
grill <- geom2trace.GeomPath(d, params)
692692
grill$hoverinfo <- "none"
693693
grill$showlegend <- FALSE
@@ -968,7 +968,7 @@ gg2list <- function(p, width = NULL, height = NULL,
968968
bgcolor = toRGB(theme$legend.background$fill),
969969
bordercolor = toRGB(theme$legend.background$colour),
970970
borderwidth = unitConvert(
971-
theme$legend.background$linewidth %||% theme$legend.background$size,
971+
theme$legend.background[[linewidth_or_size(theme$legend.background)]],
972972
"pixels", "width"
973973
),
974974
font = text2font(theme$legend.text)
@@ -1203,7 +1203,7 @@ verifyUnit <- function(u) {
12031203

12041204
## the default unit in ggplot2 is millimeters (unless it's element_text())
12051205
if (inherits(u, "element")) {
1206-
grid::unit(u$linewidth %||% u$size %||% 0, "points")
1206+
grid::unit(u[[linewidth_or_size(u)]] %||% 0, "points")
12071207
} else {
12081208
grid::unit(u %||% 0, "mm")
12091209
}
@@ -1423,7 +1423,8 @@ gdef2trace <- function(gdef, theme, gglayout) {
14231423
bgcolor = toRGB(theme$legend.background$fill),
14241424
bordercolor = toRGB(theme$legend.background$colour),
14251425
borderwidth = unitConvert(
1426-
theme$legend.background$linewidth %||% theme$legend.background$size, "pixels", "width"
1426+
theme$legend.background[[linewidth_or_size(theme$legend.background)]],
1427+
"pixels", "width"
14271428
),
14281429
thickness = unitConvert(
14291430
theme$legend.key.width, "pixels", "width"

R/layers2traces.R

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -387,13 +387,11 @@ to_basic.GeomHex <- function(data, prestats_data, layout, params, p, ...) {
387387
dy <- resolution(data[["y"]], FALSE)/sqrt(3)/2 * 1.15
388388
hexC <- hexbin::hexcoords(dx, dy, n = 1)
389389
n <- nrow(data)
390-
size_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
391-
size <- data[[size_var]]
392-
data[[size_var]] <- ifelse(
393-
size < 1, size ^ (1 / 6), size ^ 6
394-
)
395-
x <- rep.int(hexC[["x"]], n) * rep(data[[size_var]], each = 6) + rep(data[["x"]], each = 6)
396-
y <- rep.int(hexC[["y"]], n) * rep(data[[size_var]], each = 6) + rep(data[["y"]], each = 6)
390+
nm <- linewidth_or_size(GeomHex)
391+
size <- data[[nm]]
392+
data[[nm]] <- ifelse(size < 1, size ^ (1 / 6), size ^ 6)
393+
x <- rep.int(hexC[["x"]], n) * rep(data[[nm]], each = 6) + rep(data[["x"]], each = 6)
394+
y <- rep.int(hexC[["y"]], n) * rep(data[[nm]], each = 6) + rep(data[["y"]], each = 6)
397395
data <- data[rep(seq_len(n), each = 6), ]
398396
data[["x"]] <- x
399397
data[["y"]] <- y
@@ -563,11 +561,8 @@ to_basic.GeomSpoke <- function(data, prestats_data, layout, params, p, ...) {
563561
to_basic.GeomCrossbar <- function(data, prestats_data, layout, params, p, ...) {
564562
# from GeomCrossbar$draw_panel()
565563
middle <- base::transform(data, x = xmin, xend = xmax, yend = y, alpha = NA)
566-
data <- if ("linewidth" %in% names(middle)) {
567-
base::transform(data, linewidth = linewidth * params$fatten)
568-
} else {
569-
base::transform(data, size = size * params$fatten)
570-
}
564+
nm <- linewidth_or_size(GeomCrossbar)
565+
data[[nm]] <- data[[nm]] * params$fatten
571566
list(
572567
prefix_class(to_basic.GeomRect(data), "GeomCrossbar"),
573568
prefix_class(to_basic.GeomSegment(middle), "GeomCrossbar")
@@ -707,9 +702,6 @@ geom2trace.GeomBlank <- function(data, params, p) {
707702
geom2trace.GeomPath <- function(data, params, p) {
708703
data <- group2NA(data)
709704

710-
# ggplot2 >3.4.0 changed from size to linewidth for controlling line width
711-
width_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
712-
713705
L <- list(
714706
x = data[["x"]],
715707
y = data[["y"]],
@@ -723,7 +715,7 @@ geom2trace.GeomPath <- function(data, params, p) {
723715
name = if (inherits(data, "GeomSmooth")) "fitted values",
724716
line = list(
725717
# TODO: line width array? -- https://github.com/plotly/plotly.js/issues/147
726-
width = aes2plotly(data, params, width_var)[1],
718+
width = aes2plotly(data, params, linewidth_or_size(GeomPath))[1],
727719
color = toRGB(
728720
aes2plotly(data, params, "colour"),
729721
aes2plotly(data, params, "alpha")
@@ -791,9 +783,6 @@ geom2trace.GeomBar <- function(data, params, p) {
791783
base <- data[["ymin"]]
792784
x <- with(data, ymax - ymin)
793785
}
794-
795-
# ggplot2 >3.4.0 changed from size to linewidth for controlling line width
796-
width_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
797786

798787
compact(list(
799788
orientation = if (flip) "h" else "v",
@@ -819,7 +808,7 @@ geom2trace.GeomBar <- function(data, params, p) {
819808
aes2plotly(data, params, "alpha")
820809
),
821810
line = list(
822-
width = aes2plotly(data, params, width_var),
811+
width = aes2plotly(data, params, linewidth_or_size(GeomBar)),
823812
color = aes2plotly(data, params, "colour")
824813
)
825814
)
@@ -830,9 +819,6 @@ geom2trace.GeomBar <- function(data, params, p) {
830819
geom2trace.GeomPolygon <- function(data, params, p) {
831820
data <- group2NA(data)
832821

833-
# ggplot2 >3.4.0 changed from size to linewidth for controlling line width
834-
width_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
835-
836822
L <- list(
837823
x = data[["x"]],
838824
y = data[["y"]],
@@ -844,7 +830,7 @@ geom2trace.GeomPolygon <- function(data, params, p) {
844830
type = "scatter",
845831
mode = "lines",
846832
line = list(
847-
width = aes2plotly(data, params, width_var),
833+
width = aes2plotly(data, params, linewidth_or_size(GeomPolygon)),
848834
color = toRGB(
849835
aes2plotly(data, params, "colour"),
850836
aes2plotly(data, params, "alpha")
@@ -865,9 +851,6 @@ geom2trace.GeomPolygon <- function(data, params, p) {
865851

866852
#' @export
867853
geom2trace.GeomBoxplot <- function(data, params, p) {
868-
# ggplot2 >3.4.0 changed from size to linewidth for controlling line width
869-
width_var <- if ("linewidth" %in% names(data)) "linewidth" else "size"
870-
871854
compact(list(
872855
x = data[["x"]],
873856
y = data[["y"]],
@@ -894,7 +877,7 @@ geom2trace.GeomBoxplot <- function(data, params, p) {
894877
),
895878
line = list(
896879
color = aes2plotly(data, params, "colour"),
897-
width = aes2plotly(data, params, width_var)
880+
width = aes2plotly(data, params, linewidth_or_size(GeomBoxplot))
898881
)
899882
))
900883
}
@@ -1135,6 +1118,26 @@ aes2plotly <- function(data, params, aes = "size") {
11351118
converter(vals)
11361119
}
11371120

1121+
1122+
# ggplot2 3.4.0 deprecated size in favor of linewidth in line-based geoms (e.g.,
1123+
# GeomLine, GeomRect, etc) and elements (e.g., element_line(), element_rect(),
1124+
# etc). Note that, some geoms (e.g., GeomBoxplot, GeomSf) can have both
1125+
# linewidth and size
1126+
linewidth_or_size <- function(x) {
1127+
UseMethod("linewidth_or_size")
1128+
}
1129+
1130+
#' @export
1131+
linewidth_or_size.Geom <- function(x) {
1132+
if ("linewidth" %in% x$aesthetics()) "linewidth" else "size"
1133+
}
1134+
1135+
#' @export
1136+
linewidth_or_size.element <- function(x) {
1137+
if ("linewidth" %in% names(x)) "linewidth" else "size"
1138+
}
1139+
1140+
11381141
# Convert R pch point codes to plotly "symbol" codes.
11391142
pch2symbol <- function(x) {
11401143
lookup <- list(

0 commit comments

Comments
 (0)