Skip to content

Added legends for boxplots and densities #230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 13, 2015
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: plotly
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
Version: 2.0.13
Version: 2.0.14
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
email = "[email protected]"),
person("Chris", "Parmer", role = c("aut", "cph"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.0.14 -- 13 Dec 2015

Fix #212

2.0.13 -- 12 Dec 2015

Fix #286
Expand Down
5 changes: 4 additions & 1 deletion R/ggplotly.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ markLegends <-
## characteristics and could be drawn using just 1 trace!
polygon=c("colour", "fill", "linetype", "size"),
bar=c("colour", "fill"),
density=c("colour", "fill", "linetype"),
boxplot=c("colour", "fill", "size"),
errorbar=c("colour", "linetype"),
errorbarh=c("colour", "linetype"),
area=c("colour", "fill"),
Expand All @@ -114,7 +116,8 @@ markLegends <-

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

markSplit <- c(markLegends,list(boxplot=c("x")))
markSplit <- markLegends
markSplit$boxplot <- "x"

guide_names <- function(p, aes = c("shape", "fill", "alpha", "area",
"color", "colour", "size", "linetype")) {
Expand Down
43 changes: 42 additions & 1 deletion tests/testthat/test-ggplot-boxplot.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
context("Boxplot")

expect_traces <- function(gg, n.traces, name) {
stopifnot(is.ggplot(gg))
stopifnot(is.numeric(n.traces))
save_outputs(gg, paste0("boxplot-", name))
L <- gg2list(gg)
all.traces <- L$data
no.data <- sapply(all.traces, function(tr) {
is.null(tr[["x"]]) && is.null(tr[["y"]])
})
has.data <- all.traces[!no.data]
expect_equal(length(has.data), n.traces)
list(traces=has.data, layout=L$layout)
}

test_that("geom_boxplot gives a boxplot", {
gg <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()

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

# check legend shows up when each box-and-whiskers has a fill
# make ggplot2
m <- ggplot(mtcars, aes(factor(cyl), mpg))
p <- m + geom_boxplot(aes(fill = factor(cyl)))
# tests
test_that("legends for boxplot", {
info <- expect_traces(p, 3, "legends_for_fill")
tr <- info$traces
la <- info$layout
expect_identical(tr[[1]]$type, "box")
# check legend exists
expect_identical(la$showlegend, TRUE)
# check legend for each fill exists
for (i in 1:3) {
expect_identical(tr[[i]]$showlegend, TRUE)
}
# check the fill colors are correct
g <- ggplot_build(p)
fill.colors <- unique(g$data[[1]]["fill"])[,1]
for (i in 1:3) {
plotly.color <- as.integer(strsplit(gsub("[\\(\\)]|rgb", "",
tr[[i]]$fillcolor), split = ",")[[1]])
names(plotly.color) <- c("red", "green", "blue")
expect_equal(plotly.color, col2rgb(fill.colors[i])[,1],
tolerance = 1)
}
})

dat <- data.frame(
cond = factor(rep(c("A", "B", "C", "D"), each = 200)),
col = factor(rep(c("C1", "C2"), each = 400)),
Expand All @@ -58,4 +100,3 @@ test_that("correct # of unique fillcolors", {
fills <- sapply(L$data, "[[", "fillcolor")
expect_equal(length(unique(fills)), length(unique(dat$col)))
})

4 changes: 4 additions & 0 deletions tests/testthat/test-ggplot-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ test_that("geom_density() respects fill aesthetic", {
fill <- unique(sapply(trs, "[[", "fill"))
expect_identical(type, "scatter")
expect_identical(fill, "tozeroy")
# check legend exists
expect_true(info$layout$showlegend, TRUE)
# check legend for each fill exists
expect_true(all(sapply(trs, "[[", "showlegend")))
})

test_that("geom_density() respects colour aesthetic", {
Expand Down
Empty file.