1context("Fortify")
2
3test_that("spatial polygons have correct ordering", {
4  skip_if_not_installed("sp")
5
6  make_square <- function(x = 0, y = 0, height = 1, width = 1){
7    delx <- width/2
8    dely <- height/2
9    sp::Polygon(matrix(c(x + delx, x - delx,x - delx,x + delx,x + delx ,
10        y - dely,y - dely,y + dely,y + dely,y - dely), ncol = 2))
11  }
12
13  make_hole <- function(x = 0, y = 0, height = .5, width = .5){
14    p <- make_square(x = x, y = y, height = height, width = width)
15    p@hole <- TRUE
16    p
17  }
18
19  fake_data <- data_frame(ids = 1:5, region = c(1,1,2,3,4))
20  rownames(fake_data) <- 1:5
21  polys <- list(sp::Polygons(list(make_square(), make_hole()), 1),
22                sp::Polygons(list(make_square(1,0), make_square(2, 0)), 2),
23                sp::Polygons(list(make_square(1,1)), 3),
24                sp::Polygons(list(make_square(0,1)), 4),
25                sp::Polygons(list(make_square(0,3)), 5))
26
27  polys_sp <- sp::SpatialPolygons(polys)
28  fake_sp <- sp::SpatialPolygonsDataFrame(polys_sp, fake_data)
29
30  # now reorder regions
31  polys2 <- rev(polys)
32  polys2_sp <- sp::SpatialPolygons(polys2)
33  fake_sp2 <- sp::SpatialPolygonsDataFrame(polys2_sp, fake_data)
34  fake_sp2_fortified <- fortify(fake_sp2)
35
36  expect_equivalent(fortify(fake_sp), fake_sp2_fortified[order(fake_sp2_fortified$id, fake_sp2_fortified$order), ])
37})
38
39test_that("fortify.default proves a helpful error with class uneval", {
40  expect_error(
41    ggplot(aes(x = x)),
42    regexp = paste(
43      "`data` must be a data frame, or other object coercible by (.+)",
44      "Did you accidentally pass `aes\\(\\)` to the `data` argument?",
45      sep = "\\n"
46    )
47  )
48})
49