oswaldosantos/ggsn

Support for two variable faceting

Closed this issue · 0 comments

ggsn only supports one variable faceting. At the moment, when using two variables the scale bar is drawn on multiple facets. For example:

library(rgdal); library(broom); library(dplyr);
dsn <- system.file('extdata', package = 'ggsn')

## Map in geographic coordinates.
map <- readOGR(dsn, 'sp')
map@data$id <- 1:nrow(map@data)
map.df <- merge(tidy(map), map, by = 'id')

map_df <- bind_rows(
    map.df %>% mutate(f1 = "a", f2 = "c"),
    map.df %>% mutate(f1 = "b", f2 = "c"),
    map.df %>% mutate(f1 = "a", f2 = "d"),
    map.df %>% mutate(f1 = "b", f2 = "d")
)

ggplot(data = map_df, aes(long, lat, group = group, fill = nots)) +
    geom_polygon() +
    coord_equal() +
    geom_path() +
    facet_grid(f1~f2) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
    scalebar(map.df, dist = 5, dd2km = TRUE, model = 'WGS84', facet.var = 'f1', facet.lev = 'a')

Two variable faceting could be specified by supplying a vector of variable names and facet levels.

ggplot(data = map_df, aes(long, lat, group = group, fill = nots)) +
    geom_polygon() +
    coord_equal() +
    geom_path() +
    facet_grid(f1~f2) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
    scalebar(map.df, dist = 5, dd2km = TRUE, model = 'WGS84', facet.var = c('f1', 'f2'), facet.lev = c('a', 'b'))