Skip to content

Commit 7d0890d

Browse files
committed
bug fixes for GeomTile, GeomPoint, and hoverinfo
1 parent de00838 commit 7d0890d

File tree

5 files changed

+42
-23
lines changed

5 files changed

+42
-23
lines changed

.Rbuildignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
^data-raw$
55
CONDUCT.md
66
CONTRIBUTING.md
7+
build_site.R

R/ggplotly.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
191191
map <- sub("^\\.\\.", "", sub("\\.\\.$", "", map))
192192
# TODO: allow users to specify a _list_ of mappings?
193193
if (!identical(tooltip, "all")) {
194-
map <- map[names(map) %in% tooltip]
194+
map <- map[tooltip]
195195
}
196196
# tooltips for discrete positional scales are misleading
197197
if (scales$get_scales("x")$is_discrete()) {
@@ -220,12 +220,14 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all", source = "A
220220
if ("datetime" %in% scaleName) forMat <- function(x) as.POSIXct(x / 1000, origin = "1970-01-01")
221221
# convert "days from the UNIX epoch" to a date/datetime
222222
if ("date" %in% scaleName) forMat <- function(x) as.Date(as.POSIXct(x * 86400, origin = "1970-01-01"))
223-
} else {
224-
if (aesName != "text") aesName <- paste0(aesName, "_plotlyDomain")
225223
}
226224
# add a line break if hovertext already exists
227225
if ("hovertext" %in% names(x)) x$hovertext <- paste0(x$hovertext, "<br>")
228-
x$hovertext <- paste0(x$hovertext, varName, ": ", forMat(x[[aesName]]))
226+
x$hovertext <- paste0(
227+
x$hovertext,
228+
varName, ": ",
229+
forMat(x[[paste0(aesName, "_plotlyDomain")]] %||% x[[aesName]])
230+
)
229231
}
230232
x
231233
}, data, aesMap)

R/layers2traces.R

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ geom2trace.GeomPoint <- function(data, params) {
380380
)
381381
# fill is irrelevant for pch %in% c(1, 15:20)
382382
pch <- uniq(data$shape) %||% params$shape %||% GeomPoint$default_aes$shape
383-
if (any(pch %in% c(1, 15:20))) {
383+
if (any(pch %in% c(1, 15:20)) ||
384+
all(grepl("open$", shape)) && all(L$marker$color %in% "transparent")) {
384385
L$marker$color <- L$marker$line$color
385386
}
386387
L
@@ -491,27 +492,23 @@ geom2trace.GeomText <- function(data, params) {
491492
#' @export
492493
geom2trace.GeomTile <- function(data, params) {
493494
# make sure order of value make sense before throwing z in matrix
494-
data <- data[order(data$x, order(data$y, decreasing = T)), ]
495+
data <- data[order(order(data$x), data$y), ]
495496
x <- sort(unique(data$x))
496497
y <- sort(unique(data$y))
497-
fill <- data$fill_plotlyDomain
498-
colorscale <- cbind(
499-
c(0, 1),
500-
data[c(which.min(fill), which.max(fill)), "fill"]
501-
)
498+
fill <- scales::rescale(data$fill_plotlyDomain)
499+
txt <- data$hovertext
500+
# create the colorscale, which should ignore NAs
501+
data <- data[!is.na(fill), ]
502+
o <- data[order(data$fill_plotlyDomain), "fill"]
503+
n <- length(o)
504+
qs <- seq(0, 1, length.out = min(n, 100))
505+
idx <- o[pmax(1, round(n * qs))]
506+
colorscale <- cbind(qs, idx)
502507
list(
503508
x = x,
504509
y = y,
505-
z = matrix(
506-
scales::rescale(fill),
507-
nrow = length(y),
508-
ncol = length(x)
509-
),
510-
text = matrix(
511-
data$hovertext,
512-
nrow = length(y),
513-
ncol = length(x)
514-
),
510+
z = matrix(fill, nrow = length(y), ncol = length(x)),
511+
text = matrix(txt, nrow = length(y), ncol = length(x)),
515512
colorscale = colorscale,
516513
type = "heatmap",
517514
showscale = FALSE,
@@ -619,10 +616,10 @@ make_error <- function(data, params, xy = "x") {
619616
e <- list(
620617
x = data$x,
621618
y = data$y,
619+
text = data$hovertext,
622620
type = "scatter",
623621
mode = "lines",
624622
opacity = 0,
625-
hoverinfo = "none",
626623
line = list(color = color)
627624
)
628625
e[[paste0("error_", xy)]] <- list(

tests/testthat/test-ggplot-density.R

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
context("Probability density")
22

33
expect_traces <- function(gg, n.traces, name) {
4-
stopifnot(is.ggplot(gg))
54
stopifnot(is.numeric(n.traces))
65
L <- save_outputs(gg, paste0("density-", name))
76
all.traces <- L$data
@@ -73,3 +72,12 @@ test_that("traces are ordered correctly in geom_density", {
7372
expect_identical(nms, c("4", "6", "8"))
7473
})
7574

75+
test_that("tooltip argument respects ordering", {
76+
p <- qplot(mpg, fill = factor(cyl), data = mtcars, geom = "density")
77+
p <- ggplotly(p, tooltip = c("y", "x"))
78+
info <- expect_traces(p, 3, "tooltip-order")
79+
txt <- strsplit(info$data[[1]]$text, "<br>")
80+
expect_true(all(grepl("^density", sapply(txt, "[[", 1))))
81+
expect_true(all(grepl("^mpg", sapply(txt, "[[", 2))))
82+
})
83+

tests/testthat/test-ggplot-point.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ test_that("geom_point size & alpha translate to a single trace", {
2323
expect_equal(length(mkr$opacity), nrow(mtcars))
2424
})
2525

26+
test_that("marker color is non-transparent for open shapes", {
27+
p <- ggplot(mtcars, aes(mpg, wt)) + geom_point(pch = 2)
28+
info <- save_outputs(p, "open-shapes")
29+
expect_true(
30+
grepl("open$", info$data[[1]]$marker$symbol)
31+
)
32+
expect_true(
33+
info$data[[1]]$marker$color == toRGB(GeomPoint$default_aes$colour)
34+
)
35+
})
36+
2637
test_that("can plot on sub-second time scale", {
2738
d <- data.frame(
2839
x = Sys.time() + 1e-3 * c(1:9, 5000),

0 commit comments

Comments
 (0)