Skip to content

Commit

Permalink
Fixes for submission
Browse files Browse the repository at this point in the history
  • Loading branch information
hafen committed Nov 29, 2023
1 parent 58c62ed commit 0046861
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 141 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Expand Up @@ -16,3 +16,4 @@ geofacet.code-workspace
^pkgdown$
^revdep$
^\.github$
^CRAN-SUBMISSION$
2 changes: 2 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Expand Up @@ -26,4 +26,6 @@ jobs:
extra-packages: any::rcmdcheck
needs: check

- run: Rscript -e 'install.packages("rnaturalearthhires", repos = "https://ropensci.r-universe.dev", type = "source")'

- uses: r-lib/actions/check-r-package@v2
4 changes: 2 additions & 2 deletions R/grid_auto.R
Expand Up @@ -215,11 +215,11 @@ plot_geo_raw <- function(x, label = "name") {

ggplot2::ggplot(tmp) +
ggplot2::geom_polygon(aes(x = long, y = lat, group = group),
fill = "lightgray", color = "white", size = 0.3) +
fill = "lightgray", color = "white", linewidth = 0.3) +
ggrepel::geom_text_repel(aes(xcentroid, ycentroid, label = label_col),
data = tmpl, min.segment.length = 0) +
ggplot2::coord_equal() +
ggplot2::guides(fill = FALSE) +
ggplot2::guides(fill = "none") +
ggplot2::theme_void()
}

Expand Down
4 changes: 0 additions & 4 deletions cran-comments.md
Expand Up @@ -47,10 +47,6 @@ Found the following (possibly) invalid URLs:
inst/doc/geofacet.html
Status: 403
Message: Forbidden
URL: https://countymapsofwashington.com/aapics/washingstate.gif
From: man/grids.Rd
Status: 403
Message: Forbidden
For content that is 'Moved Permanently', please change http to https,
add trailing slashes, or replace the old by the new URL.

Expand Down
34 changes: 20 additions & 14 deletions tests/testthat/test-auto.R
Expand Up @@ -5,22 +5,28 @@ test_that("auto examples work", {
testthat::skip_if_not_installed("rnaturalearthhires")

# auto grid using a name to identify the country
grd <- grid_auto("brazil", seed = 1234)
grid_preview(grd, label = "name")
grd$name2 <- gsub(" ", "\n", grd$name)
grid_preview(grd, label = "name2", label_raw = "name")
# open the result up in the grid designer for further refinement
grid_design(grd, label = "name")
expect_no_error({
grd <- grid_auto("uruguay", seed = 1234)
grid_preview(grd, label = "name")
grd$name2 <- gsub(" ", "\n", grd$name)
grid_preview(grd, label = "name2", label_raw = "name")
# open the result up in the grid designer for further refinement
# grid_design(grd, label = "name")
})

# using a custom file (can be GeoJSON or shapefile)
ff <- system.file("extdata", "bay_counties.geojson", package = "geogrid")
bay_shp <- sf::st_read(ff)
grd <- grid_auto(bay_shp, seed = 1) # names are inferred
grid_preview(grd, label = "name_county")
grid_design(grd, label = "code_fipsstco")
expect_no_error({
ff <- system.file("extdata", "bay_counties.geojson", package = "geogrid")
bay_shp <- sf::st_read(ff)
grd <- grid_auto(bay_shp, seed = 1) # names are inferred
grid_preview(grd, label = "name_county")
# grid_design(grd, label = "code_fipsstco")
})

# explicitly specify the names and codes variables to use
grd <- grid_auto(bay_shp, seed = 1, names = "county", codes = "fipsstco")
grid_preview(grd, label = "name_county")
grid_preview(grd, label = "code_fipsstco")
expect_no_error({
grd <- grid_auto(bay_shp, seed = 1, names = "county", codes = "fipsstco")
grid_preview(grd, label = "name_county")
# grid_preview(grd, label = "code_fipsstco")
})
})
2 changes: 1 addition & 1 deletion tests/testthat/test-break.R
Expand Up @@ -59,7 +59,7 @@ test_that("things break in an expected way", {
y = "Population density per square km") +
theme_bw()
},
"will be ignored")
"does not exist")

my_grid <- us_state_grid1

Expand Down
269 changes: 150 additions & 119 deletions tests/testthat/test-examples.R
Expand Up @@ -3,154 +3,185 @@ context("examples")
library(ggplot2)

test_that("examples work", {

# barchart of state rankings in various categories
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state) +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state) +
theme_bw()
print(p)
})

# use an alternative US state grid and place
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state, grid = "us_state_grid2") +
theme(panel.spacing = unit(0.1, "lines"))
print(p)

grid_preview(us_state_grid2)
grid_preview(eu_grid1, label = "name")
expect_no_error({
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state, grid = "us_state_grid2") +
theme(panel.spacing = unit(0.1, "lines"))
print(p)
})

expect_no_error(grid_preview(us_state_grid2))
expect_no_error(grid_preview(eu_grid1, label = "name"))

# custom grid (move Hawaii over more)
my_grid <- us_state_grid2
my_grid$col <- my_grid$col + 2
my_grid$col[my_grid$code == "HI"] <- 1
grid_preview(my_grid)
expect_no_error({
my_grid <- us_state_grid2
my_grid$col <- my_grid$col + 2
my_grid$col[my_grid$code == "HI"] <- 1
grid_preview(my_grid)
})

# test to make sure we can have empty columns (since Hawaii is moved over)
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state, grid = my_grid)
print(p)
expect_no_error({
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state, grid = my_grid)
print(p)
})

# use a free x-axis (not a good idea but just to show it works)
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state, scales = "free_x") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(state_ranks, aes(variable, rank, fill = variable)) +
geom_col() +
coord_flip() +
facet_geo(~ state, scales = "free_x") +
theme_bw()
print(p)
})

# plot unemployment rate time series for each state
p <- ggplot(state_unemp, aes(year, rate)) +
geom_line() +
facet_geo(~ state) +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("Unemployment Rate (%)")
print(p)
expect_no_error({
p <- ggplot(state_unemp, aes(year, rate)) +
geom_line() +
facet_geo(~ state) +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("Unemployment Rate (%)")
print(p)
})

# plot the 2016 unemployment rate
p <- ggplot(subset(state_unemp, year == 2016), aes(factor(year), rate)) +
geom_col(fill = "steelblue") +
facet_geo(~ state) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
ylab("Unemployment Rate (%)")
print(p)
expect_no_error({
p <- ggplot(subset(state_unemp, year == 2016), aes(factor(year), rate)) +
geom_col(fill = "steelblue") +
facet_geo(~ state) +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
ylab("Unemployment Rate (%)")
print(p)
})

# plot European Union GDP
p <- ggplot(eu_gdp, aes(year, gdp_pc)) +
geom_line(color = "steelblue") +
geom_hline(yintercept = 100, linetype = 2) +
facet_geo(~ name, grid = "eu_grid1") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("GDP Per Capita") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(eu_gdp, aes(year, gdp_pc)) +
geom_line(color = "steelblue") +
geom_hline(yintercept = 100, linetype = 2) +
facet_geo(~ name, grid = "eu_grid1") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("GDP Per Capita") +
theme_bw()
print(p)
})

# use a free y-axis to look at just change
p <- ggplot(eu_gdp, aes(year, gdp_pc)) +
geom_line(color = "steelblue") +
facet_geo(~ name, grid = "eu_grid1", scales = "free_y") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("GDP Per Capita in Relation to EU Index (100)") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(eu_gdp, aes(year, gdp_pc)) +
geom_line(color = "steelblue") +
facet_geo(~ name, grid = "eu_grid1", scales = "free_y") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
ylab("GDP Per Capita in Relation to EU Index (100)") +
theme_bw()
print(p)
})

# plot European Union annual # of resettled persons
p <- ggplot(eu_imm, aes(year, persons)) +
geom_line() +
facet_geo(~ name, grid = "eu_grid1") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
scale_y_sqrt(minor_breaks = NULL) +
ylab("# Resettled Persons") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(eu_imm, aes(year, persons)) +
geom_line() +
facet_geo(~ name, grid = "eu_grid1") +
scale_x_continuous(labels = function(x) paste0("'", substr(x, 3, 4))) +
scale_y_sqrt(minor_breaks = NULL) +
ylab("# Resettled Persons") +
theme_bw()
print(p)
})

# plot just for 2016
p <- ggplot(subset(eu_imm, year == 2016), aes(factor(year), persons)) +
geom_col(fill = "steelblue") +
geom_text(aes(factor(year), 3000, label = persons), color = "gray") +
facet_geo(~ name, grid = "eu_grid1") +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
ylab("# Resettled Persons in 2016") +
xlab("Year") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(subset(eu_imm, year == 2016), aes(factor(year), persons)) +
geom_col(fill = "steelblue") +
geom_text(aes(factor(year), 3000, label = persons), color = "gray") +
facet_geo(~ name, grid = "eu_grid1") +
theme(
axis.title.x = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()) +
ylab("# Resettled Persons in 2016") +
xlab("Year") +
theme_bw()
print(p)
})

# plot Australian population
p <- ggplot(aus_pop, aes(age_group, pop / 1e6, fill = age_group)) +
geom_col() +
facet_geo(~ code, grid = "aus_grid1") +
coord_flip() +
labs(
title = "Australian Population Breakdown",
caption = "Data Source: ABS Labour Force Survey, 12 month average",
y = "Population [Millions]") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(aus_pop, aes(age_group, pop / 1e6, fill = age_group)) +
geom_col() +
facet_geo(~ code, grid = "aus_grid1") +
coord_flip() +
labs(
title = "Australian Population Breakdown",
caption = "Data Source: ABS Labour Force Survey, 12 month average",
y = "Population [Millions]") +
theme_bw()
print(p)
})

# South Africa population density by province
p <- ggplot(sa_pop_dens, aes(factor(year), density, fill = factor(year))) +
geom_col() +
facet_geo(~ code, grid = "sa_prov_grid1") +
labs(title = "South Africa population density by province",
caption = "Data Source: Statistics SA Census",
y = "Population density per square km") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(sa_pop_dens, aes(factor(year), density, fill = factor(year))) +
geom_col() +
facet_geo(~ code, grid = "sa_prov_grid1") +
labs(title = "South Africa population density by province",
caption = "Data Source: Statistics SA Census",
y = "Population density per square km") +
theme_bw()
print(p)
})

# Use the Afrikaans name stored in the grid, "name_af", as facet labels
p <- ggplot(sa_pop_dens, aes(factor(year), density, fill = factor(year))) +
geom_col() +
facet_geo(~ code, grid = "sa_prov_grid1", label = "name_af") +
labs(title = "South Africa population density by province",
caption = "Data Source: Statistics SA Census",
y = "Population density per square km") +
theme_bw()
print(p)
expect_no_error({
p <- ggplot(sa_pop_dens, aes(factor(year), density, fill = factor(year))) +
geom_col() +
facet_geo(~ code, grid = "sa_prov_grid1", label = "name_af") +
labs(title = "South Africa population density by province",
caption = "Data Source: Statistics SA Census",
y = "Population density per square km") +
theme_bw()
print(p)
})

Sys.setenv(GEOFACET_PKG_TESTING = "TRUE")
my_grid <- us_state_grid1
my_grid$col[my_grid$code == "WI"] <- 7
grid_submit(my_grid, name = "us_grid_tweak_wi",
desc = "Modified us_state_grid1 to move WI over")

# edit aus_grid1
grid_design(data = aus_grid1,
img = "http://www.john.chapman.name/Austral4.gif")
# start with a clean slate
grid_design()
# arrange the alphabet
grid_design(data.frame(code = letters))
expect_no_error({
my_grid <- us_state_grid1
my_grid$col[my_grid$code == "WI"] <- 7
grid_submit(my_grid, name = "us_grid_tweak_wi",
desc = "Modified us_state_grid1 to move WI over")
})

expect_no_error({
# edit aus_grid1
grid_design(data = aus_grid1,
img = "http://www.john.chapman.name/Austral4.gif")
# start with a clean slate
grid_design()
# arrange the alphabet
grid_design(data.frame(code = letters))
})

Sys.setenv(GEOFACET_PKG_TESTING = "")

Expand Down

0 comments on commit 0046861

Please sign in to comment.