From 261244ada631b2dcf9d603989572621c65445160 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Mon, 13 Apr 2015 13:05:46 -0500 Subject: [PATCH 1/5] geom_density is now a stacked area chart, Fix #199. --- R/trace_generation.R | 20 ++--------- tests/testthat/test-ggplot-density.R | 50 ++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 27 deletions(-) diff --git a/R/trace_generation.R b/R/trace_generation.R index 45bca7a002..8c07aa2934 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 <- group2NA(g, "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..1fba382c8d 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[[1]]$type, "scatter") + expect_identical(tr[[1]]$fill, "tozeroy") + expect_identical(tr[[1]]$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")) + type <- 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")) + type <- unique(sapply(trs, "[[", "fill")) + expect_identical(type, "scatter") + expect_identical(fill, "tozeroy") +}) From 4659669a2ea26bb8537cfc70b6e714afd6f62094 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Mon, 13 Apr 2015 15:48:33 -0500 Subject: [PATCH 2/5] Density test fixes --- tests/testthat/test-ggplot-density.R | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/testthat/test-ggplot-density.R b/tests/testthat/test-ggplot-density.R index 1fba382c8d..99639ebe60 100644 --- a/tests/testthat/test-ggplot-density.R +++ b/tests/testthat/test-ggplot-density.R @@ -21,16 +21,16 @@ 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[[1]]$type, "scatter") - expect_identical(tr[[1]]$fill, "tozeroy") - expect_identical(tr[[1]]$fillcolor, "rgba(51,51,51,0)") + expect_identical(tr$type, "scatter") + expect_identical(tr$fill, "tozeroy") + expect_identical(tr$fillcolor, "rgba(51,51,51,0)") }) 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")) - type <- unique(sapply(trs, "[[", "fill")) + fill <- unique(sapply(trs, "[[", "fill")) expect_identical(type, "scatter") expect_identical(fill, "tozeroy") }) @@ -39,7 +39,7 @@ 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")) - type <- unique(sapply(trs, "[[", "fill")) + fill <- unique(sapply(trs, "[[", "fill")) expect_identical(type, "scatter") expect_identical(fill, "tozeroy") }) From 4a336c65d8cdc0a4f974912a4e93d45e1852e003 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Thu, 16 Apr 2015 11:33:39 -0500 Subject: [PATCH 3/5] Don't use group2NA --- R/trace_generation.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/trace_generation.R b/R/trace_generation.R index 8c07aa2934..673e88d80b 100644 --- a/R/trace_generation.R +++ b/R/trace_generation.R @@ -350,7 +350,7 @@ toBasic <- list( g }, density=function(g) { - g <- group2NA(g, "area") + 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 From 3eea935a7c3f8a422b34a12e72d798407bf6ae8f Mon Sep 17 00:00:00 2001 From: cpsievert Date: Thu, 16 Apr 2015 14:52:03 -0500 Subject: [PATCH 4/5] space --- tests/testthat/test-ggplot-density.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-ggplot-density.R b/tests/testthat/test-ggplot-density.R index 99639ebe60..0d77b191d4 100644 --- a/tests/testthat/test-ggplot-density.R +++ b/tests/testthat/test-ggplot-density.R @@ -1,6 +1,6 @@ context("Probability density") -expect_traces <- function(gg, n.traces, name){ +expect_traces <- function(gg, n.traces, name) { stopifnot(is.ggplot(gg)) stopifnot(is.numeric(n.traces)) save_outputs(gg, paste0("density-", name)) From 308f5d6c27fbf3244259f70e3486448d3488cdc4 Mon Sep 17 00:00:00 2001 From: cpsievert Date: Thu, 16 Apr 2015 15:53:14 -0500 Subject: [PATCH 5/5] Bump version; update NEWS --- DESCRIPTION | 2 +- NEWS | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) 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