Skip to content

Toby cookbook lines #196

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 26 commits into from
May 7, 2015
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e47ca93
expect_shape for hline
tdhock Mar 31, 2015
3ef9be5
test scatter facet lines
tdhock Mar 31, 2015
b60d935
only copy global mapping to layer if inherit.aes
tdhock Mar 31, 2015
1bea709
compute ranges for factors
tdhock Apr 1, 2015
8e68d8a
also use ranges for date and datetime variables
tdhock Apr 1, 2015
305ad6c
tests fail
tdhock Apr 1, 2015
7308760
simplify factor handling
tdhock Apr 1, 2015
6c72262
handle dodged factors with aes.name
tdhock Apr 1, 2015
84ab366
special cases
tdhock Apr 1, 2015
6318787
markLegends + boxplot = markSplit
tdhock Apr 1, 2015
4a2dc57
no need for workaround
tdhock Apr 1, 2015
a4d0059
testing showlegend=FALSE for each trace is sufficient
tdhock Apr 1, 2015
4a7f713
set showlegend=FALSE for all but the first trace with a given name
tdhock Apr 2, 2015
56ede93
merge conflict
tdhock Apr 2, 2015
3105143
use more complicated legend merging from master
tdhock Apr 2, 2015
bd8c3f6
test for no legends
tdhock Apr 8, 2015
5fa2434
hide boxplot legends
tdhock Apr 14, 2015
96f0d40
clean up comments
tdhock Apr 14, 2015
f0eb05e
version update
tdhock Apr 15, 2015
8f937e0
Use ||, not |, for logical vector of length 1
cpsievert May 2, 2015
9ab52e1
Resolve merge conflicts with master
cpsievert May 2, 2015
f90928b
merge conflicts with master
cpsievert May 5, 2015
d142570
test fixes for cookbook-lines (due to changes in gg2list())
cpsievert May 5, 2015
ab4ca1c
Fix style (notably, RStudio indentation)
mkcor May 5, 2015
498455d
Update version and docs
mkcor May 5, 2015
4cfedf9
Fix merge conflicts with master
mkcor May 5, 2015
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
Next Next commit
expect_shape for hline
  • Loading branch information
tdhock committed Mar 31, 2015
commit e47ca93c8b7c069003adb4075ccad2564dad1f0b
188 changes: 188 additions & 0 deletions tests/testthat/test-cookbook-lines.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
context("cookbook lines")

expect_traces_shapes <- function(gg, n.traces, n.shapes, name){
stopifnot(is.ggplot(gg))
stopifnot(is.numeric(n.traces))
stopifnot(is.numeric(n.shapes))
save_outputs(gg, paste0("cookbook-lines-", name))
L <- gg2list(gg)
is.trace <- names(L) == ""
all.traces <- L[is.trace]
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)
shapes <- L$kwargs$layout$shapes
expect_equal(length(shapes), n.shapes)
list(traces=has.data,
shapes=shapes,
kwargs=L$kwargs)
}

expect_shape <- function(s, ...){
expected.list <- list(...)
for(key in names(expected.list)){
value <- expected.list[[key]]
expect_identical(s[[key]], value)
}
}

# Some sample data
df <- read.table(header=T, text="
cond result
control 10
treatment 11.5
")

# Basic bar plot
bp <- ggplot(df, aes(x=cond, y=result)) +
geom_bar(position="dodge", stat="identity")

## info <- gg2list(bp)
## info$kwargs$layout$shapes <-
## list(list(xref="paper",
## x0=0,
## x1=1,
## yref="y1",
## y0=10,
## y1=10))
## sendJSON(info)

test_that("geom_bar -> 1 trace", {
info <- expect_traces_shapes(bp, 1, 0, "basic-bar")
})

# Add a horizontal line
temp <- bp + geom_hline(aes(yintercept=12))
test_that("bar + hline = 1 trace, 1 shape", {
info <- expect_traces_shapes(temp, 1, 1, "basic-horizontal-line")
expect_shape(info$shapes[[1]],
xref="paper", x0=0, x1=1,
yref="y1", y0=12, y1=12)
})

# Make the line red and dashed
temp <- bp + geom_hline(aes(yintercept=12), colour="#990000", linetype="dashed")
test_that("bar + red dashed hline", {
info <- expect_traces(temp, 2, "dashed-red-line")
info$traces[[2]]
})

# Draw separate hlines for each bar. First add another column to df
df$hline <- c(9,12)
# cond result hline
# control 10.0 9
# treatment 11.5 12

# Need to re-specify bp, because the data has changed
bp <- ggplot(df, aes(x=cond, y=result)) + geom_bar(position=position_dodge())

# Draw with separate lines for each bar
bp + geom_errorbar(aes(y=hline, ymax=hline, ymin=hline), colour="#AA0000")

# Make the lines narrower
bp + geom_errorbar(width=0.5, aes(y=hline, ymax=hline, ymin=hline), colour="#AA0000")


# Can get the same result, even if we get the hline values from a second data frame
# Define data frame with hline
df.hlines <- data.frame(cond=c("control","treatment"), hline=c(9,12))
# cond hline
# control 9
# treatment 12

# The bar graph are from df, but the lines are from df.hlines
bp + geom_errorbar(data=df.hlines, aes(y=hline, ymax=hline, ymin=hline), colour="#AA0000")

df <- read.table(header=T, text="
cond group result hline
control A 10 9
treatment A 11.5 12
control B 12 9
treatment B 14 12
")

# Define basic bar plot
bp <- ggplot(df, aes(x=cond, y=result, fill=group)) + geom_bar(position=position_dodge())
bp

# The error bars get plotted over one another -- there are four but it looks like two
bp + geom_errorbar(aes(y=hline, ymax=hline, ymin=hline), linetype="dashed")

df <- read.table(header=T, text="
cond group result hline
control A 10 11
treatment A 11.5 12
control B 12 12.5
treatment B 14 15
")

# Define basic bar plot
bp <- ggplot(df, aes(x=cond, y=result, fill=group)) + geom_bar(position=position_dodge())
bp

bp + geom_errorbar(aes(y=hline, ymax=hline, ymin=hline), linetype="dashed", position=position_dodge())

df <- read.table(header=T, text="
cond xval yval
control 11.5 10.8
control 9.3 12.9
control 8.0 9.9
control 11.5 10.1
control 8.6 8.3
control 9.9 9.5
control 8.8 8.7
control 11.7 10.1
control 9.7 9.3
control 9.8 12.0
treatment 10.4 10.6
treatment 12.1 8.6
treatment 11.2 11.0
treatment 10.0 8.8
treatment 12.9 9.5
treatment 9.1 10.0
treatment 13.4 9.6
treatment 11.6 9.8
treatment 11.5 9.8
treatment 12.0 10.6
")

library(ggplot2)

# The basic scatterplot
sp <- ggplot(df, aes(x=xval, y=yval, colour=cond)) + geom_point()


# Add a horizontal line
temp <- sp + geom_hline(aes(yintercept=10))
save_outputs(temp, "lines/hline on scatter", file_prefix="")

# Add a red dashed vertical line
temp <- sp + geom_hline(aes(yintercept=10)) + geom_vline(aes(xintercept=11.5), colour="#BB0000", linetype="dashed")
save_outputs(temp, "lines/hline n vline on scatter", file_prefix="")

# Add colored lines for the mean xval of each group
temp <- sp + geom_hline(aes(yintercept=10)) +
geom_line(stat="vline", xintercept="mean")
save_outputs(temp, "lines/colored lines on scatter", file_prefix="")

# Facet, based on cond
spf <- sp + facet_grid(. ~ cond)
spf

# Draw a horizontal line in all of the facets at the same value
temp <- spf + geom_hline(aes(yintercept=10))
save_outputs(temp, "lines/hline on facets", file_prefix="")

df.vlines <- data.frame(cond=levels(df$cond), xval=c(10,11.5))
# cond xval
# control 10.0
# treatment 11.5

spf + geom_hline(aes(yintercept=10)) +
geom_vline(aes(xintercept=xval), data=df.vlines,
colour="#990000", linetype="dashed")

spf + geom_hline(aes(yintercept=10)) +
geom_line(stat="vline", xintercept="mean")