Skip to content

Commit 55e7e75

Browse files
committed
update vignette
1 parent 32f36bd commit 55e7e75

13 files changed

+61
-30
lines changed

R/annotation-map-tile.R

-13
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@
2323
#' annotation_map_tile(zoom = 13, cachedir = system.file("rosm.cache", package = "ggspatial")) +
2424
#' geom_sf(data = longlake_waterdf, fill = NA, col = "grey50")
2525
#'
26-
#' ggplot() +
27-
#' annotation_map_tile(
28-
#' data = tibble::tibble(
29-
#' type = c("osm", "stamenbw", "cartolight", "cartodark"),
30-
#' zoom = 13
31-
#' ),
32-
#' cachedir = system.file("rosm.cache", package = "ggspatial"),
33-
#' mapping = aes(type = type, zoom = zoom)
34-
#' ) +
35-
#' geom_sf(data = longlake_waterdf, fill = NA, col = "grey50") +
36-
#' coord_sf(crs = 3857) +
37-
#' facet_wrap(~type)
38-
#'
3926
annotation_map_tile <- function(type = "osm", zoom = NULL, zoomin = -2,
4027
forcedownload = FALSE, cachedir = NULL,
4128
progress = c("text", "none"), quiet = TRUE,
16.9 KB
Loading
14.8 KB
Loading
15.3 KB
Loading
12.7 KB
Loading
14.6 KB
Loading
15.1 KB
Loading
13.9 KB
Loading
13.7 KB
Loading
13 KB
Loading
12.8 KB
Loading

man/annotation_map_tile.Rd

-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/ggspatial.Rmd

+61-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "A framework for plotting spatial objects using ggplot2"
2+
title: "Spatial objects using ggspatial and ggplot2"
33
author: "Dewey Dunnington"
44
date: "`r Sys.Date()`"
55
output: rmarkdown::html_vignette
@@ -11,18 +11,75 @@ vignette: >
1111

1212
```{r setup, include = FALSE}
1313
library(ggspatial)
14+
rosm::set_default_cachedir(system.file("rosm.cache", package = "ggspatial"))
15+
knitr::opts_chunk$set(fig.width = 4, fig.height = 3, dpi = 150)
1416
```
1517

16-
On first use, a GIS user who stumbles across [ggplot2](https://cran.r-project.org/package=ggplot2) will recognize much of the syntax of how plots are built from GIS language: there are layers, geometries, coordinate systems, and the ability to map attributes to the appearance of the layer (aesthetics). Using ggplot2 to plot spatial line and polygon objects has long been possible thanks to the `fortify()` implementation for `SpatialLines` and `SpatialPolygons` provided in the `ggplot2` package, and there is even an [entire wiki article](https://github.com/tidyverse/ggplot2/wiki/plotting-polygon-shapefiles) on ploting spatial polygons using ggplot2. As is, the syntax for plotting spatial objects ussing `ggplot()` is verbose and unlikely to inspire its use for the rapid creation of maps. The `ggspatial` package is designed to bridge the syntax gap between spatial objects and ggplot2 layers, allowing spatial data to be plotted efficiently.
17-
18-
That said, it should be noted that `ggplot2` is **not** a GIS. The grammar of graphics (outlined by Leland Wilkinson in his [excellent book](http://www.springer.com/gp/book/9780387245447)) was never designed to produce maps, and is almost infinitely flexible at (it could be argued) the expense of performance. So to qualify the above statement, it is more accurate to say that `ggspatial` is designed to leverage the existing facilities of (the `ggplot2` implementation of) the grammar of graphics to produce effecive maps with a small amount of readable code. For large data files, it is unlikely that this package will produce these plots quickly, in the computing sense. There are many packages in the [spatial task view](https://CRAN.R-project.org/view=Spatial) that are well suited to this task.
18+
On first use, a GIS user who stumbles across [ggplot2](https://cran.r-project.org/package=ggplot2) will recognize much of the syntax of how plots are built from GIS language: there are layers, geometries, coordinate systems, and the ability to map attributes to the appearance of the layer (aesthetics). With **ggplot** version 3.0, `geom_sf()` and `coord_sf()` let users pass simple features (**sf** package) objects as layers. Non-spatial data (data frames with XY or lon/lat coluns) and raster data are not well-supported in **ggplot2**, which the gap filled by this package.
1919

2020
This vignette assumes that readers are familar with the usage of `ggplot2`. There are many excellent resources for learning to use `ggplot2`, one of which is the [data visualization chapter](http://r4ds.had.co.nz/data-visualisation.html) in Hadley Wickham's excellent book, [R for Data Science](http://r4ds.had.co.nz).
2121

2222
## Sample data
2323

24+
This vignette uses some data that was used in/collected for my honours thesis [@dunnington11]. The data is included as files within the package, and can be loaded using `load_longlake_data()`.
25+
2426
```{r}
27+
library(ggspatial)
2528
load_longlake_data()
2629
```
2730

31+
## Using `layer_spatial()` and `annotation_spatial()`
32+
33+
Any spatial layer can be added to a `ggplot()` using `layer_spatial()` (well, any object from the **sf**, **sp**, or **raster** packages...). These layers will train the scales, meaning they will be visible unless you explicitly set the X or Y scale limits. Unlike `geom_` or `stat_` functions, `layer_spatial()` always takes its data first. Aesthetics can be passed for most types of objects, the exception being RGB rasters, which are more like backround images than data that should be mapped to a scale. Unlike `layer_spatial()`, `annotation_spatial()` layers never train the scales, so they can be used as a backdrop for other layers.
34+
35+
```{r}
36+
ggplot() +
37+
annotation_spatial(longlake_waterdf) +
38+
layer_spatial(longlake_depthdf, aes(col = DEPTH.M))
39+
```
40+
41+
## Using north arrow and scalebar
42+
43+
North arrows are added using the `annotation_north_arrow()` function, and scalebars can be added using `annotation_scale()`. These functions are "spatial-aware", meaning they know where north is and what the distance is across the plot. Thus, they don't need any arguments (unless you think the defaults aren't pretty). There are two styles for scalebars and four styles for north arrows (see `?annotation_scale` and `?annotation_north_arrow` for details).
44+
45+
```{r}
46+
ggplot() +
47+
annotation_spatial(longlake_waterdf) +
48+
layer_spatial(longlake_depthdf, aes(col = DEPTH.M)) +
49+
annotation_scale(location = "tl") +
50+
annotation_north_arrow(location = "br", which_north = "true")
51+
```
52+
53+
## Tile map layers
54+
55+
Using the **rosm** package, **ggspatial** can add tile layers from a few predefined tile sources (see `rosm::osm.types()`), or from a custom URL pattern. The tiles will get projected into whatever projection is defined by `coord_sf()` (this defaults to the CRS of the first `geom_sf()` or `layer_spatial()` layer that was added to the plot). It's usually necessary to adjust the zoom to the right level when you know how the plot will be used...the default is to be a little more zoomed out than usual, so that the plot loads quickly. Higher zoom levels will make the plot render slowly quite fast.
56+
57+
```{r, message=FALSE}
58+
ggplot() +
59+
annotation_map_tile(type = "osm") +
60+
layer_spatial(longlake_depthdf, aes(col = DEPTH.M))
61+
```
62+
63+
There are a number of url patterns available for tiles, although how they are formatted varies. The **rosm** package uses `${x}`, `${y}`, and `${z}` for the x, y , and zoom of the tile (or `${q}` for the quadkey, if you're using Microsoft Bing maps), which is for the most part the same as for QGIS3. For some freely available tile sources, see [this blog post](https://www.xyht.com/spatial-itgis/using-openstreetmap-basemaps-qgis-3-0/), and for a number of other tile sources that are less open you'll have to dig around yourself. Bing Virtual Earth is a particularly good one (`type = "http://ecn.t3.tiles.virtualearth.net/tiles/a${q}.jpeg?g=1"`).
64+
65+
## Data frames with coordinates
66+
67+
Lots of good spatial data comes in tables with a longitude and latitude column (or sometimes UTM easting/northing columns). In **ggspatial** you can use `df_spatial()` to get a spatial object into a data frame with coordinate columms (much like `fortify()` does, but with a bit better coverage of spatial types). Conversely, you can use a data frame with coordinate columns in `geom_spatial_*` functions to use these data with `geom_sf()`/`coord_sf()`/`layer_spatial()`. More generally, you can use `stat_spatial_identity()` to use any geometry with `coord_sf()`. For example, a polar perspective on two cities across the world from eachother could look like this:
68+
69+
```{r, message=FALSE, warning=FALSE}
70+
cities <- data.frame(
71+
x = c(-63.58595, 116.41214),
72+
y = c(44.64862, 40.19063),
73+
city = c("Halifax", "Beijing")
74+
)
75+
76+
library(ggrepel) # needed for geom_text_repel()
77+
78+
ggplot(cities, aes(x, y)) +
79+
annotation_map_tile(type = "stamenwatercolor") +
80+
geom_spatial_point() +
81+
stat_spatial_identity(aes(label = city), geom = "text_repel", box.padding = 1) +
82+
coord_sf(crs = 3995)
83+
```
2884

85+
Using `df_spatial()` is probably useful mostly with point geometries so they can be used with `stat_spatial_identity()` and `geom_text_repel()`. It can also be useful with **raster** objects that need calculations done on them, as it translates them into a data frame.

0 commit comments

Comments
 (0)