Skip to content

geom_density() as filled area chart #202

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 6 commits into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"),
person("Scott", "Chamberlain", role = "aut",
Expand Down
6 changes: 5 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
@@ -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

Expand Down
20 changes: 3 additions & 17 deletions R/trace_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
50 changes: 40 additions & 10 deletions tests/testthat/test-ggplot-density.R
Original file line number Diff line number Diff line change
@@ -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")
})