Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hexisticker - with regions #386

Merged
merged 11 commits into from
Jun 23, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/get_available_datasets.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ get_available_datasets <- function(type, render = FALSE,
bind_rows()
country_data <- available_country_data
} else {
country_data <- all_country_data
country_data <- covidregionaldata::all_country_data
}
if (!missing(type)) {
target_type <- match.arg(
Expand Down
2 changes: 2 additions & 0 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ knitr::opts_chunk$set(
### Design 3
![design 3](man/figures/logo3.png)

### Design 5
![design 5](man/figures/logo5.png)

# Subnational data for the COVID-19 outbreak

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

![design 3](man/figures/logo3.png)

### Design 5

![design 5](man/figures/logo5.png)

# Subnational data for the COVID-19 outbreak

[![Lifecycle:
Expand Down
91 changes: 87 additions & 4 deletions inst/make_hexsticker.R
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
library(covidregionaldata)
library(hexSticker)
library(showtext)
library(ggplot2)
library(dplyr)
library(maps)
library(countrycode)
library(sf)
library(rnaturalearth)
library(rmapshaper)

# font setup
font_add_google("Zilla Slab Highlight", "useme")

# get countries we have data for
regional_countries <- get_available_datasets() %>%
filter(type == "regional")
filter(.data$type == "regional")

regional_countries_l2 <- regional_countries %>%
filter(!(is.na(level_2_region)))
filter(!(is.na(.data$level_2_region)))

regional_countries_l1 <- regional_countries %>%
filter(is.na(.data$level_2_region))

# get world data
world <- spData::world %>%
st_as_sf()


# mark supported countries from the world data
supported_countries <- world %>%
mutate(
fill = case_when(
name_long %in% countryname(regional_countries_l2[["origin"]], , destination = "country.name.en") ~ "Level 2",
name_long %in% countryname(regional_countries[["origin"]], , destination = "country.name.en") ~ "Level 1",
name_long %in% countryname(regional_countries_l2[["origin"]], destination = "country.name.en") ~ "Level 2",
name_long %in% countryname(regional_countries[["origin"]], destination = "country.name.en") ~ "Level 1",
TRUE ~ "Unsupported"
)
)
Expand Down Expand Up @@ -86,6 +94,67 @@ covid_map_2 <- ggplot() +

print(covid_map_2)

# Approach using Natural Earth data

world_without_regions <- ne_countries(returnclass = "sf") %>%
filter(sovereignt != "Antarctica")

# numberOfLevels should be less than half the number of colours in the
# divergent palette used (usually 7)
numberOfLevels <- 3

regional_maps_l1 <- ms_simplify(
ne_states(gsub(' \\(.*\\)', "",
regional_countries_l1$origin, perl=TRUE),returnclass = "sf") %>%
mutate( region_code = woe_id %% numberOfLevels), keep = 0.04)

regional_maps_l2 <- ms_simplify(
ne_states(gsub(' \\(.*\\)', "",
regional_countries_l2$origin, perl=TRUE),returnclass = "sf") %>%
mutate( region_code = woe_id %% numberOfLevels + numberOfLevels + 1), keep = 0.04)

regional_maps <- bind_rows(regional_maps_l1, regional_maps_l2)

# We keep 50% of the points of the country outlines because it's a
# finer scale map
# We add the US and the UK to the list because otherwise we don't
# successfully include them.
regional_outlines <- ms_simplify(
ne_countries(country = c(gsub(' \\(.*\\)', "", regional_countries$origin, perl=TRUE),
"United States", "United Kingdom"),
returnclass = "sf"),
keep = 0.5
)


covid_map_3 <- ggplot() +
ggspatial::layer_spatial(data = world_without_regions, size = 0.01) +
coord_sf(crs = "ESRI:54016") +
# scale_fill_manual(
# name = "",
# values = c("#0072b2", "#cc79a7", "grey80")
# ) +
# scale_color_manual(
# name = "",
# values = c("black", "black", "#666666")
# ) +
# scale_size_manual(
# name = "",
# values = c(0.1, 0.1, 0.018)
# ) +
Copy link
Collaborator

@joseph-palmer joseph-palmer Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put the borders back in around the countries we have data for? I think this will help make them stand out, particularly for the blue countries against the blue background.

Copy link
Collaborator Author

@RichardMN RichardMN Jun 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With some meddling I've managed to put borders back in but they're currently red and I've not figured out why.

The polygons provided by the ne_states call don't know which of their edges are "external" so there's no straightforward way to draw the outlines of the countries using this geometry.

We could try drawing outlines just on some of the countries using the underlying system, and this may be a simpler approach.

The current approach is to draw all countries, then draw the supported countries with a wide outline (I have not figured out how to have an empty fill, sorry), then draw the regions of the supported countries.

Currently the outline of the country is reddish pink and I've not managed to fix that either. I've made some progress but will have to stop for tonight.

edit: If we turn the online layer to just draw edges with a blank fill then the order could be reversed and we’d get the borders between supposed countries drawn as well (and should likely reduce the size). My attempts to do this have hit errors regarding continuous and discrete values when I apply a scale so I am missing some ggplot wisdom. It’s also slightly odd how the thickness of lines differs between a rstudio preview and the rendered hexsticker.

We could also look at reordering the calls to bind_rows and ms_simplify. I don’t think these commute but at the scale we’re dealing with it doesn’t seem to matter.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is wierd. I'm not a fan of the orange borders, ideally these should be black and thinner but if its to much of a hassle I'm happy with it without borders

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shall we merge into the main hex branch and try and resolve the final tweak there? I agree this is great. If I get the chance I will bang my head into the outline issue as it does seem strange.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done a slight reordering.

We now convert the countries outlines into lines (instead of polygons) and draw them after we draw the coloured regions. This means that we now draw borders between countries for which we have regional data.

I know we didn't like the orange, so is it ... better? ... that now the border appears to be blue by default? I think there is a simple ggplot scale/colour error I am making and I hope someone with more expertise may figure it out, though the spatial_layer objects are particular in how they approach aesthetics.

In any case, I think this is closer and would be happy if this were merged into the mainstream hexsticker branch (and possibly tidied to reduce the proliferation and numbering glitches of options) and then polishing can be done there.

In terms of things which could be done to make it cleaner, ne_countries and ne_states appear to have different default scales of the geometries they return, which is why the code simplifies the country outlines down to 4% of original points and the regions only to 50% of original points. Discrepancies only seem to be visible when very closely zoomed in (where regions are drawn over countries with slightly different outlines) but it should be possible to specify the same scale for both and then simplify them by the same factor.

ggspatial::layer_spatial(data = regional_outlines,
aes(colour="black"), size=0.5) +
# scale_color_manual(name = "",
# values = c("black")) +
ggspatial::layer_spatial(data = regional_maps,
aes(fill = region_code), size=0.02) +
coord_sf(crs = "ESRI:54016") +
scale_fill_fermenter(palette = "RdBu") +
theme_void() +
theme(legend.position = "none", axis.text.x = element_blank())

print(covid_map_3)

logo2 <- sticker(
covid_map_2,
package = "covidregionaldata",
Expand Down Expand Up @@ -113,3 +182,17 @@ logo3 <- sticker(
u_size = 3.5,
dpi = 1000
)

logo4 <- sticker(
covid_map_3,
package = "covidregionaldata",
p_size = 48, s_x = 1, s_y = 0.8, s_width = 1.7, s_height = 1.7,
p_y = 1.45,
p_color = "white",
p_family = "useme",
h_color = "#646770",
h_fill = "#24A7DF",
filename = "man/figures/logo5.png",
u_size = 3.5,
dpi = 1000
)
Binary file modified man/figures/logo1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/logo2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified man/figures/logo3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/logo5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.