|
| 1 | +context("ggplot ylim") |
| 2 | + |
| 3 | +## http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/ |
| 4 | + |
| 5 | +df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), |
| 6 | + total_bill = c(14.89, 17.23)) |
| 7 | + |
| 8 | +gg.ylim <- |
| 9 | + ggplot(data=df, aes(x=time, y=total_bill, group=1)) + |
| 10 | + geom_line() + |
| 11 | + geom_point() + |
| 12 | + ylim(0, max(df$total_bill)) + |
| 13 | + xlab("Time of day") + ylab("Total bill") + |
| 14 | + ggtitle("Average bill for 2 people") |
| 15 | + |
| 16 | +expect_traces <- function(gg, n.traces, name){ |
| 17 | + stopifnot(is.ggplot(gg)) |
| 18 | + stopifnot(is.numeric(n.traces)) |
| 19 | + save_outputs(gg, paste0("ylim-", name)) |
| 20 | + L <- gg2list(gg) |
| 21 | + is.trace <- names(L) == "" |
| 22 | + all.traces <- L[is.trace] |
| 23 | + no.data <- sapply(all.traces, function(tr) { |
| 24 | + is.null(tr[["x"]]) && is.null(tr[["y"]]) |
| 25 | + }) |
| 26 | + has.data <- all.traces[!no.data] |
| 27 | + expect_equal(length(has.data), n.traces) |
| 28 | + list(traces=has.data, kwargs=L$kwargs) |
| 29 | +} |
| 30 | + |
| 31 | +test_that("ylim is respected for 1 trace", { |
| 32 | + expect_traces(gg.ylim, 1, "one-trace") |
| 33 | +}) |
0 commit comments