-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03-polygons-section.Rmd
More file actions
120 lines (81 loc) · 4.22 KB
/
Copy path03-polygons-section.Rmd
File metadata and controls
120 lines (81 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
---
title: "Geospatial tutorial - section 3"
author: "JR Ferrer-Paris"
---
## Explore polygons with `sf`
So we know the basics of a _simple feature_ object with points, and we have seen some simple ways to interact with this object. We will now learn that this structure will be very similar for other kind of geometries and this makes working with multiple objects very straightforward.
### The municipalities of Franconia (polygons)
Let's check one object that is already in one of the loaded data sets, it contains the boundaries of the municipalities of Franconia.
We will update the crs information to avoid warnings about an old-style crs object:
```{r}
st_crs(franconia) <- "EPSG:4326"
```
###
Check the information of this object, what is different from previous examples? what is similar?:
```{r polysex1, exercise=TRUE, paged.print=FALSE}
franconia
```
###
All `sf` objects have some common structure, regardless of how complex the spatial information is. The interesting feature here is that the geometry is composed of several polygons that can be associated with a single record in the data frame. Many spatial objects are nested, and thus multiple elements (points, lines, polygons) can share the same attributes. The _multi_-type of geometries allow to handle this kind of data in the same way as we would handle single points, lines or polygons.
### Plotting geometry and data
We have seen before that we can query both the geometry and the non spatial data of `sf` objects.
Let's plot the geometry without any attributes:
```{r polysex2, exercise=TRUE }
franconia %>% st_geometry() %>% plot
```
Or plot ALL attributes. (This can be kind of annoying for very large and complex objects...)
```{r polysex3, exercise=TRUE }
franconia %>% plot
```
###
Usually we want to plot one single attribute, for example let's choose the district variable, do you remember how to do this?:
```{r polysex4, exercise=TRUE}
# franconia %>% ____(district) %>% ____
```
```{r polysex4-solution}
franconia %>% select(district) %>% plot
```
```{r polysex4-check}
"Great! we first use `select` to select the variable and then `plot` to call the default plot function for `sf` objects"
```
###
We can go one step further and dissolve the boundaries of adjacent municipalities of the same district, this will give us a new spatial object with three features:
```{r polysex6, exercise=TRUE}
franconia %>% group_by(district) %>% summarise(new_geom=st_union(geometry)) %>% plot
```
Notice how we can use spatial functions like `st_union` with other functions like `group_by` and `summarise`.
### Spatial query of a polygon
If all our data is in the same projection (sharing the same or similar _crs_) we can perform spatial queries, for example, we want to extract the intersection of the location of the University of Bayreuth (in object UBT) with the Franconia municipalities (in object franconia)
```{r polysex7, exercise=TRUE}
st_intersection(UBT,franconia)
```
The result is a point geometry with all variables from the UBT and the franconia data sets.
```{r where_is_UBT, echo = FALSE}
question(" Where is the University of Bayreuth located?",
answer("Unterfranken"),
answer("Oberfranken", correct = TRUE),
answer("Mittelfranken"),
allow_retry = TRUE
)
```
### Plotting spatial data
Simple feature object have their own plot functions, but I find it easier to combine different spatial objects with the `ggplot2` functions.
In `ggplot`, each layer of plot elements is handled by a `geom_` function, for `sf` object the function is called `geom_sf`.
We can combine the geom_sf calls with other ggplot functions to modify colors, etc.
```{r}
ggplot() +
geom_sf(data = franconia, aes(fill=district)) +
geom_sf(data = breweries, col="darkgreen") +
geom_sf(data = UBT, cex=2, col="brown") +
scale_fill_brewer(palette = "Greys")
```
###
Better yet, in `ggplot` we can use variables in different aesthetic elements of the plot, for example, colour or size:
```{r}
ggplot() +
geom_sf(data = franconia, aes(fill=district)) +
geom_sf(data = breweries, aes(size=number.of.types, colour=founded)) +
geom_sf(data = UBT, cex=2, col="brown") +
scale_fill_brewer(palette = "Greys") +
scale_colour_gradientn(colours = brewer.pal(5,"Oranges"))
```