Skip to content

Commit cbe1a51

Browse files
committed
Merge branch 'baobao-legend_hide' of https://github.com/ropensci/plotly into baobao-legend_hide
2 parents d7a340c + 7b6f87f commit cbe1a51

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

R/ggplotly.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ markLegends <-
105105
## group, even when they have the exact same visual
106106
## characteristics and could be drawn using just 1 trace!
107107
polygon=c("colour", "fill", "linetype", "size"),
108-
bar=c("colour", "fill"),
108+
bar = c("colour", "fill"),
109+
density = c("colour", "fill", "linetype"),
109110
errorbar=c("colour", "linetype"),
110111
errorbarh=c("colour", "linetype"),
111112
area=c("colour", "fill"),
@@ -305,6 +306,9 @@ gg2list <- function(p) {
305306
traces <- layer2traces(L, df, misc)
306307

307308
possible.legends <- markLegends[[L$geom$objname]]
309+
if (L$geom$objname == "boxplot"){
310+
possible.legends <- markSplit[[L$geom$objname]]
311+
}
308312
actual.legends <- possible.legends[possible.legends %in% names(L$mapping)]
309313
layer.legends[[paste(i)]] <- actual.legends
310314

tests/testthat/test-ggplot-boxplot.R

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
context("Boxplot")
22

3+
expect_traces <- function(gg, n.traces, name) {
4+
stopifnot(is.ggplot(gg))
5+
stopifnot(is.numeric(n.traces))
6+
save_outputs(gg, paste0("boxplot-", name))
7+
L <- gg2list(gg)
8+
all.traces <- L$data
9+
no.data <- sapply(all.traces, function(tr) {
10+
is.null(tr[["x"]]) && is.null(tr[["y"]])
11+
})
12+
has.data <- all.traces[!no.data]
13+
expect_equal(length(has.data), n.traces)
14+
list(traces=has.data, layout=L$layout)
15+
}
16+
317
test_that("geom_boxplot gives a boxplot", {
418
gg <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
519

@@ -42,3 +56,33 @@ test_that("you can make a boxplot for a distribution of datetimes", {
4256
expect_identical(L$data[[1]]$type, "box")
4357
expect_identical(L$data[[1]]$y, as.character(df$y))
4458
})
59+
60+
# check legend shows up when each box-and-whiskers has a fill
61+
# make ggplot2
62+
m <- ggplot(mtcars, aes(factor(cyl), mpg))
63+
p <- m + geom_boxplot(aes(fill = factor(cyl)))
64+
# tests
65+
test_that("legends for boxplot", {
66+
info <- expect_traces(p, 3, "legends_for_fill")
67+
tr <- info$traces
68+
la <- info$layout
69+
expect_identical(tr[[1]]$type, "box")
70+
# check legend exists
71+
expect_identical(la$showlegend, TRUE)
72+
# check legend for each fill exists
73+
for (i in 1:3) {
74+
expect_identical(tr[[i]]$showlegend, TRUE)
75+
}
76+
# check the fill colors are correct
77+
g <- ggplot_build(p)
78+
fill.colors <- unique(g$data[[1]]["fill"])[,1]
79+
for (i in 1:3) {
80+
plotly.color <- as.integer(strsplit(gsub("[\\(\\)]|rgb", "",
81+
tr[[i]]$fillcolor), split = ",")[[1]])
82+
names(plotly.color) <- c("red", "green", "blue")
83+
expect_equal(plotly.color, col2rgb(fill.colors[i])[,1],
84+
tolerance = 1)
85+
}
86+
})
87+
88+

tests/testthat/test-ggplot-density.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,28 @@ test_that("geom_density() respects fill aesthetic", {
2828
gg <- base + geom_density(aes(fill=factor(vs)), alpha = 0.3)
2929
info <- expect_traces(gg, 2, "fill")
3030
trs <- info$traces
31+
la <- info$layout
3132
type <- unique(sapply(trs, "[[", "type"))
3233
fill <- unique(sapply(trs, "[[", "fill"))
3334
expect_identical(type, "scatter")
3435
expect_identical(fill, "tozeroy")
36+
# check legend exists
37+
expect_identical(la$showlegend, TRUE)
38+
# check legend for each fill exists
39+
for (i in 1:2) {
40+
expect_identical(trs[[i]]$showlegend, TRUE)
41+
}
42+
# check the fill colors are correct
43+
g <- ggplot_build(gg)
44+
fill.colors <- unique(g$data[[1]]["fill"])[,1]
45+
for (i in 1:2) {
46+
plotly.color <- as.integer(
47+
strsplit(gsub("[\\(\\)]|rgba", "",
48+
trs[[i]]$fillcolor), split = ",")[[1]])[1:3]
49+
names(plotly.color) <- c("red", "green", "blue")
50+
expect_equal(plotly.color, col2rgb(fill.colors[i])[,1],
51+
tolerance = 1)
52+
}
3553
})
3654

3755
test_that("geom_density() respects colour aesthetic", {

tests/testthat/test-ggplot-trace-order.R

Whitespace-only changes.

0 commit comments

Comments
 (0)