forked from plotly/plotly.R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-ggplot-bar.R
More file actions
186 lines (165 loc) · 6.7 KB
/
test-ggplot-bar.R
File metadata and controls
186 lines (165 loc) · 6.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
context("bar")
expect_traces <- function(gg, n.traces, name) {
stopifnot(is.ggplot(gg))
stopifnot(is.numeric(n.traces))
L <- save_outputs(gg, paste0("bar-", name))
all.traces <- L$data
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)
list(data = has.data, layout = L$layout)
}
researchers <- data.frame(
country = c("Canada", "Canada", "Germany", "USA"),
name = c("Warren", "Andreanne", "Stefan", "Toby"),
papers = c(23, 14, 37, 20),
field = c("Math", "Bio", "Bio", "Math")
)
gg <- ggplot(researchers, aes(country, papers, fill = field))
test_that("position_dodge is translated to barmode=group", {
gg.dodge <- gg + geom_bar(stat = "identity", position = "dodge")
info <- expect_traces(gg.dodge, 2, "dodge")
trs <- info$data
trace.names <- sapply(trs[1:2], "[[", "name")
expect_true(all(c("Math", "Bio") %in% trace.names))
expect_identical(info$layout$barmode, "group")
# Check x values
expect_identical(as.character(trs[[1]]$x), c("Canada", "Germany"))
expect_identical(as.character(trs[[2]]$x), c("Canada", "USA"))
})
test_that("position_stack is translated to barmode=stack", {
gg.stack <- gg + geom_bar(stat="identity", position="stack")
info <- expect_traces(gg.stack, 2, "stack")
trs <- info$data
trace.names <- sapply(trs[1:2], "[[", "name")
expect_true(all(c("Math", "Bio") %in% trace.names))
expect_identical(info$layout$barmode, "stack")
})
test_that("position_identity is translated to barmode=stack", {
gg.identity <- gg + geom_bar(stat="identity", position="identity")
info <- expect_traces(gg.identity, 2, "identity")
trs <- info$data
trace.names <- sapply(trs[1:2], "[[", "name")
expect_true(all(c("Math", "Bio") %in% trace.names))
expect_identical(info$layout$barmode, "stack")
})
test_that("dates work well with bar charts", {
researchers$month <- c("2012-01-01", "2012-01-01", "2012-02-01", "2012-02-01")
researchers$month <- as.Date(researchers$month)
gd <- ggplot(researchers, aes(month, papers, fill = field)) +
geom_bar(stat = "identity")
info <- expect_traces(gd, 2, "dates")
trs <- info$data
expect_identical(info$layout$xaxis$type, "date")
# plotly likes time in milliseconds
t <- as.numeric(unique(researchers$month)) * 24 * 60 * 60 * 1000
expect_equal(trs[[1]]$x, t)
})
## http://www.cookbook-r.com/Graphs/Bar_and_line_graphs_%28ggplot2%29/
df <- data.frame(time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(14.89, 17.23))
test_that("Very basic bar graph", {
gg <- ggplot(data = df, aes(x = time, y = total_bill)) +
geom_bar(stat = "identity")
info <- expect_traces(gg, 1, "nocolor")
tr <- info$data[[1]]
expect_identical(tr$type, "bar")
expect_identical(tr$bargap, "default")
expect_identical(tr$x, df$time)
expect_identical(tr$y, df$total_bill)
expect_null(info$layout$annotations)
expect_false(info$layout$showlegend)
})
test_that("Map the time of day to different fill colors", {
gg <- ggplot(data = df, aes(x = time, y = total_bill, fill = time)) +
geom_bar(stat = "identity")
info <- expect_traces(gg, 2, "color")
# is the color of the two bars the same?
same <- identical(info$data[[1]]$marker$color, info$data[[2]]$marker$color)
expect_true(!same)
expect_match(info$layout$annotations[[1]]$text, "time")
expect_true(info$layout$showlegend)
})
test_that("Add a black outline", {
gg <- ggplot(data = df, aes(x = time, y = total_bill, fill = time)) +
geom_bar(colour = "black", stat = "identity")
info <- expect_traces(gg, 2, "black-outline")
for(tr in info$data){
expect_true(is.character(tr$marker$color))
expect_identical(tr$marker$line$color, toRGB("black"))
expect_true(tr$showlegend)
}
expect_match(info$layout$annotations[[1]]$text, "time")
expect_true(info$layout$showlegend)
})
test_that("guides(fill=FALSE) hides fill legend", {
gg <- ggplot(data = df, aes(x = time, y = total_bill, fill = time)) +
geom_bar(colour = "black", stat = "identity") +
guides(fill = FALSE)
info <- expect_traces(gg, 2, "aes-fill-guides-fill-FALSE")
for(tr in info$data){
expect_true(is.character(tr$marker$color))
expect_identical(tr$marker$line$color, toRGB("black"))
}
expect_null(info$layout$annotations)
expect_false(info$layout$showlegend)
})
test_that('guides(fill="none") hides fill legend', {
gg <- ggplot(data = df, aes(x = time, y = total_bill, fill = time)) +
geom_bar(colour = "black", stat = "identity") +
guides(fill = "none")
info <- expect_traces(gg, 2, "aes-fill-guides-fill-none")
expect_null(info$layout$annotations)
expect_false(info$layout$showlegend)
})
test_that('guides(colour="none") does not affect fill legend', {
gg <- ggplot(data = df, aes(x = time, y = total_bill, fill = time)) +
geom_bar(color = "black", stat = "identity") +
guides(colour = "none")
info <- expect_traces(gg, 2, "aes-fill-guides-color-none")
expect_match(info$layout$annotations[[1]]$text, "time")
expect_true(info$layout$showlegend)
})
test_that("guides(fill=FALSE) does not affect colour legend", {
gg <- ggplot(data = df, aes(x = time, y = total_bill, colour = time)) +
geom_bar(fill = "grey", stat = "identity") +
guides(fill = FALSE)
info <- expect_traces(gg, 2, "aes-colour-guides-fill-FALSE")
for(tr in info$data){
expect_identical(tr$marker$color, toRGB("grey"))
expect_true(is.character(tr$marker$line$color))
expect_true(tr$showlegend)
}
expect_match(info$layout$annotations[[1]]$text, "time")
expect_true(info$layout$showlegend)
})
base <- ggplot(mtcars, aes(factor(vs), fill = factor(cyl)))
test_that("geom_bar() stacks counts", {
info <- expect_traces(base + geom_bar(), 3, "position-stack")
expect_identical(info$layout$barmode, "stack")
trs <- info$data
# sum of y values for each trace
test <- as.numeric(sort(sapply(trs, function(x) sum(x$y))))
true <- as.numeric(sort(table(mtcars$cyl)))
expect_identical(test, true)
})
test_that("geom_bar(position = 'fill') stacks proportions", {
info <- expect_traces(base + geom_bar(position = "fill"), 3, "position-fill")
expect_identical(info$layout$barmode, "stack")
trs <- info$data
# sum of y-values *conditioned* on a x-value
prop <- sum(sapply(sapply(trs, "[[", "y"), "[", 1))
expect_identical(prop, 1)
})
d <- head(diamonds, 50)
gbar <- ggplot(d, aes(cut, price)) + geom_bar(stat = "identity")
test_that("For a given x value, if multiple y exist, sum them. ", {
info <- expect_traces(gbar, 1, "category-names")
expect_identical(info$data[[1]]$type, "bar")
y <- with(d, tapply(price, cut, sum))
# make sure order of counts match
y <- y[info$data[[1]]$x]
expect_equal(info$data[[1]]$y, as.numeric(y))
})