Skip to content

Commit 0448edb

Browse files
committed
better solution to trace order; tests for density and area
1 parent 495ba85 commit 0448edb

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

R/trace_generation.R

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

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

tests/testthat/test-ggplot-area.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,41 @@ test_that("transparency alpha in geom_area is converted", {
2626
})
2727

2828
save_outputs(gg, "area-fillcolor")
29+
30+
# Test that the order of traces is correct
31+
# Expect traces function
32+
expect_traces <- function(gg, n_traces, name) {
33+
stopifnot(is.ggplot(gg))
34+
stopifnot(is.numeric(n_traces))
35+
save_outputs(gg, paste0("coord_fixed-", name))
36+
L <- gg2list(gg)
37+
all_traces <- L$data
38+
no_data <- sapply(all_traces, function(tr) {
39+
is.null(tr[["x"]]) && is.null(tr[["y"]])
40+
})
41+
has_data <- all_traces[!no_data]
42+
expect_equal(length(has_data), n_traces)
43+
list(traces = has_data, layout = L$layout)
44+
}
45+
# Generate ggplot object
46+
library(dplyr)
47+
df <- diamonds %>% group_by(carat, cut) %>%
48+
summarise(n = n()) %>% mutate(freq = n/sum(n))
49+
df$r.cut <- factor(df$cut, levels = rev(levels(df$cut)))
50+
p <- ggplot(data = df, aes(x = carat, y = freq, fill = cut)) +
51+
geom_area()
52+
# Test
53+
test_that("traces are ordered correctly in geom_area", {
54+
info <- expect_traces(p, 5, "geom_area_traces_order")
55+
tr <- info$traces[[1]]
56+
la <- info$layout
57+
expect_identical(tr$type, "scatter")
58+
# check trace order
59+
trace.names <- rev(levels(df$cut))
60+
for (i in 1:5){
61+
expect_identical(info$traces[[i]]$name, trace.names[i])
62+
}
63+
})
64+
# Save output
65+
save_outputs(p, "area_traces_order")
66+

tests/testthat/test-ggplot-density.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,23 @@ test_that("geom_histogram(aes(y = ..density..)) + geom_density() works", {
5454
type <- unique(sapply(trs, "[[", "type"))
5555
expect_identical(sort(type), c("bar", "scatter"))
5656
})
57+
58+
# Check if the traces are in the correct order when position = stack
59+
# Generate ggplot object
60+
p <- ggplot(data = movies, aes(x = rating, fill = mpaa,)) +
61+
geom_density(position = "stack")
62+
# Test
63+
test_that("traces are ordered correctly in geom_density", {
64+
info <- expect_traces(p, 5, "geom_density_traces_order")
65+
tr <- info$traces[[1]]
66+
la <- info$layout
67+
expect_identical(tr$type, "scatter")
68+
# check trace order
69+
trace.names <- rev(levels(movies$mpaa))
70+
for (i in 1:5){
71+
expect_identical(info$traces[[i]]$name, trace.names[i])
72+
}
73+
})
74+
# Save output
75+
save_outputs(p, "density_traces_order")
76+

0 commit comments

Comments
 (0)