diff --git a/R/VizRobinson.R b/R/VizRobinson.R index f3f0ff170b2931f145153665ca4accaff609f066..cb8f2509ba70b906e97d967ea53e856989e6d3d0 100644 --- a/R/VizRobinson.R +++ b/R/VizRobinson.R @@ -119,7 +119,7 @@ #' @importFrom rlang .data #'@export VizRobinson <- function(data, lon, lat, lon_dim = NULL, lat_dim = NULL, - target_proj = "ESRI:54030", drawleg = 'bar', style = 'point', + target_proj = NULL, drawleg = 'bar', style = 'point', dots = NULL, mask = NULL, brks = NULL, cols = NULL, bar_limits = NULL, triangle_ends = NULL, col_inf = NULL, col_sup = NULL, colNA = NULL, color_fun = ClimPalette(), bar_extra_margin = rep(0, 4), vertical = TRUE, @@ -176,18 +176,21 @@ VizRobinson <- function(data, lon, lat, lon_dim = NULL, lat_dim = NULL, # original_proj: it can only be regular grid now original_proj <- st_crs(4326) - # tartget_proj + # target_proj if (is.null(target_proj)) { - stop("Parameter 'target_proj' cannot be NULL.") - } else { - target_proj_tmp <- st_crs(target_proj) - if (is.na(target_proj_tmp)) { - warning(paste0("Try ESRI code: ESRI:", target_proj)) - target_proj <- st_crs(paste0("ESRI:", target_proj)) + if (packageVersion("sf") < "1.0.10") { + target_proj <- 54030 } else { - target_proj <- target_proj_tmp + target_proj <- "ESRI:54030" } } + target_proj_tmp <- st_crs(target_proj) + if (is.na(target_proj_tmp)) { + warning(paste0("Try ESRI code: ESRI:", target_proj)) + target_proj <- st_crs(paste0("ESRI:", target_proj)) + } else { + target_proj <- target_proj_tmp + } # drawleg if (!drawleg %in% c('bar', 'ggplot2', FALSE)) { @@ -446,7 +449,7 @@ VizRobinson <- function(data, lon, lat, lon_dim = NULL, lat_dim = NULL, datapoly <- datapoly %>% dplyr::group_by(.data$id) %>% dplyr::summarise() %>% #NOTE: VERY SLOW if plot global - dplyr::mutate(value = .data[[values[order(values$id), ]$value]]) %>% + dplyr::mutate(value = values[order(values$id), ]$value) %>% st_cast("POLYGON") %>% st_convex_hull() # maintain outer polygen (no bowtie shape) } diff --git a/tests/testthat.R b/tests/testthat.R new file mode 100644 index 0000000000000000000000000000000000000000..86040617c9c71c524fc09a9793d156038169f36d --- /dev/null +++ b/tests/testthat.R @@ -0,0 +1,3 @@ +library(testthat) + +test_check("esviz") \ No newline at end of file diff --git a/tests/testthat/_snaps/PlotRobinson/PlotRobinson_1a.png b/tests/testthat/_snaps/PlotRobinson/PlotRobinson_1a.png deleted file mode 100644 index bb12bad6df3b712142afcbd07398bbc4c8ec86ee..0000000000000000000000000000000000000000 Binary files a/tests/testthat/_snaps/PlotRobinson/PlotRobinson_1a.png and /dev/null differ diff --git a/tests/testthat/_snaps/PlotRobinson/PlotRobinson_2a.png b/tests/testthat/_snaps/PlotRobinson/PlotRobinson_2a.png deleted file mode 100644 index 811ceb7dfe2b862c9e2772cdff963c02d2fd4084..0000000000000000000000000000000000000000 Binary files a/tests/testthat/_snaps/PlotRobinson/PlotRobinson_2a.png and /dev/null differ diff --git a/tests/testthat/_snaps/VizRobinson/VizRobinson_1a.png b/tests/testthat/_snaps/VizRobinson/VizRobinson_1a.png new file mode 100644 index 0000000000000000000000000000000000000000..3f89b8dcacc8a0feabdd0e2b090b151454107759 Binary files /dev/null and b/tests/testthat/_snaps/VizRobinson/VizRobinson_1a.png differ diff --git a/tests/testthat/_snaps/VizRobinson/VizRobinson_1b.png b/tests/testthat/_snaps/VizRobinson/VizRobinson_1b.png new file mode 100644 index 0000000000000000000000000000000000000000..276ca5b0bff3f72bba25d255eb6f7914a98a341f Binary files /dev/null and b/tests/testthat/_snaps/VizRobinson/VizRobinson_1b.png differ diff --git a/tests/testthat/_snaps/VizRobinson/VizRobinson_1c.png b/tests/testthat/_snaps/VizRobinson/VizRobinson_1c.png new file mode 100644 index 0000000000000000000000000000000000000000..6cf2a949267f172bac352603bd7b664a05a80dd5 Binary files /dev/null and b/tests/testthat/_snaps/VizRobinson/VizRobinson_1c.png differ diff --git a/tests/testthat/_snaps/VizRobinson/VizRobinson_2a.png b/tests/testthat/_snaps/VizRobinson/VizRobinson_2a.png new file mode 100644 index 0000000000000000000000000000000000000000..54910aa9c6b18e628b4b8f15a966b712ba0fbfbd Binary files /dev/null and b/tests/testthat/_snaps/VizRobinson/VizRobinson_2a.png differ diff --git a/tests/testthat/test-PlotRobinson.R b/tests/testthat/test-PlotRobinson.R deleted file mode 100644 index c16d1bc599bad572b9cc591f415c8f369d646e19..0000000000000000000000000000000000000000 --- a/tests/testthat/test-PlotRobinson.R +++ /dev/null @@ -1,157 +0,0 @@ -#=============================================================== -# NOTE: The figures are generated with the following environment: -#- On workstation -#- R/4.1.2-foss-2015a-bare -#- GDAL/2.2.1-foss-2015a -#- PROJ/4.8.0-foss-2015a -#- GEOS/3.7.2-foss-2015a-Python-2.7.9 -#=============================================================== - -# data1: global -set.seed(19) -data1 <- array(rep(seq(-10, 10, length.out = 181), 360) + rnorm(360), - dim = c(sdate = 1, lat = 181, lon = 360)) -mask1 <- drop(data1) -mask1[which(mask1 > 9 & mask1 < -9)] <- 0 -mask1[which(mask1 != 0)] <- 1 -dots1 <- mask1 -lon1_1 <- -180:179 -lon1_2 <- 0:359 -lat1 <- -90:90 - -data1_NA <- data1; data1_NA[10000:15000] <- NA - -# data2: Europe -data2 <- data1[,21:61, 161:221] -lon2 <- lon1_1[161:221] -lat2 <- 70:30 -dots2 <- array(t(dots1[21:61, 161:221]), dim = c(lon = length(lon2), lat = length(lat2))) - -#-------------------------------------------------------------------- - -test_that("1. Input checks", { - -# data -expect_error( - PlotRobinson(array(data1*2, dim = c(sdate = 2, lat = 181, lon = 360))), - "Parameter 'data' must have two dimensions." -) -# lon, lon_dim -tmp <- data1; dim(tmp) <- c(sdate = 1, latt = 181, lonn = 360) -expect_error( - PlotRobinson(tmp), - "Cannot find known longitude name in data dimension. Please define parameter 'lon_dim'." -) -expect_error( - PlotRobinson(data1, lon = 1:10), - "Length of parameter 'lon' should match longitude dimension in 'data'." -) -# target_proj -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, target_proj = NULL), - "Parameter 'target_proj' cannot be NULL." -) -# legend -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, legend = 1), - "Parameter 'legend' must be NULL, ggplot2 or s2dv." -) -# style -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, style = 'line'), - "Parameter 'style' must be 'point' or 'polygon'." -) -# dots -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, dots = c(1, 0)), - "Parameter 'dots' must have two dimensions named as longitude and latitude dimensions in 'data'." -) -tmp <- dots1; dim(tmp) <- c(sdate = 1, lat = 360, lon = 181) -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, dots = tmp), - "Parameter 'dots' must have the same dimensions as 'data'." -) -tmp <- dots1; tmp[1] <- NA -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, dots = tmp), - "Parameter 'dots' must have only TRUE/FALSE or 0/1." -) -# colNA -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, colNA = 'fair'), - "Parameter 'colNA' must be a valid colour identifier." -) -# toptitle -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, toptitle = 1:10), - "Parameter 'toptitle' must be a character string." -) -# caption -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, caption = 1:10), - "Parameter 'caption' must be a character string." -) -# crop_coastlines -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, crop_coastlines = c(x1 = 1, x2 = 10, y1 = -10, y2 = 10)), - "Parameter 'crop_coastlines' needs to have names 'latmax', 'latmin', 'lonmax', 'lonmin'." -) -# point_size -expect_error( - PlotRobinson(data1, lon = lon1_1, lat = lat1, point_size = 'small'), - "Parameter 'point_size' must be a number." -) - -}) - -#-------------------------------------------------------- -# PlotRobinson -save_PlotRobinson <- function(...) { - path <- tempfile(fileext = ".png") - do.call(PlotRobinson, list(..., fileout = path)) - path -} - -test_that("2. data1: Global", { - -# up triangle end only -expect_snapshot_file( - save_PlotRobinson(data1, lon = lon1_1, lat = lat1, dots = dots1, brks = seq(-12.6, 12.6, 1), vertical = F, toptitle = 'PlotRobinson', caption = 'unit test: 1a'), - name = 'PlotRobinson_1a.png' -) -# NAs -expect_snapshot_file( - save_PlotRobinson(data1_NA, lon = lon1_1, lat = lat1, dots = dots1, brks = seq(-13, 13, 2), toptitle = 'PlotRobinson', caption = 'unit test: 1b', bar_extra_margin = c(5, 0, 5, 0)), - name = 'PlotRobinson_1b.png' -) - -expect_snapshot_file( - save_PlotRobinson(data1, lon = lon1_1, lat = lat1, mask = mask1, brks = seq(-11, 11, 2), color_fun = ClimPalette('purpleorange'), colNA = 'red', legend = 'ggplot2', toptitle = 'PlotRobinson', caption = 'unit test: 1c'), - name = 'PlotRobinson_1c.png' -) - - -}) - - -test_that("3. data2: Europe", { - -expect_snapshot_file( - save_PlotRobinson(data2, lon = lon2, lat = lat2, dots = dots2, target_proj = 102014, - brks = seq(-10, 0, 2), cols = c("#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), col_inf = "#FEF0D9", - toptitle = 'PlotRobinson', caption = 'unit test: 1a\n projection: Lambert Europe (ESRI:102014)', point_size = 1.1, title_size = 12, - width = 6, vertical = F, bar_extra_margin = c(0.5, 4, 0.5, 4), crop_coastlines = c(latmin = 27, latmax = 70, lonmin = -20, lonmax = 40)), - name = 'PlotRobinson_2a.png' -) - -#polygon -#expect_snapshot_file( -# save_PlotRobinson(data2, lon = lon2, lat = lat2, dots = dots2, target_proj = 102014, -# brks = seq(-10, 0, 2), cols = c("#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), col_inf = "#FEF0D9", -# toptitle = 'PlotRobinson', caption = 'unit test: 1a\n projection: Lambert Europe (ESRI:102014)', point_size = 1.1, title_size = 12, -# width = 6, vertical = F, bar_extra_margin = c(0.5, 4, 0.5, 4), crop_coastlines = c(latmin = 27, latmax = 70, lonmin = -20, lonmax = 40), style = 'polygon'), -# name = 'PlotRobinson_2a.png' -#) - - -}) diff --git a/tests/testthat/test-VizRobinson.R b/tests/testthat/test-VizRobinson.R new file mode 100644 index 0000000000000000000000000000000000000000..fd2463e9a5af7c1dcd8a438f3b96cbbe250ac4dc --- /dev/null +++ b/tests/testthat/test-VizRobinson.R @@ -0,0 +1,143 @@ +#=============================================================== +# NOTE: The figures are generated with the following environment: +#- On workstation +#- R/4.1.2-foss-2015a-bare +#- GDAL/2.2.1-foss-2015a +#- PROJ/4.8.0-foss-2015a +#- GEOS/3.7.2-foss-2015a-Python-2.7.9 +#=============================================================== + +# data1: global +set.seed(19) +data1 <- array(rep(seq(-10, 10, length.out = 181), 360) + rnorm(360), + dim = c(sdate = 1, lat = 181, lon = 360)) +mask1 <- drop(data1) +mask1[which(mask1 > 9 & mask1 < -9)] <- 0 +mask1[which(mask1 != 0)] <- 1 +dots1 <- mask1 +lon1_1 <- -180:179 +lon1_2 <- 0:359 +lat1 <- -90:90 + +data1_NA <- data1; data1_NA[10000:15000] <- NA + +# data2: Europe +data2 <- data1[,21:61, 161:221] +lon2 <- lon1_1[161:221] +lat2 <- 70:30 +dots2 <- array(t(dots1[21:61, 161:221]), dim = c(lon = length(lon2), lat = length(lat2))) + +#-------------------------------------------------------------------- + +test_that("1. Input checks", { + + # data + expect_error( + VizRobinson(array(data1*2, dim = c(sdate = 2, lat = 181, lon = 360))), + "Parameter 'data' must have two dimensions." + ) + # lon, lon_dim + tmp <- data1; dim(tmp) <- c(sdate = 1, latt = 181, lonn = 360) + expect_error( + VizRobinson(tmp), + "Cannot find known longitude name in data dimension. Please define parameter 'lon_dim'." + ) + expect_error( + VizRobinson(data1, lon = 1:10), + "Length of parameter 'lon' should match longitude dimension in 'data'." + ) + # style + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, style = 'line'), + "Parameter 'style' must be 'point' or 'polygon'." + ) + # dots + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, dots = c(1, 0)), + "Parameter 'dots' must have two dimensions named as longitude and latitude dimensions in 'data'." + ) + tmp <- dots1; dim(tmp) <- c(sdate = 1, lat = 360, lon = 181) + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, dots = tmp), + "Parameter 'dots' must have the same dimensions as 'data'." + ) + tmp <- dots1; tmp[1] <- NA + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, dots = tmp), + "Parameter 'dots' must have only TRUE/FALSE or 0/1." + ) + # colNA + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, colNA = 'fair'), + "Parameter 'colNA' must be a valid colour identifier." + ) + # toptitle + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, toptitle = 1:10), + "Parameter 'toptitle' must be a character string." + ) + # caption + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, caption = 1:10), + "Parameter 'caption' must be a character string." + ) + # crop_coastlines + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, crop_coastlines = c(x1 = 1, x2 = 10, y1 = -10, y2 = 10)), + "Parameter 'crop_coastlines' needs to have names 'latmax', 'latmin', 'lonmax', 'lonmin'." + ) + # point_size + expect_error( + VizRobinson(data1, lon = lon1_1, lat = lat1, point_size = 'small'), + "Parameter 'point_size' must be a number." + ) + +}) + +#-------------------------------------------------------- +# VizRobinson +save_VizRobinson <- function(...) { + path <- tempfile(fileext = ".png") + do.call(VizRobinson, list(..., fileout = path)) + path +} + +test_that("2. data1: Global", { + # up triangle end only + expect_snapshot_file( + save_VizRobinson(data1, lon = lon1_1, lat = lat1, dots = dots1, brks = seq(-12.6, 12.6, 1), vertical = F, toptitle = 'VizRobinson', caption = 'unit test: 1a'), + name = 'VizRobinson_1a.png' + ) + # NAs + expect_snapshot_file( + save_VizRobinson(data1_NA, lon = lon1_1, lat = lat1, dots = dots1, brks = seq(-13, 13, 2), toptitle = 'VizRobinson', caption = 'unit test: 1b', bar_extra_margin = c(5, 0, 5, 0)), + name = 'VizRobinson_1b.png' + ) + + expect_snapshot_file( + save_VizRobinson(data1, lon = lon1_1, lat = lat1, mask = mask1, brks = seq(-11, 11, 2), color_fun = ClimPalette('purpleorange'), colNA = 'red', toptitle = 'VizRobinson', caption = 'unit test: 1c'), + name = 'VizRobinson_1c.png' + ) +}) + + +test_that("3. data2: Europe", { + + expect_snapshot_file( + save_VizRobinson(data2, lon = lon2, lat = lat2, dots = dots2, target_proj = 102014, + brks = seq(-10, 0, 2), cols = c("#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), col_inf = "#FEF0D9", + toptitle = 'VizRobinson', caption = 'unit test: 1a\n projection: Lambert Europe (ESRI:102014)', point_size = 1.1, title_size = 12, + width = 6, vertical = F, bar_extra_margin = c(0.5, 4, 0.5, 4), crop_coastlines = c(latmin = 27, latmax = 70, lonmin = -20, lonmax = 40)), + name = 'VizRobinson_2a.png' + ) + + # polygon + # NOTE: This test returns error in WS (R 4.1.2 (2021-11-01)) and works on HUB (4.2.1 (2022-06-23)) + # expect_snapshot_file( + # save_VizRobinson(data2, lon = lon2, lat = lat2, dots = dots2, target_proj = 102014, + # brks = seq(-10, 0, 2), cols = c("#FDD49E", "#FDBB84", "#FC8D59", "#E34A33", "#B30000"), col_inf = "#FEF0D9", + # toptitle = 'VizRobinson', caption = 'unit test: 1a\n projection: Lambert Europe (ESRI:102014)', point_size = 1.1, title_size = 12, + # width = 6, vertical = F, bar_extra_margin = c(0.5, 4, 0.5, 4), crop_coastlines = c(latmin = 27, latmax = 70, lonmin = -20, lonmax = 40), style = 'polygon'), + # name = 'VizRobinson_2b.png' + # ) +})