1context("stat_bin2d") 2 3test_that("binwidth is respected", { 4 df <- data_frame(x = c(1, 1, 1, 2), y = c(1, 1, 1, 2)) 5 base <- ggplot(df, aes(x, y)) + 6 stat_bin2d(geom = "tile", binwidth = 0.25) 7 8 out <- layer_data(base) 9 expect_equal(nrow(out), 2) 10 # Adjust tolerance to account for fuzzy breaks adjustment 11 expect_equal(out$xmin, c(1, 1.75), tolerance = 1e-7) 12 expect_equal(out$xmax, c(1.25, 2), tolerance = 1e-7) 13}) 14 15test_that("breaks override binwidth", { 16 # Test explicitly setting the breaks for x, overriding 17 # the binwidth. 18 integer_breaks <- (0:4) - 0.5 # Will use for x 19 half_breaks <- seq(0, 3.5, 0.5) # Will test against this for y 20 21 df <- data_frame(x = 0:3, y = 0:3) 22 base <- ggplot(df, aes(x, y)) + 23 stat_bin2d( 24 breaks = list(x = integer_breaks, y = NULL), 25 binwidth = c(0.5, 0.5) 26 ) 27 28 out <- layer_data(base) 29 expect_equal(out$xbin, cut(df$x, adjust_breaks(integer_breaks), include.lowest = TRUE, labels = FALSE)) 30 expect_equal(out$ybin, cut(df$y, adjust_breaks(half_breaks), include.lowest = TRUE, labels = FALSE)) 31}) 32 33test_that("breaks are transformed by the scale", { 34 df <- data_frame(x = c(1, 10, 100, 1000), y = 0:3) 35 base <- ggplot(df, aes(x, y)) + 36 stat_bin_2d( 37 breaks = list(x = c(5, 50, 500), y = c(0.5, 1.5, 2.5))) 38 39 out1 <- layer_data(base) 40 out2 <- layer_data(base + scale_x_log10()) 41 expect_equal(out1$x, c(27.5, 275)) 42 expect_equal(out2$x, c(1.19897, 2.19897)) 43}) 44