Skip to content

Commit a4a9a1d

Browse files
committed
Merge pull request #227 from ropensci/baobao-trace_order
Correcting the order of traces
2 parents d7a340c + 27b9439 commit a4a9a1d

File tree

5 files changed

+73
-6
lines changed

5 files changed

+73
-6
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 2.0.9
3+
Version: 2.0.10
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.0.10 -- 10 Dec 2015
2+
3+
Fix #225
4+
15
2.0.9 -- 10 Dec 2015
26

37
Fix #333

R/trace_generation.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,16 @@ layer2traces <- function(l, d, misc) {
278278
}
279279
})
280280

281+
# reverse the traces in the following cases:
282+
# geom_area
283+
# geom_density with position = stack
284+
if (g$geom == "area" |
285+
g$geom == "density" & l$position$.super$objname == "stack"){
286+
traces <- rev(traces)
287+
} else{
288+
traces
289+
}
290+
281291
ord <- order(sort.val)
282292
no.sort <- traces[ord]
283293
for(tr.i in seq_along(no.sort)){

tests/testthat/test-ggplot-area.R

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,44 @@ test_that("transparency alpha in geom_area is converted", {
2222
expect_identical(L$data[[1]]$line$color, "transparent")
2323
expect_identical(L$data[[1]]$fillcolor, "rgba(51,51,51,0.4)")
2424
})
25+
26+
save_outputs(gg, "area-fillcolor")
27+
28+
# Test that the order of traces is correct
29+
# Expect traces function
30+
expect_traces <- function(gg, n_traces, name) {
31+
stopifnot(is.ggplot(gg))
32+
stopifnot(is.numeric(n_traces))
33+
save_outputs(gg, paste0("area-", name))
34+
L <- gg2list(gg)
35+
all_traces <- L$data
36+
no_data <- sapply(all_traces, function(tr) {
37+
is.null(tr[["x"]]) && is.null(tr[["y"]])
38+
})
39+
has_data <- all_traces[!no_data]
40+
expect_equal(length(has_data), n_traces)
41+
list(traces = has_data, layout = L$layout)
42+
}
43+
# Generate data
44+
df <- aggregate(price ~ cut + carat, data = diamonds, FUN = length)
45+
names(df)[3] <- "n"
46+
temp <- aggregate(n ~ carat, data = df, FUN = sum)
47+
names(temp)[2] <- "sum.n"
48+
df <- merge(x = df, y = temp, all.x = TRUE)
49+
df$freq <- df$n / df$sum.n
50+
# Generate ggplot object
51+
p <- ggplot(data = df, aes(x = carat, y = freq, fill = cut)) +
52+
geom_area()
53+
# Test
54+
test_that("traces are ordered correctly in geom_area", {
55+
info <- expect_traces(p, 5, "traces_order")
56+
tr <- info$traces[[1]]
57+
la <- info$layout
58+
expect_identical(tr$type, "scatter")
59+
# check trace order
60+
trace.names <- rev(levels(df$cut))
61+
for (i in 1:5){
62+
expect_identical(info$traces[[i]]$name, trace.names[i])
63+
}
64+
})
65+

tests/testthat/test-ggplot-density.R

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ expect_traces <- function(gg, n.traces, name) {
1010
})
1111
has.data <- all.traces[!no.data]
1212
expect_equal(length(has.data), n.traces)
13-
list(traces=has.data, layout=L$layout)
13+
list(data=has.data, layout=L$layout)
1414
}
1515

1616
# Draw a probability density estimation using geom_density
1717
base <- ggplot(mtcars, aes(wt))
1818

1919
test_that("geom_density() is translated to area chart", {
2020
info <- expect_traces(base + geom_density(), 1, "simple")
21-
tr <- info$traces[[1]]
21+
tr <- info$data[[1]]
2222
expect_identical(tr$type, "scatter")
2323
expect_identical(tr$fill, "tozeroy")
2424
expect_identical(tr$fillcolor, "rgba(51,51,51,0)")
@@ -27,7 +27,7 @@ test_that("geom_density() is translated to area chart", {
2727
test_that("geom_density() respects fill aesthetic", {
2828
gg <- base + geom_density(aes(fill=factor(vs)), alpha = 0.3)
2929
info <- expect_traces(gg, 2, "fill")
30-
trs <- info$traces
30+
trs <- info$data
3131
type <- unique(sapply(trs, "[[", "type"))
3232
fill <- unique(sapply(trs, "[[", "fill"))
3333
expect_identical(type, "scatter")
@@ -36,7 +36,7 @@ test_that("geom_density() respects fill aesthetic", {
3636

3737
test_that("geom_density() respects colour aesthetic", {
3838
info <- expect_traces(base + geom_density(aes(colour=factor(vs))), 2, "color")
39-
trs <- info$traces
39+
trs <- info$data
4040
type <- unique(sapply(trs, "[[", "type"))
4141
fill <- unique(sapply(trs, "[[", "fill"))
4242
expect_identical(type, "scatter")
@@ -49,7 +49,19 @@ g <- base +
4949

5050
test_that("geom_histogram(aes(y = ..density..)) + geom_density() works", {
5151
info <- expect_traces(g, 2, "histogram")
52-
trs <- info$traces
52+
trs <- info$data
5353
type <- unique(sapply(trs, "[[", "type"))
5454
expect_identical(sort(type), c("bar", "scatter"))
5555
})
56+
57+
# Check if the traces are in the correct order when position = stack
58+
# Generate ggplot object
59+
p <- ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) +
60+
geom_density(position = "stack")
61+
62+
test_that("traces are ordered correctly in geom_density", {
63+
info <- expect_traces(p, 3, "traces_order")
64+
nms <- sapply(info$data, "[[", "name")
65+
expect_identical(nms, c("8", "6", "4"))
66+
})
67+

0 commit comments

Comments
 (0)