From 07916a32c5644964cd5d23597de3c731ed6ca334 Mon Sep 17 00:00:00 2001 From: hrbrmstr Date: Tue, 28 Aug 2018 11:11:48 -0400 Subject: [PATCH] tests and docs --- .travis.yml | 5 ++--- NEWS.md | 2 ++ R/geom_wtg.R | 15 +++++++++------ R/worldtilegrid-package.R | 6 ++++++ README.Rmd | 1 + README.md | 3 +++ man/geom_wtg.Rd | 15 +++++++++------ man/worldtilegrid.Rd | 7 +++++++ tests/testthat/test-worldtilegrid.R | 31 ++++++++++++++++++++++++++++--- vignettes/basic-usage.Rmd | 4 ++++ 10 files changed, 71 insertions(+), 18 deletions(-) diff --git a/.travis.yml b/.travis.yml index f93993f..8d139ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,5 @@ +# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r + language: R sudo: false cache: packages - -after_success: -- Rscript -e 'covr::codecov()' diff --git a/NEWS.md b/NEWS.md index 6c03376..37a2a73 100644 --- a/NEWS.md +++ b/NEWS.md @@ -5,6 +5,8 @@ * theme enhancer * basic vignette * figure example in function help page +* passes CRAN checks +* tests 0.1.0 * Initial release diff --git a/R/geom_wtg.R b/R/geom_wtg.R index 507e94d..73ce9ed 100644 --- a/R/geom_wtg.R +++ b/R/geom_wtg.R @@ -57,8 +57,8 @@ #' A `function` will be called with a single argument, #' the plot data. The return value must be a `data.frame.`, and #' will be used as the layer data. -#' @param border_col border color of the state squares, default "`white`" -#' @param border_size thickness of the square state borders +#' @param border_col border color of the country squares, default "`white`" +#' @param border_size thickness of the square country borders #' @param na.rm If `FALSE`, the default, missing values are removed with #' a warning. If `TRUE`, missing values are silently removed. #' @param show.legend logical. Should this layer be included in the legends? @@ -76,17 +76,20 @@ #' to the paired geom/stat. #' @export #' @examples +#' library(ggplot2) +#' library(worldtilegrid) #' set.seed(1) #' data.frame( #' ctry = worldtilegrid::wtg$alpha.3, -#' al = sample(1000, length(worldtilegrid::wtg$alpha.3)) -#' ) -> xdf1 +#' val = sample(1000, length(worldtilegrid::wtg$alpha.3)), +#' stringsAsFactors = FALSE +#' ) -> xdf #' #' ggplot(xdf, aes(country = ctry, fill = val)) + #' geom_wtg() + -#' geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat for labeling +#' geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat to label #' coord_equal() + -#' viridis::scale_fill_viridis() + +#' viridis::scale_fill_viridis(direction = -1) + #' labs(title = "World Tile Grid") + #' theme_minimal() + #' theme_enhance_wtg() diff --git a/R/worldtilegrid-package.R b/R/worldtilegrid-package.R index 31b8d1c..77e9dad 100644 --- a/R/worldtilegrid-package.R +++ b/R/worldtilegrid-package.R @@ -1,5 +1,11 @@ #' A ggplot2 Geom for World Tile Grids #' +#' A "tile grid map" is a cartogram that uses same-sized tiles +#' in approximate, relative positions of each other to represent a world +#' map. The world tile grid relative position reference system used by +#' this 'ggplot2' 'Geom/Stat' was the original work of 'Jon Schwabish' and +#' converted to 'CSV' by 'Maarten Lambrechts'. +#' #' - Ref: #' - Ref: # diff --git a/README.Rmd b/README.Rmd index 1c0216d..9c51d6c 100644 --- a/README.Rmd +++ b/README.Rmd @@ -1,6 +1,7 @@ --- output: rmarkdown::github_document --- +[![Travis-CI Build Status](https://travis-ci.org/hrbrmstr/worldtilegrid.svg?branch=master)](https://travis-ci.org/hrbrmstr/worldtilegrid) # worldtilegrid [WIP] diff --git a/README.md b/README.md index 36b2359..c74e131 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,7 @@ +[![Travis-CI Build +Status](https://travis-ci.org/hrbrmstr/worldtilegrid.svg?branch=master)](https://travis-ci.org/hrbrmstr/worldtilegrid) + # worldtilegrid \[WIP\] A ggplot2 Geom for World Tile Grids diff --git a/man/geom_wtg.Rd b/man/geom_wtg.Rd index eb70db2..a9a154a 100644 --- a/man/geom_wtg.Rd +++ b/man/geom_wtg.Rd @@ -40,9 +40,9 @@ A \code{function} will be called with a single argument, the plot data. The return value must be a \code{data.frame.}, and will be used as the layer data.} -\item{border_col}{border color of the state squares, default "\code{white}"} +\item{border_col}{border color of the country squares, default "\code{white}"} -\item{border_size}{thickness of the square state borders} +\item{border_size}{thickness of the square country borders} \item{na.rm}{If \code{FALSE}, the default, missing values are removed with a warning. If \code{TRUE}, missing values are silently removed.} @@ -113,17 +113,20 @@ A sample of the output from \code{geom_wtg()}: } \examples{ +library(ggplot2) +library(worldtilegrid) set.seed(1) data.frame( ctry = worldtilegrid::wtg$alpha.3, - al = sample(1000, length(worldtilegrid::wtg$alpha.3)) -) -> xdf1 + val = sample(1000, length(worldtilegrid::wtg$alpha.3)), + stringsAsFactors = FALSE +) -> xdf ggplot(xdf, aes(country = ctry, fill = val)) + geom_wtg() + - geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat for labeling + geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat to label coord_equal() + - viridis::scale_fill_viridis() + + viridis::scale_fill_viridis(direction = -1) + labs(title = "World Tile Grid") + theme_minimal() + theme_enhance_wtg() diff --git a/man/worldtilegrid.Rd b/man/worldtilegrid.Rd index 99d5fcf..19b0683 100644 --- a/man/worldtilegrid.Rd +++ b/man/worldtilegrid.Rd @@ -6,6 +6,13 @@ \alias{worldtilegrid-package} \title{A ggplot2 Geom for World Tile Grids} \description{ +A "tile grid map" is a cartogram that uses same-sized tiles +in approximate, relative positions of each other to represent a world +map. The world tile grid relative position reference system used by +this 'ggplot2' 'Geom/Stat' was the original work of 'Jon Schwabish' and +converted to 'CSV' by 'Maarten Lambrechts'. +} +\details{ \itemize{ \item Ref: \url{https://policyviz.com/2017/10/12/the-world-tile-grid-map/} \item Ref: \url{http://www.maartenlambrechts.com/2017/10/22/tutorial-a-worldtilegrid-with-ggplot2.html} diff --git a/tests/testthat/test-worldtilegrid.R b/tests/testthat/test-worldtilegrid.R index 0c22968..48c3f28 100644 --- a/tests/testthat/test-worldtilegrid.R +++ b/tests/testthat/test-worldtilegrid.R @@ -1,6 +1,31 @@ -context("minimal package functionality") -test_that("we can do something", { +context("wtg data joins correctly & ggplot2 data structures are created properly") +test_that("wtg data joins properly", { - #expect_that(some_function(), is_a("data.frame")) + require(ggplot2) + require(worldtilegrid) + + set.seed(1) + + data.frame( + ctry = worldtilegrid::wtg$alpha.3[1:2], + val = sample(1000, length(worldtilegrid::wtg$alpha.3[1:2])), + stringsAsFactors = FALSE + ) -> xdf + + ggplot(xdf, aes(country = ctry, fill = val)) + + geom_wtg() + + geom_text(aes(label = stat(alpha.2)), stat="wtg", size=2) + # re-compute the stat to label + coord_equal() + + viridis::scale_fill_viridis(direction = -1) + + labs(title = "World Tile Grid") + + theme_minimal() + + theme_enhance_wtg() -> gg + + suppressMessages(gb <- ggplot_build(gg)) + + expect_equal(length(gb$data), 2) + expect_identical(dim(gb$data[[1]]), c(192L, 24L)) + expect_identical(dim(gb$data[[2]]), c(192L, 24L)) + expect_identical(gb$data[[1]]$group, gb$data[[2]]$group) }) diff --git a/vignettes/basic-usage.Rmd b/vignettes/basic-usage.Rmd index 5b7da19..b665c6f 100644 --- a/vignettes/basic-usage.Rmd +++ b/vignettes/basic-usage.Rmd @@ -59,6 +59,10 @@ ggplot(xdf, aes(country = ctry, fill = val)) + Note that you can use `theme_enhance_wtg()` with any theme since all it does is cruft removal. +Also note that to add labels to the tiles, you _must_ use `stat_wtg()` so `geom_text()` has access to the `x` and `y` aesthetics as well as the `alpha.2` and `alpha.3` information (if you're going to use one of those for labels). + +Adding labels to the tiles is fraught with peril. Not doing so may be confusing for the vast majority fo the planet who are bereft of geographical knowledge. Doing so may make the colors you used hard to read or (worse) skew the color to an unintended shade. Adding labels to faceted world tile grid maps may make also them unreadable. + ### Example (Only a few countries are in the data set) This is far from ideal since there are so few countries, but can come in handy if you're following a measure over time and some countries either did not exist at that time or had no measure for a given time period.