Skip to content

Commit e13a80a

Browse files
committed
Update intro comments, mention sf
1 parent 1b03211 commit e13a80a

File tree

1 file changed

+26
-68
lines changed

1 file changed

+26
-68
lines changed

intro-spatial.Rmd

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ experience with R will help.
5656
If you have not used R before, it may be worth following an
5757
introductory tutorial, such as
5858
*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+
...
6062

6163
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:
6264

@@ -127,7 +129,7 @@ on and download some packages and data.
127129
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:
128130
[http://cran.r-project.org/web/views/Spatial.html](http://cran.r-project.org/web/views/Spatial.html).
129131

130-
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:
131133

132134
- **ggmap**: extends the plotting package **ggplot2** for maps
133135
- **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:
136138
- [**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
137139
- **tmap**: a new packages for rapidly creating beautiful maps
138140

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*.
139142

140143
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.
141144
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
208211

209212
```{r, message=FALSE, results='hide'}
210213
library(rgdal)
211-
lnd <- readOGR(dsn = "data/LondonBoroughs.shp")
214+
lnd <- readOGR(dsn = "data/london_sport.shp")
212215
# lnd <- readOGR(dsn = "data", layer = "london_sport")
213216
```
214217

@@ -264,6 +267,12 @@ grid.raster(readPNG("figure/tab-complete.png"))
264267
265268
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)`.
266269
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+
267276
## Basic plotting
268277

269278
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
282291
R has powerful subsetting capabilities that can be accessed very concisely using square brackets,as shown in the following example:
283292

284293
```{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]
287296
```
288297

289298
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.
324333

325334
As a bonus stage, select and plot
326335
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
328337
thinking and it helps to define the problem
329338
more specifically:
330339

@@ -383,75 +392,24 @@ lnd$quadrant <- "unknown" # prevent NAs in result
383392
lnd$quadrant[east & north] <- "northeast"
384393
```
385394

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.
387396
Hint - you can use the **llgridlines** function in order to overlay the long-lat lines.
388397
For bonus points try to desolve the quadrants so the map is left with only 4 polygons.
389398

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"}
391400
lnd$quadrant[!east & north] <- "northwest"
392401
lnd$quadrant[east & !north] <- "southeast"
393402
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)
410406
411-
london = gUnaryUnion(lnd, lnd$dummy)
412-
london = SpatialPolygonsDataFrame(london, data.frame(dummy = c("london")), match.ID = FALSE)
413-
414-
centrelondon = gCentroid(london, byid = TRUE)
415-
416-
centreLEP = gCentroid(lnd, byid = TRUE)
417-
coords <- cbind(centrelondon$y)
418-
419-
c1 = c(centrelondon$x, centrelondon$x)
420-
c2 = c(90, -90)
421-
c3 = c(90, -90)
422-
c4 = c(centrelondon$y,centrelondon$y)
423-
424-
# simple line strings
425-
L1 = Line(cbind(c1, c2))
426-
L2 = Line(cbind(c3, c4))
427-
428-
Ls1 = Lines(list(L1), ID = "a")
429-
Ls2 = Lines(list(L2), ID = "b")
430-
431-
Ls1 <- SpatialLines(LinesList = list(Ls1))
432-
Ls2 <- SpatialLines(LinesList = list(Ls2))
433-
434-
Longitude = SpatialLinesDataFrame(Ls1, data.frame(Z = c("1", "2"), row.names = c("a","b")))
435-
Latitude = SpatialLinesDataFrame(Ls2, data.frame(Z = c("1", "2"), row.names = c("a","b")))
436-
437-
east <- coordinates(lnd)[,1] > Longitude@lines[[1]]@Lines[[1]]@coords[,1][1]
438-
north <- coordinates(lnd)[,2] > Latitude@lines[[1]]@Lines[[1]]@coords[,2][1]
439-
west <- coordinates(lnd)[,1] < Longitude@lines[[1]]@Lines[[1]]@coords[,1][1]
440-
south <-coordinates(lnd)[,2] < Latitude@lines[[1]]@Lines[[1]]@coords[,2][1]
441-
442-
lnd@data$quadrant[east & north] <- "northeast"
443-
lnd@data$quadrant[west & north] <- "northwest"
444-
lnd@data$quadrant[east & south] <- "southeast"
445-
lnd@data$quadrant[west & south] <- "southwest"
446-
447-
# plot(lnd)
448-
# plot(lnd[east & north,],add = TRUE, col = "red" )
449-
# plot(lnd[west & north,],add = TRUE, col = "blue" )
450-
# plot(lnd[east & south,],add = TRUE, col = "green" )
451-
# plot(lnd[west & south,],add = TRUE, col = "yellow" )
407+
lnd_disolved = rgeos::gUnaryUnion(spgeom = lnd, id = lnd$quadrant)
452408
453-
# llgridlines(lnd, lty= 3, side ="EN", offset = -0.5)
454-
par(mfrow=c(1,1)) # return to default par
409+
library(tmap)
410+
qtm(lnd, fill = "quadrant") +
411+
tm_shape(lnd_disolved) +
412+
tm_borders(lwd = 9)
455413
```
456414

457415
<!-- ## Attribute data -->
@@ -975,7 +933,7 @@ plot(stations) # test the clip succeeded
975933
<!-- ``` -->
976934

977935
<!-- This results in a simple choropleth map and a new vector containing the area of each -->
978-
<!-- borough (the basis for Fig. 11). As an additional step, try comparing the mean -->
936+
<!-- borough (the basis for Figure 11). As an additional step, try comparing the mean -->
979937
<!-- area of each borough with the -->
980938
<!-- mean value of `stations` points within it: `plot(lnd_n$NUMBER, areas)`. -->
981939

0 commit comments

Comments
 (0)