Skip to content

Commit 4d7227d

Browse files
authored
Merge pull request #34 from BroVic/dev
Update to 0.4.2
2 parents b5c43ff + d1065e3 commit 4d7227d

5 files changed

Lines changed: 140 additions & 45 deletions

File tree

DESCRIPTION

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
Package: naijR
2-
Type: Package
3-
Title: Operations to Ease Data Analyses Specific to Nigeria
4-
Version: 0.4.1.9004
5-
Date: 2022-02-18
6-
Depends:
7-
R (>= 3.6),
8-
methods,
9-
utils
10-
Imports:
11-
RColorBrewer (>= 1.1.2),
12-
lifecycle (>= 0.2.0),
13-
magrittr (>= 1.5),
14-
mapdata (>= 2.3.0),
15-
maps (>= 3.3.0),
16-
rgdal (>= 1.4.4),
17-
rlang (>= 0.4.0)
18-
Suggests:
19-
covr,
20-
here,
21-
knitr,
22-
readxl,
23-
rmarkdown,
24-
sp (>= 1.4.2),
25-
testthat,
26-
usethis
27-
Authors@R: person("Victor", "Ordu",
28-
email = "victorordu@outlook.com",
29-
role = c("aut", "cre"),
30-
comment = c(ORCID = "0000-0003-3716-0668"))
31-
Description: A set of convenience functions as well as geographical/political
32-
data about Nigeria, aimed at simplifying work with data and information that
33-
are specific to the country.
34-
License: GPL-3
35-
LazyData: TRUE
36-
Encoding: UTF-8
37-
RoxygenNote: 7.1.1
38-
VignetteBuilder: knitr
39-
RdMacros: lifecycle
40-
URL: https://brovic.github.io/naijR/
41-
BugReports: https://github.com/BroVic/naijR/issues
1+
Package: naijR
2+
Type: Package
3+
Title: Operations to Ease Data Analyses Specific to Nigeria
4+
Version: 0.4.2
5+
Date: 2022-07-18
6+
Depends:
7+
R (>= 3.6),
8+
grDevices,
9+
methods,
10+
utils
11+
Imports:
12+
RColorBrewer (>= 1.1.2),
13+
lifecycle (>= 0.2.0),
14+
magrittr (>= 1.5),
15+
mapdata (>= 2.3.0),
16+
maps (>= 3.3.0),
17+
rgdal (>= 1.4.4),
18+
rlang (>= 0.4.0)
19+
Suggests:
20+
covr,
21+
here,
22+
knitr,
23+
readxl,
24+
rmarkdown,
25+
sp (>= 1.4.2),
26+
testthat,
27+
usethis
28+
Authors@R: person("Victor", "Ordu",
29+
email = "victorordu@outlook.com",
30+
role = c("aut", "cre"),
31+
comment = c(ORCID = "0000-0003-3716-0668"))
32+
Description: A set of convenience functions as well as geographical/political
33+
data about Nigeria, aimed at simplifying work with data and information that
34+
are specific to the country.
35+
License: GPL-3
36+
LazyData: TRUE
37+
Encoding: UTF-8
38+
RoxygenNote: 7.1.2
39+
VignetteBuilder: knitr
40+
RdMacros: lifecycle
41+
URL: https://brovic.github.io/naijR/
42+
BugReports: https://github.com/BroVic/naijR/issues

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import(mapdata)
2222
import(stats)
2323
importFrom(RColorBrewer,brewer.pal)
2424
importFrom(RColorBrewer,brewer.pal.info)
25+
importFrom(grDevices,colours)
2526
importFrom(grDevices,palette)
2627
importFrom(graphics,legend)
2728
importFrom(graphics,par)

NEWS.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# naijR 0.4.2
2+
* Improved type checking for mapping functionality and better fidelity.
3+
4+
# naijR 0.4.1
5+
* Enable the exclusion of selected States from a choropleth map (#27).
6+
* Cleaner output for `states` and `lgas` objects.
7+
18
# naijR 0.4.0
29
* Introduce the ability to 'manually' fix names of States or LGAs.
310
* Update the documentation with a new vignette.

R/map_nigeria.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,14 @@ map_ng <- function(region = character(),
326326
#' @import mapdata
327327
.getMapData.default <- function(x)
328328
{
329+
if (is.factor(x))
330+
x <- as.character(x)
331+
stopifnot(is.character(x))
329332
if (identical(x, 'Nigeria'))
330333
return("mapdata::worldHires")
331-
if (x %in% .synonymRegions() || is_state(x))
332-
return(.getMapData(states()))
334+
if ((length(x) == 1L && (x %in% .synonymRegions()))
335+
|| all(is_state(x)))
336+
return(.getMapData(states(x)))
333337
.getMapData(lgas(x))
334338
}
335339

@@ -523,6 +527,8 @@ map_ng <- function(region = character(),
523527
df$ind <- as.integer(df$cat)
524528
df$color <- colrange[df$ind]
525529
mapregions <- .getUniqueStateNames(map)
530+
if (nrow(df) < length(mapregions))
531+
mapregions <- mapregions[mapregions %in% df$region]
526532
new.ind <- order(df$region, mapregions)
527533
ord.df <- df[new.ind, ] # This is why a data frame was made
528534
colors <- .reassignColours(map$names, ord.df$region, ord.df$color, ...)
@@ -667,10 +673,15 @@ map_ng <- function(region = character(),
667673
# Reassigns colours to polygons that refer to similar regions i.e. duplicated
668674
# polygon, ensuring that when the choropleth is drawn, the colours are
669675
# properly applied to the respective regions and not recycled.
676+
#' @importFrom grDevices colours
670677
.reassignColours <-
671678
function(names, all.regions, in.colours, excl.region = NULL, excl.col = NULL)
672679
{
673-
stopifnot(is.character(names), all(is_state(all.regions)), .isHexColor(in.colours))
680+
stopifnot({
681+
is.character(names)
682+
all(is_state(all.regions))
683+
.isHexColor(in.colours)
684+
})
674685
out.colours <- new.names <- rep(NA, length(names))
675686
for (i in seq_along(all.regions)) {
676687
regx <- .regexDuplicatedPolygons(all.regions[i])
@@ -683,8 +694,15 @@ map_ng <- function(region = character(),
683694
## the choropleth and should be given an 'off-colour'
684695
if (!is.null(excl.region)) {
685696
off.color <- "grey"
686-
if (!is.null(excl.col))
697+
if (!is.null(excl.col)) {
698+
if (length(excl.col) > 1L)
699+
stop("Non-null 'exclude.fill' must be of length 1L")
700+
if (!is.character(excl.col))
701+
stop("'exclude.fill' must be a string")
702+
if (!excl.col %in% colours())
703+
stop("'exclude.fill' must be a valid colour")
687704
off.color <- excl.col
705+
}
688706
excluded <- match(excl.region, new.names)
689707
out.colours[excluded] <- off.color
690708
}

tests/testthat/test-map_nigeria.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,48 @@ test_that("Choropleth map can be formed with excluded regions", {
510510
),
511511
mapClass
512512
)
513+
514+
expect_error(
515+
map_ng(
516+
data = d,
517+
x = total,
518+
col = colpal,
519+
excluded = excluded.reg,
520+
exclude.fill = c(green, "grey"),
521+
leg.title = "Legend title",
522+
plot = FALSE
523+
),
524+
"Non-null 'exclude.fill' must be of length 1L",
525+
fixed = TRUE
526+
)
527+
528+
expect_error(
529+
map_ng(
530+
data = d,
531+
x = total,
532+
col = colpal,
533+
excluded = excluded.reg,
534+
exclude.fill = "notacolour",
535+
leg.title = "Legend title",
536+
plot = FALSE
537+
),
538+
"'exclude.fill' must be a valid colour",
539+
fixed = TRUE
540+
)
541+
542+
expect_error(
543+
map_ng(
544+
data = d,
545+
x = total,
546+
col = colpal,
547+
excluded = excluded.reg,
548+
exclude.fill = 99L,
549+
leg.title = "Legend title",
550+
plot = FALSE
551+
),
552+
"'exclude.fill' must be a string",
553+
fixed = TRUE
554+
)
513555
})
514556

515557

@@ -519,3 +561,29 @@ test_that("Choropleth map can be formed with excluded regions", {
519561
# expect_error(map_ng(lgas(c("Imo", "Abia"))),
520562
# "LGA-level maps for adjoining States are not yet supported")
521563
# })
564+
565+
566+
567+
test_that(
568+
"Mapping fails when choropleth is plotted with repetitive State levels",
569+
{
570+
data("esoph")
571+
set.seed(87)
572+
ng.esoph <-
573+
transform(esoph, state = sample(states(), nrow(esoph), TRUE))
574+
575+
expect_error(map_ng(data = ng.esoph, x = agegp, plot = FALSE),
576+
"argument lengths differ")
577+
578+
# One record per State (although there are missing states)
579+
ng.esoph <- ng.esoph[!duplicated(ng.esoph$state), ]
580+
581+
expect_s3_class(
582+
map_ng(ng.esoph$state, x = ng.esoph$agegp, plot = FALSE),
583+
"map"
584+
)
585+
expect_s3_class(
586+
map_ng(data = ng.esoph, x = agegp, plot = FALSE),
587+
"map"
588+
)
589+
})

0 commit comments

Comments
 (0)