Skip to content

Commit e0f0fae

Browse files
committed
Merge a892b5d into 6ff8831
2 parents 6ff8831 + a892b5d commit e0f0fae

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,42 @@ 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("area-", 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 data
46+
df <- aggregate(price ~ cut + carat, data = diamonds, FUN = length)
47+
names(df)[3] <- "n"
48+
temp <- aggregate(n ~ carat, data = df, FUN = sum)
49+
names(temp)[2] <- "sum.n"
50+
df <- merge(x = df, y = temp, all.x = TRUE)
51+
df$freq <- df$n / df$sum.n
52+
# Generate ggplot object
53+
p <- ggplot(data = df, aes(x = carat, y = freq, fill = cut)) +
54+
geom_area()
55+
# Test
56+
test_that("traces are ordered correctly in geom_area", {
57+
info <- expect_traces(p, 5, "traces_order")
58+
tr <- info$traces[[1]]
59+
la <- info$layout
60+
expect_identical(tr$type, "scatter")
61+
# check trace order
62+
trace.names <- rev(levels(df$cut))
63+
for (i in 1:5){
64+
expect_identical(info$traces[[i]]$name, trace.names[i])
65+
}
66+
})
67+

tests/testthat/test-ggplot-density.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,21 @@ 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, "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+

0 commit comments

Comments
 (0)