library(rgdal); library(rgeos)
dsn <- system.file('extdata', package = 'ggsn')
## Map in geographic coordinates.
map <- readOGR(dsn, 'sp')
map@data$id <- 1:nrow(map@data)
map.ff <- fortify(map, region = 'id')
map.df <- merge(map.ff, map@data, by = 'id')
ggplot(data = map.df, aes(long, lat, group = group, fill = nots)) +
geom_polygon() +
coord_equal() +
geom_path() +
scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
scalebar(map.df, dist = 5, dd2km = TRUE, model = 'WGS84')
## Map in meter coordinates.
map2 <- spTransform(map, CRS("+init=epsg:31983"))
map2@data$id <- 1:nrow(map2@data)
map2.ff <- fortify(map2, region = 'id')
map2.df <- merge(map2.ff, map2@data, by = 'id')
ggplot(data = map2.df, aes(long, lat, group = group, fill = nots)) +
geom_polygon() +
coord_equal() +
geom_path() +
scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
scalebar(map2.df, dist = 5)
ggplot(data = map2.df, aes(long, lat, group = group, fill = nots)) +
geom_polygon() +
coord_equal() +
geom_path() +
scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
scalebar(map2.df, dist = 7, anchor = c(x = 355000, y = 7360000),
st.bottom = FALSE, st.size = 8)
ggplot(data = map2.df, aes(long, lat, group = group, fill = nots)) +
geom_polygon() +
coord_equal() +
geom_path() +
scale_fill_brewer(name = 'Animal abuse\nnotifications', palette = 8) +
scalebar(map2.df, location = 'topright', dist = 5)
Run the code above in your browser using DataLab