1context("coord_trans") 2 3test_that("warnings are generated when cord_trans() results in new infinite values", { 4 p <- ggplot(head(diamonds, 20)) + 5 geom_bar(aes(x = cut)) + 6 coord_trans(y = "log10") 7 8 p2 <- ggplot(data_frame(a = c(1, 2, 0), b = c(10, 6, 4)), aes(a, b)) + 9 geom_point() + 10 coord_trans(x = "log") 11 12 expect_warning(ggplot_gtable(ggplot_build(p)), "Transformation introduced infinite values in y-axis") 13 expect_warning(ggplot_gtable(ggplot_build(p2)), "Transformation introduced infinite values in x-axis") 14}) 15 16test_that("no warnings are generated when original data has Inf values, but no new Inf values created from the transformation", { 17 p <- ggplot(data_frame(x = c(-Inf, 2, 0), y = c(Inf, 6, 4)), aes(x, y)) + 18 geom_point() + 19 coord_trans(x = 'identity') 20 21 expect_silent(benchplot(p)) 22}) 23 24test_that("coord_trans() expands axes identically to coord_cartesian()", { 25 p <- ggplot(mpg, aes(class, hwy)) + geom_point() 26 built_cartesian <- ggplot_build(p + coord_cartesian()) 27 built_trans <- ggplot_build(p + coord_trans()) 28 29 cartesian_params <- built_cartesian$layout$panel_params[[1]] 30 trans_params <- built_trans$layout$panel_params[[1]] 31 32 expect_identical(cartesian_params$x.range, trans_params$x.range) 33 expect_identical(cartesian_params$y.range, trans_params$y.range) 34}) 35 36test_that("coord_trans(expand = FALSE) expands axes identically to coord_cartesian(expand = FALSE)", { 37 p <- ggplot(mpg, aes(class, hwy)) + geom_point() 38 built_cartesian <- ggplot_build(p + coord_cartesian(expand = FALSE)) 39 built_trans <- ggplot_build(p + coord_trans(expand = FALSE)) 40 41 cartesian_params <- built_cartesian$layout$panel_params[[1]] 42 trans_params <- built_trans$layout$panel_params[[1]] 43 44 expect_identical(cartesian_params$x.range, trans_params$x.range) 45 expect_identical(cartesian_params$y.range, trans_params$y.range) 46}) 47 48test_that("coord_trans(y = 'log10') expands the x axis identically to scale_y_log10()", { 49 p <- ggplot(mpg, aes(class, hwy)) + geom_point() 50 built_cartesian <- ggplot_build(p + scale_y_log10()) 51 built_trans <- ggplot_build(p + coord_trans(y = "log10")) 52 53 cartesian_params <- built_cartesian$layout$panel_params[[1]] 54 trans_params <- built_trans$layout$panel_params[[1]] 55 56 expect_identical(cartesian_params$y.range, trans_params$y.range) 57}) 58 59test_that("coord_trans() expands axes outside the domain of the axis trans", { 60 # sqrt_trans() has a lower limit of 0 61 df <- data_frame(x = 1, y = c(0, 1, 2)) 62 p <- ggplot(df, aes(x, y)) + geom_point() 63 built_cartesian <- ggplot_build(p + scale_y_sqrt()) 64 built_trans <- ggplot_build(p + coord_trans(y = "sqrt")) 65 66 cartesian_params <- built_cartesian$layout$panel_params[[1]] 67 trans_params <- built_trans$layout$panel_params[[1]] 68 69 expect_identical(cartesian_params$y.range, trans_params$y.range) 70}) 71 72test_that("coord_trans() works with the reverse transformation", { 73 df <- data_frame(x = c("1-one", "2-two", "3-three"), y = c(20, 30, 40)) 74 75 p <- ggplot(df, aes(x, y)) + geom_point() 76 built_cartesian <- ggplot_build(p + scale_y_reverse()) 77 built_trans <- ggplot_build(p + coord_trans(y = "reverse")) 78 79 cartesian_params <- built_cartesian$layout$panel_params[[1]] 80 trans_params <- built_trans$layout$panel_params[[1]] 81 82 expect_identical(cartesian_params$y.range, trans_params$y.range) 83}) 84 85test_that("coord_trans() can reverse discrete axes", { 86 df <- data_frame(x = c("1-one", "2-two", "3-three"), y = c(20, 30, 40)) 87 88 p <- ggplot(df, aes(x, y)) + geom_point() 89 built_cartesian <- ggplot_build(p) 90 built_trans <- ggplot_build(p + coord_trans(x = "reverse")) 91 92 cartesian_params <- built_cartesian$layout$panel_params[[1]] 93 trans_params <- built_trans$layout$panel_params[[1]] 94 95 expect_identical(cartesian_params$x.range, -rev(trans_params$x.range)) 96}) 97 98test_that("basic coord_trans() plot displays both continuous and discrete axes", { 99 expect_doppelganger( 100 "basic coord_trans() plot", 101 ggplot(mpg, aes(class, hwy)) + 102 geom_point() + 103 coord_trans(y = "log10") 104 ) 105}) 106 107test_that("second axes display in coord_trans()", { 108 expect_doppelganger( 109 "sec_axis with coord_trans()", 110 ggplot(mpg, aes(cty, hwy)) + 111 geom_point() + 112 scale_y_continuous( 113 sec.axis = sec_axis( 114 trans = ~log2(.), 115 breaks = c(3.5, 4, 4.5, 5, 5.5), 116 name = "log2(hwy)" 117 ), 118 breaks = 2^c(3.5, 4, 4.5, 5, 5.5) 119 ) + 120 scale_x_continuous(sec.axis = dup_axis()) + 121 coord_trans(y = "log2") 122 ) 123}) 124