R package to add north symbols and scale bars to maps created with ggplot or ggmap
Clone or download
Permalink
Failed to load latest commit information.
R scalebar: documentation update Jun 15, 2018
dev_tools scalebar: bug fixed. Nov 10, 2017
figure Update Jul 26, 2017
inst Update Jul 6, 2016
man scalebar: documentation update. Jun 15, 2018
.Rbuildignore Update Jul 26, 2017
.gitignore First commit Aug 22, 2015
DESCRIPTION v0.4.11 Jun 15, 2018
LICENSE Initial commit Aug 22, 2015
NAMESPACE v0.4.3 Aug 16, 2017
NEWS v0.4.11 Jun 15, 2018
README.Rmd Update Jul 26, 2017
README.md Update Jul 26, 2017
_config.yml Set theme jekyll-theme-cayman Jun 28, 2017
ggsn.Rproj Allow for modification of scalebar text and box colors May 5, 2017

README.md

The ggsn package improves the GIS capabilities of R, making possible to add 18 different north symbols and scale bars in kilometers to maps in geographic or metric coordinates created with ggplot or ggmap.

To install the CRAN version use:

install.packages('ggsn')

To install the development version use (make sure that devtools is installed):

devtools::install_github('oswaldosantos/ggsn')

Examples

Read a shapefile:

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

# Map in geographic coordinates
map <- st_read(dsn, 'sp', quiet = TRUE)

# Map in projected coordinates
map2 <- st_transform(map, 31983)

ggplot map:

(ggm1 <- ggplot(map, aes(fill = nots)) +
    geom_sf() +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8))

Now, let's use the ggsn package to add a blank background, a north symbol and a scale bar with segments of 5km:

ggm1 +
    blank() +
    north(map) +
    scalebar(map, dist = 5, dd2km = TRUE, model = 'WGS84')

The scale bar works with maps in geographic and meter coordinates:

ggm1 +
    north(map) +
    scalebar(map, dist = 5, dd2km = TRUE, model = 'WGS84')

ggplot(map2, aes(fill = nots)) +
    geom_sf() +
    north(map2, symbol = 16, scale = 0.15) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
    scalebar(map2, dist = 5) +
    xlab('Meters') +
    ylab('Meters')

The packages ggsn and ggmap can be used together:

library(ggmap)
library(sp)
library(rgdal)
library(broom)
sp <- get_googlemap("São Paulo")
bb <- c(st_bbox(map) * matrix(rep(c(1.001, 0.999), e = 2), ncol = 2))
nms <- names(attr(sp, "bb"))
attr(sp, "bb")[1, ] <- bb[c(2, 1, 4, 3)]

map_sp <- readOGR(dsn, "sp")
#> OGR data source with driver: ESRI Shapefile 
#> Source: "/home/oswaldo/R/x86_64-pc-linux-gnu-library/3.4/ggsn/extdata", layer: "sp"
#> with 96 features
#> It has 2 fields
map_sp@data$id <- 0:(nrow(map_sp@data) - 1)
map_sp <- merge(tidy(map_sp), map_sp, by = 'id')

ggmap(sp) +
    geom_polygon(data = map_sp, aes(long, lat, group = group, fill = nots),
                 alpha = .7) +
    coord_equal() +
    geom_path(data = map_sp, aes(long, lat, group = group)) +
    blank() +
    scalebar(map_sp, dist = 5, dd2km = T, model = 'WGS84') +
    north(map) +
    scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
    theme(legend.position = c(0.9, 0.35))

We have used default behaviors but we can change the position and size of the north symbol and the scale bar. For the scale bar, its height, text size and text position can be controlled too. To see the available north symbols, use:

northSymbols()