Skip to content

Commit fafbff9

Browse files
committed
Merge pull request #202 from ropensci/carson-density
geom_density() as filled area chart
2 parents d958976 + 308f5d6 commit fafbff9

File tree

4 files changed

+49
-29
lines changed

4 files changed

+49
-29
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: plotly
22
Type: Package
33
Title: Interactive, publication-quality graphs online.
4-
Version: 0.5.28
4+
Version: 0.5.29
55
Authors@R: c(person("Chris", "Parmer", role = c("aut", "cre"),
66
email = "[email protected]"),
77
person("Scott", "Chamberlain", role = "aut",

NEWS

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
0.5.27 -- 15 April 2015
1+
0.5.29 -- 16 April 2015
2+
3+
geom_density() as filled area chart #202
4+
5+
0.5.28 -- 15 April 2015
26

37
Let ggplot handle histogram binning. Fix #198
48

R/trace_generation.R

+3-17
Original file line numberDiff line numberDiff line change
@@ -251,10 +251,6 @@ layer2traces <- function(l, d, misc) {
251251
"stack"
252252
} else "group"
253253
}
254-
# TODO: remove this once we reimplement density as area
255-
if (g$geom == "density") {
256-
tr$bargap <- 0
257-
}
258254

259255
traces <- c(traces, list(tr))
260256
}
@@ -354,10 +350,9 @@ toBasic <- list(
354350
g
355351
},
356352
density=function(g) {
357-
g$params$xstart <- min(g$data$x)
358-
g$params$xend <- max(g$data$x)
359-
g$params$binwidth <- (max(g$data$x) - min(g$data$x))/30
360-
g$data <- g$prestats.data
353+
g$geom <- "area"
354+
if (is.null(g$data$fill) && is.null(g$params$alpha)) g$params$alpha <- 0
355+
if (is.null(g$data$colour)) g$params$colour <- "black"
361356
g
362357
},
363358
density2d=function(g) {
@@ -628,15 +623,6 @@ geom2trace <- list(
628623
L$contours=list(coloring="lines")
629624
L
630625
},
631-
density=function(data, params) {
632-
L <- list(x=data$x,
633-
name=params$name,
634-
text=data$text,
635-
marker=list(color=toRGB(params$fill)),
636-
type="histogram",
637-
autobinx=TRUE,
638-
histnorm="probability density")
639-
},
640626
density2d=function(data, params) {
641627
L <- list(x=data$x,
642628
y=data$y,

tests/testthat/test-ggplot-density.R

+40-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
11
context("Probability density")
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("density-", name))
7+
L <- gg2list(gg)
8+
is.trace <- names(L) == ""
9+
all.traces <- L[is.trace]
10+
no.data <- sapply(all.traces, function(tr) {
11+
is.null(tr[["x"]]) && is.null(tr[["y"]])
12+
})
13+
has.data <- all.traces[!no.data]
14+
expect_equal(length(has.data), n.traces)
15+
list(traces=has.data, kwargs=L$kwargs)
16+
}
17+
318
# Draw a probability density estimation using geom_density
4-
m <- ggplot(movies) + geom_density(aes(rating))
5-
L <- gg2list(m)
6-
7-
test_that("geom_density is translated to a normalized histogram", {
8-
expect_equal(length(L), 2)
9-
expect_identical(L[[1]]$type, "histogram")
10-
expect_true(L[[1]]$autobinx)
11-
expect_identical(L[[1]]$histnorm, "probability density")
12-
expect_equal(L[[2]]$layout$bargap, 0)
19+
base <- ggplot(mtcars, aes(wt))
20+
21+
test_that("geom_density() is translated to area chart", {
22+
info <- expect_traces(base + geom_density(), 1, "simple")
23+
tr <- info$traces[[1]]
24+
expect_identical(tr$type, "scatter")
25+
expect_identical(tr$fill, "tozeroy")
26+
expect_identical(tr$fillcolor, "rgba(51,51,51,0)")
1327
})
1428

15-
save_outputs(m, "density")
29+
test_that("geom_density() respects fill aesthetic", {
30+
info <- expect_traces(base + geom_density(aes(fill=factor(vs))), 2, "fill")
31+
trs <- info$traces
32+
type <- unique(sapply(trs, "[[", "type"))
33+
fill <- unique(sapply(trs, "[[", "fill"))
34+
expect_identical(type, "scatter")
35+
expect_identical(fill, "tozeroy")
36+
})
37+
38+
test_that("geom_density() respects colour aesthetic", {
39+
info <- expect_traces(base + geom_density(aes(colour=factor(vs))), 2, "color")
40+
trs <- info$traces
41+
type <- unique(sapply(trs, "[[", "type"))
42+
fill <- unique(sapply(trs, "[[", "fill"))
43+
expect_identical(type, "scatter")
44+
expect_identical(fill, "tozeroy")
45+
})

0 commit comments

Comments
 (0)