Skip to content

Commit 4655d89

Browse files
committed
Merge branch 'master' of github.com:ropensci/plotly into fix/subplot
2 parents f8eedaa + 5e0ad36 commit 4655d89

23 files changed

+204
-113
lines changed

CONTRIBUTING.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ Sys.setenv('TRAVIS_COMMIT' = substr(system('git rev-parse HEAD', intern = T), 1,
2323
devtools::load_all(); source('tests/testthat.R', chdir = TRUE)
2424
```
2525

26-
You can also build a ggplot2/plotly comparison table:
26+
You can also build a ggplot2/plotly comparison table. To do so, you'll first need to clone the [plotly-test-table](https://github.com/cpsievert/plotly-test-table) repo.
27+
28+
```shell
29+
$ git clone https://github.com/cpsievert/plotly-test-table.git ../plotly-test-table
30+
```
31+
32+
Then, from R:
2733

2834
```r
2935
Sys.setenv('PLOTLY_TABLE' = 'TRUE')

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via 'plotly.js'
3-
Version: 3.4.15
3+
Version: 3.5.1
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NAMESPACE

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ S3method(geom2trace,default)
1414
S3method(ggplotly,ggmatrix)
1515
S3method(ggplotly,ggplot)
1616
S3method(print,figure)
17-
S3method(print,plotly)
17+
S3method(print,plotly_built)
18+
S3method(print,plotly_hash)
1819
S3method(to_basic,GeomAbline)
1920
S3method(to_basic,GeomArea)
2021
S3method(to_basic,GeomBoxplot)
@@ -51,7 +52,8 @@ export(get_figure)
5152
export(gg2list)
5253
export(ggplotly)
5354
export(knit_print.figure)
54-
export(knit_print.plotly)
55+
export(knit_print.plotly_built)
56+
export(knit_print.plotly_hash)
5557
export(last_plot)
5658
export(layout)
5759
export(offline)

NEWS

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
3.6.0 -- 26 Apr 2016
2+
3+
CHANGES:
4+
5+
Upgrade to plotlyjs v1.10.0 -- https://github.com/plotly/plotly.js/releases/tag/v1.9.0
6+
7+
Distinguish between "built" (plotly_built) and "non-built" (plotly_hash) plotly objects. See #562
8+
9+
10+
3.5.0 -- 19 Apr 2016
11+
12+
NEW FEATURES:
13+
14+
The toRGB() function will now respect alpha channels in hex color codes and can recursively apply alpha.
15+
16+
CHANGES:
17+
18+
The toRGB() function will always output color codes with an alpha channel (e.g. toRGB('black') is now 'rgba(0,0,0,1)' instead of 'rgb(0,0,0)')
19+
120
3.4.15 -- 18 Apr 2016
221

322
BUGFIX:

R/ggplotly.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,6 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
300300
theme[["strip.text.x"]] %||% theme[["strip.text"]],
301301
"npc", "height"
302302
)
303-
# TODO: why does stripSize need to be inflated here?
304-
panelMarginY <- panelMarginY + 1.5 * stripSize
305303
# space for ticks/text in free scales
306304
if (p$facet$free$x) {
307305
axisTicksX <- unitConvert(

R/imports.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
#' @importFrom viridis viridis
88
#' @importFrom jsonlite toJSON fromJSON
99
#' @importFrom httr GET POST PATCH content config add_headers stop_for_status
10+
#' @importFrom htmlwidgets createWidget sizingPolicy
1011
NULL

R/layers2traces.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,10 @@ geom2trace.GeomPolygon <- function(data, params, p) {
460460
line = list(
461461
# NOTE: line attributes must be constant on a polygon
462462
width = aes2plotly(data, params, "size"),
463-
color = aes2plotly(data, params, "colour"),
463+
color = toRGB(
464+
aes2plotly(data, params, "colour"),
465+
aes2plotly(data, params, "alpha")
466+
),
464467
dash = aes2plotly(data, params, "linetype")
465468
),
466469
fill = "tozerox",

R/plotly.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ plotly_build <- function(l = last_plot()) {
332332
x$layout$hovermode <- x$layout$hovermode %||% "closest"
333333
}
334334
# add plotly class mainly for printing method
335-
structure(x, class = unique("plotly", class(x)))
335+
structure(x, class = unique("plotly_built", class(x)))
336336
}
337337

338338
# returns a _list of traces_.

R/print.R

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,36 @@
33
#' @param x a plotly object
44
#' @param ... additional arguments
55
#' @export
6-
#' @importFrom htmlwidgets createWidget
7-
#' @importFrom htmlwidgets sizingPolicy
8-
print.plotly <- function(x, ...) {
9-
if (!inherits(x, "htmlwidget")) x <- as.widget(x)
10-
get("print.htmlwidget", envir = asNamespace("htmlwidgets"))(x, ...)
6+
print.plotly_hash <- function(x, ...) {
7+
x <- as.widget(x)
8+
print(x, ...)
119
}
1210

11+
#' Print a 'built' plotly object
12+
#'
13+
#' @param x a plotly object
14+
#' @param ... additional arguments
15+
#' @export
16+
print.plotly_built <- print.plotly_hash
17+
1318
#' Print a plotly object in a knitr doc
1419
#'
1520
#' @param x a plotly object
1621
#' @param options knitr options.
1722
#' @param ... additional arguments
1823
#' @export
19-
knit_print.plotly <- function(x, options, ...) {
20-
if (!inherits(x, "htmlwidget")) x <- as.widget(x)
21-
get("knit_print.htmlwidget", envir = asNamespace("htmlwidgets"))(x, options = options, ...)
24+
knit_print.plotly_hash <- function(x, options, ...) {
25+
x <- as.widget(x)
26+
knitr::knit_print(x, options, ...)
2227
}
2328

29+
#' Print a 'built' plotly object in a knitr doc
30+
#'
31+
#' @param x a plotly object
32+
#' @param ... additional arguments
33+
#' @export
34+
knit_print.plotly_built <- knit_print.plotly_hash
35+
2436
#' Convert a plotly object to an htmlwidget object
2537
#'
2638
#' @param x a plotly object.
@@ -34,22 +46,22 @@ knit_print.plotly <- function(x, options, ...) {
3446

3547
as.widget <- function(x, ...) {
3648
if (inherits(x, "htmlwidget")) return(x)
37-
p <- plotly_build(x)
49+
if (!inherits(x, "plotly_built")) x <- plotly_build(x)
3850
# set some margin defaults if none are provided
39-
p$layout$margin <- modifyList(
51+
x$layout$margin <- modifyList(
4052
list(b = 40, l = 60, t = 25, r = 10),
41-
p$layout$margin %||% list()
53+
x$layout$margin %||% list()
4254
)
43-
p$config$modeBarButtonsToRemove <-
44-
i(p$config$modeBarButtonsToRemove %||% "sendDataToCloud")
45-
p$base_url <- get_domain()
55+
x$config$modeBarButtonsToRemove <-
56+
i(x$config$modeBarButtonsToRemove %||% "sendDataToCloud")
57+
x$base_url <- get_domain()
4658
# customize the JSON serializer (for htmlwidgets)
47-
attr(p, 'TOJSON_FUNC') <- to_JSON
59+
attr(x, 'TOJSON_FUNC') <- to_JSON
4860
htmlwidgets::createWidget(
4961
name = "plotly",
50-
x = p,
51-
width = p$width,
52-
height = p$height,
62+
x = x,
63+
width = x$width,
64+
height = x$height,
5365
sizingPolicy = htmlwidgets::sizingPolicy(
5466
padding = 5,
5567
browser.fill = TRUE

R/toRGB.R

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,36 @@
55
#' @export
66
toRGB <- function(x, alpha = 1) {
77
if (is.null(x)) return(x)
8-
# as of ggplot2 version 1.1, an NA alpha is treated as though it's 1
9-
alpha[is.na(alpha)] <- 1
10-
# if we've already made the proper conversion, return the input
11-
if (inherits(x, "plotly_rgba")) return(x)
12-
if (inherits(x, "plotly_rgb")) {
13-
if (all(alpha == 1)) return(x)
14-
# all alpha channel
15-
x <- sub("^rgb", "rgba", sub("\\)", paste0(",", alpha, ")"), x))
16-
return(prefix_class(x, "plotly_rgba"))
8+
if (any(x %in% "transparent")) return(x)
9+
# add alpha to already converted "rgb(x,y,z)" codes
10+
idx <- grepl("^rgba\\(", x) & alpha <= 1 & 0 <= alpha
11+
if (any(idx)) {
12+
x[idx] <- rgb2hex(x[idx])
1713
}
1814
# for some reason ggplot2 has "NA" in some place (instead of NA)
1915
if (is.character(x)) {
2016
x[x == "NA"] <- NA
2117
}
22-
has_alpha <- all(0 <= alpha & alpha < 1)
23-
rgb_matrix <- col2rgb(x, alpha = has_alpha)
24-
# rescale alpha
25-
# TODO: what if x already has an alpha channel???
26-
if (has_alpha) rgb_matrix["alpha", ] <- alpha
27-
container <- if (has_alpha) "rgba(%s)" else "rgb(%s)"
28-
rgb_a <- sprintf(container, apply(rgb_matrix, 2, paste, collapse = ","))
29-
rgb_a[is.na(x)] <- "transparent"
30-
structure(rgb_a, class = if (has_alpha) "plotly_rgba" else "plotly_rgb")
18+
# as of ggplot2 version 1.1, an NA alpha is treated as though it's 1
19+
alpha[is.na(alpha)] <- 1
20+
rgb_matrix <- grDevices::col2rgb(x, alpha = TRUE)
21+
# multiply the existing alpha with specified alpha (both on 0-1 scale)
22+
rgb_matrix["alpha", ] <- alpha * scales::rescale(
23+
rgb_matrix["alpha", ], from = c(0, 255)
24+
)
25+
rgb_matrix["alpha", ] <- round(rgb_matrix["alpha", ], 4)
26+
rgba <- sprintf("rgba(%s)", apply(rgb_matrix, 2, paste, collapse = ","))
27+
rgba[is.na(x)] <- "transparent"
28+
rgba
29+
}
30+
31+
# take a 'plotly color' and produce a hex code
32+
rgb2hex <- function(string = "rgba(255,255,255,1)") {
33+
vals <- sub("rgba\\(", "", sub("\\)", "", string))
34+
valz <- strsplit(vals, ",")
35+
sapply(valz, function(x) {
36+
x <- setNames(as.numeric(x), c("red", "green", "blue", "alpha"))
37+
x[["alpha"]] <- x[["alpha"]] * 255
38+
do.call(grDevices::rgb, c(x, list(maxColorValue = 255)))
39+
})
3140
}

0 commit comments

Comments
 (0)