Skip to content

Commit 5509909

Browse files
committed
Merge pull request #230 from ropensci/baobao-legend_hide
Added legends for boxplots and densities
2 parents b84a4fe + 559381b commit 5509909

File tree

6 files changed

+55
-3
lines changed

6 files changed

+55
-3
lines changed

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's JavaScript Graphing Library
3-
Version: 2.0.13
3+
Version: 2.0.14
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.0.14 -- 13 Dec 2015
2+
3+
Fix #212
4+
15
2.0.13 -- 12 Dec 2015
26

37
Fix #286

R/ggplotly.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ markLegends <-
106106
## characteristics and could be drawn using just 1 trace!
107107
polygon=c("colour", "fill", "linetype", "size"),
108108
bar=c("colour", "fill"),
109+
density=c("colour", "fill", "linetype"),
110+
boxplot=c("colour", "fill", "size"),
109111
errorbar=c("colour", "linetype"),
110112
errorbarh=c("colour", "linetype"),
111113
area=c("colour", "fill"),
@@ -114,7 +116,8 @@ markLegends <-
114116

115117
markUnique <- as.character(unique(unlist(markLegends)))
116118

117-
markSplit <- c(markLegends,list(boxplot=c("x")))
119+
markSplit <- markLegends
120+
markSplit$boxplot <- "x"
118121

119122
guide_names <- function(p, aes = c("shape", "fill", "alpha", "area",
120123
"color", "colour", "size", "linetype")) {

tests/testthat/test-ggplot-boxplot.R

Lines changed: 42 additions & 1 deletion
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

@@ -43,6 +57,34 @@ test_that("you can make a boxplot for a distribution of datetimes", {
4357
expect_identical(L$data[[1]]$y, as.character(df$y))
4458
})
4559

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+
4688
dat <- data.frame(
4789
cond = factor(rep(c("A", "B", "C", "D"), each = 200)),
4890
col = factor(rep(c("C1", "C2"), each = 400)),
@@ -58,4 +100,3 @@ test_that("correct # of unique fillcolors", {
58100
fills <- sapply(L$data, "[[", "fillcolor")
59101
expect_equal(length(unique(fills)), length(unique(dat$col)))
60102
})
61-

tests/testthat/test-ggplot-density.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ test_that("geom_density() respects fill aesthetic", {
3232
fill <- unique(sapply(trs, "[[", "fill"))
3333
expect_identical(type, "scatter")
3434
expect_identical(fill, "tozeroy")
35+
# check legend exists
36+
expect_true(info$layout$showlegend, TRUE)
37+
# check legend for each fill exists
38+
expect_true(all(sapply(trs, "[[", "showlegend")))
3539
})
3640

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

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

Whitespace-only changes.

0 commit comments

Comments
 (0)