Browse Source

tests and docs

master
boB Rudis 6 years ago
parent
commit
07916a32c5
No known key found for this signature in database GPG Key ID: 1D7529BE14E2BBA9
  1. 5
      .travis.yml
  2. 2
      NEWS.md
  3. 15
      R/geom_wtg.R
  4. 6
      R/worldtilegrid-package.R
  5. 1
      README.Rmd
  6. 3
      README.md
  7. 15
      man/geom_wtg.Rd
  8. 7
      man/worldtilegrid.Rd
  9. 31
      tests/testthat/test-worldtilegrid.R
  10. 4
      vignettes/basic-usage.Rmd

5
.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()'

2
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

15
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()

6
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: <https://policyviz.com/2017/10/12/the-world-tile-grid-map/>
#' - Ref: <http://www.maartenlambrechts.com/2017/10/22/tutorial-a-worldtilegrid-with-ggplot2.html>
#

1
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]

3
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

15
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()

7
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}

31
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)
})

4
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.

Loading…
Cancel
Save