You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: intro-spatial.Rmd
+26-68Lines changed: 26 additions & 68 deletions
Original file line number
Diff line number
Diff line change
@@ -56,7 +56,9 @@ experience with R will help.
56
56
If you have not used R before, it may be worth following an
57
57
introductory tutorial, such as
58
58
*Efficient R Programming*
59
-
([Gillespie and Lovelace, 2016](http://cran.r-project.org/doc/contrib/Torfs+Brauer-Short-R-Intro.pdf)), the official [Introduction to R](https://cran.r-project.org/doc/manuals/r-release/R-intro.html) or tutorials suggested on [rstudio.com](https://www.rstudio.com/online-learning/) and [cran.r-project.org](https://cran.r-project.org/other-docs.html).
59
+
([Gillespie and Lovelace, 2016](https://csgillespie.github.io/efficientR/)), *R for Data Science* ([Grolemund and Wickham, 2016](http://r4ds.had.co.nz/)) or tutorials suggested on [rstudio.com](https://www.rstudio.com/online-learning/) and [cran.r-project.org](https://cran.r-project.org/other-docs.html).
60
+
61
+
...
60
62
61
63
Now you know some R, it's time to turn your attention towards spatial data with R. To that end, this tutorial is organised as follows:
62
64
@@ -127,7 +129,7 @@ on and download some packages and data.
127
129
R has a huge and growing number of spatial data packages. We recommend taking a quick browse on R's main website to see the spatial packages available:
In this tutorial we will use the following packages:
132
+
In this tutorial we will use the packages from the '**sp**verse', that use the **sp** package:
131
133
132
134
-**ggmap**: extends the plotting package **ggplot2** for maps
133
135
-**rgdal**: R's interface to the popular C/C++ spatial data processing library [gdal](http://www.gdal.org/)
@@ -136,6 +138,7 @@ In this tutorial we will use the following packages:
136
138
-[**dplyr**](http://cran.r-project.org/web/packages/dplyr/index.html) and [**tidyr**](http://blog.rstudio.org/2014/07/22/introducing-tidyr/): fast and concise data manipulation packages
137
139
-**tmap**: a new packages for rapidly creating beautiful maps
138
140
141
+
For a tutorial based on the recent [**sf**](https://github.com/edzer/sfr) package you will have to look elsewhere, like the [geocompr](https://bookdown.org/robinlovelace/geocompr/) website, the online home of the forthcoming book *Geocomputation with R*.
139
142
140
143
Some packages may already be installed on your computer. To test if a package is installed, try to load it using the `library` function; for example, to test if **ggplot2** is installed, type `library(ggplot2)` into the console window.
141
144
If there is no output from R, this is good news: it means that the library
@@ -208,7 +211,7 @@ R to handle a broader range of spatial data formats. If you've not already
To explore `lnd` object further, try typing `nrow(lnd)` (display number of rows) and record how many zones the dataset contains. You can also try `ncol(lnd)`.
266
269
270
+
One issue with the data that we have loaded is that it has no coordinate reference system (CRS):
271
+
272
+
```{r}
273
+
lnd@proj4string
274
+
```
275
+
267
276
## Basic plotting
268
277
269
278
Now we have seen something of the structure of spatial objects in R,
@@ -282,8 +291,8 @@ an entirely different type of plot. Thus R is intelligent at guessing what you w
282
291
R has powerful subsetting capabilities that can be accessed very concisely using square brackets,as shown in the following example:
283
292
284
293
```{r}
285
-
# select rows of lnd@data where sports participation is less than 15
286
-
lnd@data[lnd$Partic_Per < 15, ]
294
+
# select rows of lnd@data where sports participation is less than 13
295
+
lnd@data[lnd$Partic_Per < 13, 1:3]
287
296
```
288
297
289
298
The above line of code asked R to select only the rows from the `lnd` object, where sports participation is lower than 15,
@@ -324,7 +333,7 @@ we will cover this in more detail in subsequent sections.
324
333
325
334
As a bonus stage, select and plot
326
335
only zones that are
327
-
close to the centre of London (see Fig. 6). Programming encourages rigorous
336
+
close to the centre of London (see Figure 5). Programming encourages rigorous
328
337
thinking and it helps to define the problem
329
338
more specifically:
330
339
@@ -383,75 +392,24 @@ lnd$quadrant <- "unknown" # prevent NAs in result
383
392
lnd$quadrant[east & north] <- "northeast"
384
393
```
385
394
386
-
> **Challenge**: Based on the the above code as refrence try and find the remaining 3 quadrants and colour them as per Figure 6 below.
395
+
> **Challenge**: Based on the the above code as refrence try and find the remaining 3 quadrants and colour them as per Figure 6.
387
396
Hint - you can use the **llgridlines** function in order to overlay the long-lat lines.
388
397
For bonus points try to desolve the quadrants so the map is left with only 4 polygons.
389
398
390
-
```{r, echo=FALSE, eval=FALSE}
399
+
```{r, echo=FALSE, fig.cap="The 4 quadrants of London and dissolved borders. Challenge: recreate a plot that looks like this.", fig.show='hold', out.height="5cm"}
391
400
lnd$quadrant[!east & north] <- "northwest"
392
401
lnd$quadrant[east & !north] <- "southeast"
393
402
lnd$quadrant[!east & !north] <- "southwest"
394
-
library(tmap)
395
-
qtm(lnd, fill = "quadrant")
396
-
lnd_disolved = rgeos::gUnaryUnion(spgeom = lnd, id = lnd$quadrant)
397
-
plot(lnd_disolved)
398
-
```
399
-
400
-
401
-
```{r, echo=FALSE, fig.cap="The 4 quadrants of London"}
402
-
grid.raster(readPNG("figure/lnd-quads.png"))
403
-
par(mfrow = c(1,2))
404
-
#plot the results
405
-
# plot(london)
406
-
# plot(lnd[east & north,],add = TRUE, col = "red" )
407
-
408
-
# place a grid over the object
409
-
# llgridlines(lnd, lty= 3, side ="EN", offset = -0.5)
403
+
plot(lnd)
404
+
plot(lnd[east & north,], add = TRUE, col = "red" )
405
+
llgridlines(lnd, lty= 3, side ="EN", offset = -0.5)
410
406
411
-
london = gUnaryUnion(lnd, lnd$dummy)
412
-
london = SpatialPolygonsDataFrame(london, data.frame(dummy = c("london")), match.ID = FALSE)
0 commit comments