Skip to content

Commit baa31e8

Browse files
committed
test for error bars first or last
1 parent 3abd3f6 commit baa31e8

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

tests/testthat/test-mean-error-bars.R

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
context("means and error bars")
2+
3+
library(ggplot2)
4+
5+
one.line.df <-
6+
data.frame(
7+
x = c(1, 2, 3, 4),
8+
y = c(2, 1, 3, 4),
9+
array = c(0.1, 0.2, 0.1, 0.1),
10+
arrayminus = c(0.2, 0.4, 1, 0.2))
11+
one.line.json <- list(
12+
list(
13+
x = c(1, 2, 3, 4),
14+
y = c(2, 1, 3, 4),
15+
error_y = list(
16+
type = "data",
17+
symmetric = FALSE,
18+
array = c(0.1, 0.2, 0.1, 0.1),
19+
arrayminus = c(0.2, 0.4, 1, 0.2)
20+
),
21+
type = "scatter"
22+
)
23+
)
24+
25+
test_that("asymmetric error bars, geom_errorbar last", {
26+
one.line.gg <- ggplot(one.line.df, aes(x, y))+
27+
geom_line()+
28+
geom_point()+
29+
geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array))
30+
generated.json <- gg2list(one.line.gg)
31+
is.trace <- names(generated.json) == ""
32+
traces <- generated.json[is.trace]
33+
expect_identical(length(traces), 1L)
34+
})
35+
36+
test_that("asymmetric error bars, geom_errorbar first", {
37+
one.line.gg <- ggplot(one.line.df, aes(x, y))+
38+
geom_errorbar(aes(ymin=y-arrayminus, ymax=y+array))+
39+
geom_line()+
40+
geom_point()
41+
generated.json <- gg2list(one.line.gg)
42+
is.trace <- names(generated.json) == ""
43+
traces <- generated.json[is.trace]
44+
expect_identical(length(traces), 1L)
45+
})
46+
47+
48+
## from https://github.com/chriddyp/ggplot2-plotly-cookbook/blob/a45f2c70b7adf484e0b0eb8810a1e59e018adbb8/means_and_error_bars.R#L162-L191
49+
df <- ToothGrowth
50+
51+
## Summarizes data.
52+
## Gives count, mean, standard deviation, standard error of the mean, and confidence interval (default 95%).
53+
## data: a data frame.
54+
## measurevar: the name of a column that contains the variable to be summariezed
55+
## groupvars: a vector containing names of columns that contain grouping variables
56+
## na.rm: a boolean that indicates whether to ignore NA's
57+
## conf.interval: the percent range of the confidence interval (default is 95%)
58+
summarySE <- function(data=NULL, measurevar, groupvars=NULL, na.rm=FALSE,
59+
conf.interval=.95, .drop=TRUE) {
60+
require(plyr)
61+
length2 <- function (x, na.rm=FALSE) {
62+
if (na.rm) sum(!is.na(x))
63+
else length(x)
64+
}
65+
datac <- ddply(data, groupvars, .drop=.drop,
66+
.fun = function(xx, col) {
67+
c(N = length2(xx[[col]], na.rm=na.rm),
68+
mean = mean (xx[[col]], na.rm=na.rm),
69+
sd = sd (xx[[col]], na.rm=na.rm)
70+
)
71+
},
72+
measurevar
73+
)
74+
datac <- rename(datac, c("mean" = measurevar))
75+
datac$se <- datac$sd / sqrt(datac$N) # Calculate standard error of the mean
76+
ciMult <- qt(conf.interval/2 + .5, datac$N-1)
77+
datac$ci <- datac$se * ciMult
78+
return(datac)
79+
}
80+
dfc <- summarySE(df, measurevar="len", groupvars=c("supp","dose"))
81+
bad <- ggplot(dfc, aes(x=dose, y=len, colour=supp)) +
82+
geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1) +
83+
geom_line() +
84+
geom_point()
85+
86+
good <- ggplot(dfc, aes(x=dose, y=len, colour=supp)) +
87+
geom_line() +
88+
geom_errorbar(aes(ymin=len-se, ymax=len+se), width=.1) +
89+
geom_point()
90+
good.json <- gg2list(good)
91+
py$ggplotly(good, kwargs=list(fileopt='overwrite', filename='R-Cookbook/means-and-error-bars/basic-error-bars'))
92+
93+
## The conversion from geom_errorbar to plotly error bars is not
94+
## straightforward.
95+
96+
## we need to make a plotly trace with list(error_y=list(array=c(1, 2,
97+
## 3), symmetric=FALSE, arrayminus=c(4.45, 3.91, 2.65))) -- and maybe
98+
## traceref=2 and tracerefminus=1, which are indices of other traces
99+
## that contain the same error bar data in the master trace list.
100+
101+
## https://plot.ly/~tdhock/184 has some plotly error bars.
102+
103+
good.json[[1]]$error_y <-
104+
list(array=c(1, 2, 3), arrayminus=c(4, 5, 6),
105+
symmetric=FALSE,
106+
traceref=1, tracerefminus=1,
107+
visible=TRUE,
108+
type="data")
109+

0 commit comments

Comments
 (0)