Skip to content

Correcting the order of traces #227

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 9 commits into from
Dec 10, 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,6 +1,6 @@
Package: plotly
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
Version: 2.0.9
Version: 2.0.10
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
email = "[email protected]"),
person("Chris", "Parmer", role = c("aut", "cph"),
Expand Down
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2.0.10 -- 10 Dec 2015

Fix #225

2.0.9 -- 10 Dec 2015

Fix #333
Expand Down
10 changes: 10 additions & 0 deletions R/trace_generation.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,16 @@ layer2traces <- function(l, d, misc) {
}
})

# reverse the traces in the following cases:
# geom_area
# geom_density with position = stack
if (g$geom == "area" |
g$geom == "density" & l$position$.super$objname == "stack"){
traces <- rev(traces)
} else{
traces
}

ord <- order(sort.val)
no.sort <- traces[ord]
for(tr.i in seq_along(no.sort)){
Expand Down
41 changes: 41 additions & 0 deletions tests/testthat/test-ggplot-area.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,44 @@ test_that("transparency alpha in geom_area is converted", {
expect_identical(L$data[[1]]$line$color, "transparent")
expect_identical(L$data[[1]]$fillcolor, "rgba(51,51,51,0.4)")
})

save_outputs(gg, "area-fillcolor")

# Test that the order of traces is correct
# Expect traces function
expect_traces <- function(gg, n_traces, name) {
stopifnot(is.ggplot(gg))
stopifnot(is.numeric(n_traces))
save_outputs(gg, paste0("area-", name))
L <- gg2list(gg)
all_traces <- L$data
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, layout = L$layout)
}
# Generate data
df <- aggregate(price ~ cut + carat, data = diamonds, FUN = length)
names(df)[3] <- "n"
temp <- aggregate(n ~ carat, data = df, FUN = sum)
names(temp)[2] <- "sum.n"
df <- merge(x = df, y = temp, all.x = TRUE)
df$freq <- df$n / df$sum.n
# Generate ggplot object
p <- ggplot(data = df, aes(x = carat, y = freq, fill = cut)) +
geom_area()
# Test
test_that("traces are ordered correctly in geom_area", {
info <- expect_traces(p, 5, "traces_order")
tr <- info$traces[[1]]
la <- info$layout
expect_identical(tr$type, "scatter")
# check trace order
trace.names <- rev(levels(df$cut))
for (i in 1:5){
expect_identical(info$traces[[i]]$name, trace.names[i])
}
})

22 changes: 17 additions & 5 deletions tests/testthat/test-ggplot-density.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ expect_traces <- function(gg, n.traces, name) {
})
has.data <- all.traces[!no.data]
expect_equal(length(has.data), n.traces)
list(traces=has.data, layout=L$layout)
list(data=has.data, layout=L$layout)
}

# Draw a probability density estimation using geom_density
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]]
tr <- info$data[[1]]
expect_identical(tr$type, "scatter")
expect_identical(tr$fill, "tozeroy")
expect_identical(tr$fillcolor, "rgba(51,51,51,0)")
Expand All @@ -27,7 +27,7 @@ test_that("geom_density() is translated to area chart", {
test_that("geom_density() respects fill aesthetic", {
gg <- base + geom_density(aes(fill=factor(vs)), alpha = 0.3)
info <- expect_traces(gg, 2, "fill")
trs <- info$traces
trs <- info$data
type <- unique(sapply(trs, "[[", "type"))
fill <- unique(sapply(trs, "[[", "fill"))
expect_identical(type, "scatter")
Expand All @@ -36,7 +36,7 @@ test_that("geom_density() respects fill aesthetic", {

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

test_that("geom_histogram(aes(y = ..density..)) + geom_density() works", {
info <- expect_traces(g, 2, "histogram")
trs <- info$traces
trs <- info$data
type <- unique(sapply(trs, "[[", "type"))
expect_identical(sort(type), c("bar", "scatter"))
})

# Check if the traces are in the correct order when position = stack
# Generate ggplot object
p <- ggplot(data = mtcars, aes(x = mpg, fill = factor(cyl))) +
geom_density(position = "stack")

test_that("traces are ordered correctly in geom_density", {
info <- expect_traces(p, 3, "traces_order")
nms <- sapply(info$data, "[[", "name")
expect_identical(nms, c("8", "6", "4"))
})