Skip to content

Commit b938fda

Browse files
committed
More careful unstacking of y-values & test fixes
1 parent c558586 commit b938fda

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

R/ggplotly.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,19 +259,21 @@ gg2list <- function(p){
259259
ys <- lapply(trace.list, "[[", "y")
260260
xs <- lapply(trace.list, "[[", "x")
261261
x.vals <- unique(unlist(xs))
262-
# if there is more than one y-value (for a particular x value),
262+
# if there are two or more y-values (for a particular x value),
263263
# then modify those y-values so they *add up* to the correct value(s)
264264
for (val in x.vals) {
265265
zs <- lapply(xs, function(x) which(x == val))
266266
ys.given.x <- Map(function(x, y) y[x], zs, ys)
267267
if (length(unlist(ys.given.x)) < 2) next
268268
st <- unStack(unlist(ys.given.x))
269269
lens <- sapply(ys.given.x, length)
270-
trace.seq <- seq_along(zs)
270+
trace.seq <- seq_along(trace.list)
271271
ws <- split(st, rep(trace.seq, lens))
272-
for (tr in trace.seq) {
272+
for (tr in seq_along(ws)) {
273273
idx <- zs[[tr]]
274-
if (length(idx)) trace.list[[tr]]$y[idx] <- ws[[tr]][idx]
274+
replacement <- ws[[tr]]
275+
if (length(idx) > 0 && length(replacement) > 0)
276+
trace.list[[tr]]$y[idx] <- replacement
275277
}
276278
}
277279
}

R/trace_generation.R

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ layer2traces <- function(l, d, misc) {
4747
bargap <- 0
4848
misc$hist <- TRUE
4949
}
50-
51-
# TODO: remove this once we reimplement density as area
52-
if (g$geom == "density") {
53-
bargap <- 0
54-
}
5550

5651
# For non-numeric data on the axes, we should take the values from
5752
# the original data.
@@ -261,6 +256,10 @@ layer2traces <- function(l, d, misc) {
261256
"stack"
262257
} else "group"
263258
}
259+
# TODO: remove this once we reimplement density as area
260+
if (g$geom == "density") {
261+
tr$bargap <- 0
262+
}
264263

265264
traces <- c(traces, list(tr))
266265
}

tests/testthat/test-ggplot-bar.R

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ gg <- ggplot(researchers, aes(country, papers, fill=field))
2525

2626
test_that("position_dodge is translated to barmode=group", {
2727
gg.dodge <- gg + geom_bar(stat="identity", position="dodge")
28-
info <- expect_traces(gg.dodge, 3, "dodge")
28+
info <- expect_traces(gg.dodge, 2, "dodge")
2929
trs <- info$traces
3030
trace.names <- sapply(trs[1:2], "[[", "name")
3131
expect_true(all(c("Math", "Bio") %in% trace.names))
@@ -37,7 +37,7 @@ test_that("position_dodge is translated to barmode=group", {
3737

3838
test_that("position_stack is translated to barmode=stack", {
3939
gg.stack <- gg + geom_bar(stat="identity", position="stack")
40-
info <- expect_traces(gg.stack, 3, "stack")
40+
info <- expect_traces(gg.stack, 2, "stack")
4141
trs <- info$traces
4242
trace.names <- sapply(trs[1:2], "[[", "name")
4343
expect_true(all(c("Math", "Bio") %in% trace.names))
@@ -46,7 +46,7 @@ test_that("position_stack is translated to barmode=stack", {
4646

4747
test_that("position_identity is translated to barmode=overlay", {
4848
gg.identity <- gg + geom_bar(stat="identity", position="identity")
49-
info <- expect_traces(gg.identity, 3, "identity")
49+
info <- expect_traces(gg.identity, 2, "identity")
5050
trs <- info$traces
5151
trace.names <- sapply(trs[1:2], "[[", "name")
5252
expect_true(all(c("Math", "Bio") %in% trace.names))
@@ -58,11 +58,10 @@ test_that("dates work well with bar charts", {
5858
researchers$month <- as.Date(researchers$month)
5959
gd <- ggplot(researchers, aes(month, papers, fill=field)) +
6060
geom_bar(stat="identity")
61-
info <- expect_traces(gd, 3, "dates")
61+
info <- expect_traces(gd, 2, "dates")
6262
trs <- info$traces
6363
expect_identical(info$kwargs$layout$xaxis$type, "date")
64-
expect_identical(trs[[1]]$x[1], "2012-01-01 00:00:00")
65-
expect_identical(trs[[1]]$x[2], "2012-02-01 00:00:00")
64+
expect_identical(trs[[1]]$x, unique(researchers$month))
6665
})
6766

6867
## http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/
@@ -175,16 +174,18 @@ test_that("geom_bar() stacks counts", {
175174
info <- expect_traces(base + geom_bar(), 3, "position-stack")
176175
expect_identical(info$kwargs$layout$barmode, "stack")
177176
trs <- info$traces
178-
test <- colSums(t(sapply(trs, "[[", "y")), na.rm = TRUE)
179-
true <- as.numeric(colSums(with(mtcars, table(cyl, vs))))
177+
# sum of y values for each trace
178+
test <- as.numeric(sort(sapply(trs, function(x) sum(x$y))))
179+
true <- as.numeric(sort(table(mtcars$cyl)))
180180
expect_identical(test, true)
181181
})
182182

183183
test_that("geom_bar(position = 'fill') stacks proportions", {
184184
info <- expect_traces(base + geom_bar(position = "fill"), 3, "position-fill")
185185
expect_identical(info$kwargs$layout$barmode, "stack")
186186
trs <- info$traces
187-
props <- colSums(t(sapply(trs, "[[", "y")), na.rm = TRUE)
188-
expect_identical(props, c(1, 1))
187+
# sum of y-values *conditioned* on a x-value
188+
prop <- sum(sapply(sapply(trs, "[[", "y"), "[", 1))
189+
expect_identical(prop, 1)
189190
})
190191

0 commit comments

Comments
 (0)