1context("geom_dotplot")
2
3skip_on_cran() # This test suite is long-running (on cran) and is skipped
4
5set.seed(111)
6dat <- data_frame(x = rep(LETTERS[1:2], 15), y = rnorm(30), g = rep(LETTERS[3:5], 10))
7
8test_that("dodging works", {
9  p <- ggplot(dat, aes(x = x, y = y, fill = g)) +
10    geom_dotplot(
11      binwidth = 0.2,
12      binaxis = "y",
13      position = "dodge",
14      stackdir = "center"
15    )
16  df <- layer_data(p)
17
18  # Number of levels in the dodged variable
19  ndodge <- 3
20
21  # The amount of space allocated within each dodge group
22  dwidth <- .9 / ndodge
23
24  # This should be the x position for each before dodging
25  xbase <- ceiling(df$group / ndodge)
26
27  # This is the offset from dodging
28  xoffset <- (df$group - 1) %% ndodge - (ndodge - 1) / 2
29  xoffset <- xoffset * dwidth
30
31  # Check actual x locations equal predicted x locations
32  expect_true(all(abs(df$x - (xbase + xoffset)) < 1e-6))
33
34  # Check that xmin and xmax are in the right place
35  expect_true(all(abs(df$xmax - df$x - dwidth/2) < 1e-6))
36  expect_true(all(abs(df$x - df$xmin - dwidth/2) < 1e-6))
37})
38
39test_that("binning works", {
40  bp <- ggplot(dat, aes(y)) +
41    geom_dotplot(binwidth = .4, method = "histodot")
42  x <- layer_data(bp)$x
43
44  # Need ugly hack to make sure mod function doesn't give values like -3.99999
45  # due to floating point error
46  expect_true(all(abs((x - min(x) + 1e-7) %% .4) < 1e-6))
47
48  bp <- ggplot(dat, aes(x = y)) +
49    geom_dotplot(binwidth = .4, method = "dotdensity")
50  x <- layer_data(bp)$x
51
52  # This one doesn't ensure that dotdensity works, but it does check that it's not
53  # doing fixed bin sizes
54  expect_false(all(abs((x - min(x) + 1e-7) %% .4) < 1e-6))
55})
56
57test_that("NA's result in warning from stat_bindot", {
58  set.seed(122)
59  dat <- data_frame(x = rnorm(20))
60  dat$x[c(2,10)] <- NA
61
62  # Need to assign it to a var here so that it doesn't automatically print
63  expect_warning(ggplot_build(ggplot(dat, aes(x)) + geom_dotplot(binwidth = .2)),
64    "Removed 2 rows.*stat_bindot")
65})
66
67test_that("when binning on y-axis, limits depend on the panel", {
68   p <- ggplot(mtcars, aes(factor(cyl), mpg)) +
69        geom_dotplot(binaxis='y')
70
71   b1 <- ggplot_build(p + facet_wrap(~am))
72   b2 <- ggplot_build(p + facet_wrap(~am, scales = "free_y"))
73
74   equal_limits1 <- (b1$layout$panel_params[[1]]$y.range == b1$layout$panel_params[[2]]$y.range)
75   equal_limits2 <- (b2$layout$panel_params[[1]]$y.range == b2$layout$panel_params[[2]]$y.range)
76
77   expect_true(all(equal_limits1))
78   expect_false(all(equal_limits2))
79})
80
81
82# Visual tests ------------------------------------------------------------
83
84test_that("geom_dotplot draws correctly", {
85  set.seed(112)
86  dat <- data_frame(x = rnorm(20), g = rep(LETTERS[1:2], 10))
87
88  # Basic dotplot with binning along x axis
89  expect_doppelganger("basic dotplot with dot-density binning, binwidth = .4",
90    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4)
91  )
92  expect_doppelganger("histodot binning (equal bin spacing)",
93    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, method = "histodot")
94  )
95  expect_doppelganger("dots stacked closer: stackratio=.5, fill=white",
96    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackratio = .5, fill = "white")
97  )
98  expect_doppelganger("larger dots: dotsize=1.5, fill=white",
99    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, dotsize = 1.4, fill = "white")
100  )
101
102  # Stacking methods
103  expect_doppelganger("stack up",
104    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "up")
105  )
106  expect_doppelganger("stack down",
107    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "down")
108  )
109  expect_doppelganger("stack center",
110    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "center")
111  )
112  expect_doppelganger("stack centerwhole",
113    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "centerwhole")
114  )
115
116  # Stacking methods with coord_flip
117  expect_doppelganger("stack up with coord_flip",
118    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "up") + coord_flip()
119  )
120  expect_doppelganger("stack down with coord_flip",
121    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "down") + coord_flip()
122  )
123  expect_doppelganger("stack center with coord_flip",
124    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "center") + coord_flip()
125  )
126  expect_doppelganger("stack centerwhole with coord_flip",
127    ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "centerwhole") + coord_flip()
128  )
129
130  # Binning along x, with groups
131  expect_doppelganger("multiple groups, bins not aligned",
132    ggplot(dat, aes(x, fill = g)) + geom_dotplot(binwidth = .4, alpha = .4)
133  )
134  expect_doppelganger("multiple groups, bins aligned",
135    ggplot(dat, aes(x, fill = g)) + geom_dotplot(binwidth = .4, alpha = .4, binpositions = "all")
136  )
137
138  # Binning along y axis
139  expect_doppelganger("bin along y, stack center",
140    ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "center")
141  )
142  expect_doppelganger("bin along y, stack centerwhole",
143    ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "centerwhole")
144  )
145  expect_doppelganger("bin along y, stack centerwhole, histodot",
146    ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "centerwhole", method = "histodot")
147  )
148
149  # Binning along y, with multiple grouping factors
150  dat2 <- data_frame(x = rep(factor(LETTERS[1:3]), 30), y = rnorm(90), g = rep(factor(LETTERS[1:2]), 45))
151
152  expect_doppelganger("bin y, three x groups, stack centerwhole",
153    ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "centerwhole")
154  )
155  expect_doppelganger("bin y, three x groups, bins aligned across groups",
156    ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "center", binpositions = "all")
157  )
158  expect_doppelganger("bin y, three x groups, bins aligned, coord_flip",
159    ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "center", binpositions = "all") +
160      coord_flip()
161  )
162  expect_doppelganger("bin y, dodged",
163    ggplot(dat2, aes("foo", y, fill = x)) + scale_y_continuous(breaks = seq(-4, 4, .4)) +
164      geom_dotplot(binwidth = .25, position = "dodge", binaxis = "y", stackdir = "center")
165  )
166  expect_doppelganger("bin y, dodged, coord_flip",
167    ggplot(dat2, aes("foo", y, fill = x)) + scale_y_continuous(breaks = seq(-4, 4, .4)) +
168      geom_dotplot(binwidth = .25, position = "dodge", binaxis = "y", stackdir = "center") +
169      coord_flip()
170  )
171  expect_doppelganger("bin y, three x groups, fill and dodge",
172    ggplot(dat2, aes(x, y, fill = g)) + scale_y_continuous(breaks = seq(-4 ,4, .4)) +
173      geom_dotplot(binwidth = .2, position = "dodge", binaxis = "y", stackdir = "center")
174  )
175  expect_doppelganger("bin y, continous x-axis, grouping by x",
176    ggplot(dat2, aes(as.numeric(x), y, group = x)) + geom_dotplot(binwidth = .2, binaxis = "y", stackdir = "center")
177  )
178  expect_doppelganger("bin y, continous x-axis, single x group",
179    ggplot(dat2, aes(as.numeric(x), y)) + geom_dotplot(binwidth = .2, binaxis = "y", stackdir = "center")
180  )
181
182  # border width and size
183  expect_doppelganger(
184    "variable linetype and size specified as aesthetics",
185    ggplot(
186      dat,
187      aes(
188        x,
189        linetype = rep(c("a", "b"), length.out = nrow(dat)),
190        stroke = rep(c(1, 2), length.out = nrow(dat))
191      )
192    ) +
193      geom_dotplot(binwidth = .4, fill = "red", col = "blue") +
194      continuous_scale("stroke", "scaleName", function(x) scales::rescale(x, to = c(1, 6)))
195  )
196
197  # Stacking groups
198  expect_doppelganger("3 stackgroups, dot-density with aligned bins",
199    ggplot(dat2, aes(y, fill = x)) + geom_dotplot(binwidth = .25, stackgroups = TRUE, binpositions = "all", alpha = 0.5)
200  )
201  expect_doppelganger("3 stackgroups, histodot",
202    ggplot(dat2, aes(y, fill = x)) + geom_dotplot(binwidth = .25, stackgroups = TRUE, method = "histodot", alpha = 0.5)
203  )
204  expect_doppelganger("3 stackgroups, bin y, histodot",
205    ggplot(dat2, aes(1, y, fill = x)) + geom_dotplot(binaxis = "y", binwidth = .25, stackgroups = TRUE, method = "histodot", alpha = 0.5)
206  )
207
208  # This one is currently broken but it would be a really rare case, and it
209  # probably requires a really ugly hack to fix
210  expect_doppelganger("bin y, dodging, 3 stackgroups, histodot",
211    ggplot(dat2, aes(x, y, fill = g)) +
212      geom_dotplot(binaxis = "y", binwidth = .25, stackgroups = TRUE, method = "histodot",
213                   alpha = 0.5, stackdir = "centerwhole")
214  )
215  expect_doppelganger("facets, 3 groups, histodot, stackgroups",
216    ggplot(dat2, aes(y, fill = g)) + geom_dotplot(binwidth = .25, stackgroups = TRUE, method = "histodot", alpha = 0.5) +
217      facet_grid(x ~ .)
218  )
219
220  # Missing values
221  dat2 <- dat
222  dat2$x[c(1, 10)] <- NA
223
224  expect_warning(expect_doppelganger("2 NA values, dot-density binning, binwidth = .4",
225    ggplot(dat2, aes(x)) + geom_dotplot(binwidth = .4)
226  ))
227  expect_warning(expect_doppelganger("2 NA values, bin along y, stack center",
228    ggplot(dat2, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "center")
229  ))
230})
231