diff --git a/DESCRIPTION b/DESCRIPTION index 4b1c431222..8ed1f20701 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: plotly Type: Package Title: Interactive, publication-quality graphs online. -Version: 0.5.28 +Version: 0.5.29 Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"), email = "chris@plot.ly"), person("Scott", "Chamberlain", role = "aut", diff --git a/NEWS b/NEWS index 2239b51be7..1509920b1a 100644 --- a/NEWS +++ b/NEWS @@ -1,4 +1,8 @@ -0.5.27 -- 15 April 2015 +0.5.29 -- 16 April 2015 + +geom_density() as filled area chart #202 + +0.5.28 -- 15 April 2015 Let ggplot handle histogram binning. Fix #198 diff --git a/R/trace_generation.R b/R/trace_generation.R index 45bca7a002..673e88d80b 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -251,10 +251,6 @@ layer2traces <- function(l, d, misc) { "stack" } else "group" } - # TODO: remove this once we reimplement density as area - if (g$geom == "density") { - tr$bargap <- 0 - } traces <- c(traces, list(tr)) } @@ -354,10 +350,9 @@ toBasic <- list( g }, density=function(g) { - g$params$xstart <- min(g$data$x) - g$params$xend <- max(g$data$x) - g$params$binwidth <- (max(g$data$x) - min(g$data$x))/30 - g$data <- g$prestats.data + g$geom <- "area" + if (is.null(g$data$fill) && is.null(g$params$alpha)) g$params$alpha <- 0 + if (is.null(g$data$colour)) g$params$colour <- "black" g }, density2d=function(g) { @@ -628,15 +623,6 @@ geom2trace <- list( L$contours=list(coloring="lines") L }, - density=function(data, params) { - L <- list(x=data$x, - name=params$name, - text=data$text, - marker=list(color=toRGB(params$fill)), - type="histogram", - autobinx=TRUE, - histnorm="probability density") - }, density2d=function(data, params) { L <- list(x=data$x, y=data$y, diff --git a/tests/testthat/test-ggplot-density.R b/tests/testthat/test-ggplot-density.R index a1d4de8100..0d77b191d4 100644 --- a/tests/testthat/test-ggplot-density.R +++ b/tests/testthat/test-ggplot-density.R @@ -1,15 +1,45 @@ context("Probability density") +expect_traces <- function(gg, n.traces, name) { + stopifnot(is.ggplot(gg)) + stopifnot(is.numeric(n.traces)) + save_outputs(gg, paste0("density-", name)) + L <- gg2list(gg) + is.trace <- names(L) == "" + all.traces <- L[is.trace] + 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, kwargs=L$kwargs) +} + # Draw a probability density estimation using geom_density -m <- ggplot(movies) + geom_density(aes(rating)) -L <- gg2list(m) - -test_that("geom_density is translated to a normalized histogram", { - expect_equal(length(L), 2) - expect_identical(L[[1]]$type, "histogram") - expect_true(L[[1]]$autobinx) - expect_identical(L[[1]]$histnorm, "probability density") - expect_equal(L[[2]]$layout$bargap, 0) +base <- ggplot(mtcars, aes(wt)) + +test_that("geom_density() is translated to area chart", { + info <- expect_traces(base + geom_density(), 1, "simple") + tr <- info$traces[[1]] + expect_identical(tr$type, "scatter") + expect_identical(tr$fill, "tozeroy") + expect_identical(tr$fillcolor, "rgba(51,51,51,0)") }) -save_outputs(m, "density") +test_that("geom_density() respects fill aesthetic", { + info <- expect_traces(base + geom_density(aes(fill=factor(vs))), 2, "fill") + trs <- info$traces + type <- unique(sapply(trs, "[[", "type")) + fill <- unique(sapply(trs, "[[", "fill")) + expect_identical(type, "scatter") + expect_identical(fill, "tozeroy") +}) + +test_that("geom_density() respects colour aesthetic", { + info <- expect_traces(base + geom_density(aes(colour=factor(vs))), 2, "color") + trs <- info$traces + type <- unique(sapply(trs, "[[", "type")) + fill <- unique(sapply(trs, "[[", "fill")) + expect_identical(type, "scatter") + expect_identical(fill, "tozeroy") +})