diff --git a/figure/unnamed-chunk-10.png b/figure/unnamed-chunk-10.png index d0a2174..67d368d 100644 Binary files a/figure/unnamed-chunk-10.png and b/figure/unnamed-chunk-10.png differ diff --git a/figure/unnamed-chunk-111.png b/figure/unnamed-chunk-111.png new file mode 100644 index 0000000..6ba665d Binary files /dev/null and b/figure/unnamed-chunk-111.png differ diff --git a/figure/unnamed-chunk-112.png b/figure/unnamed-chunk-112.png new file mode 100644 index 0000000..1b12e4f Binary files /dev/null and b/figure/unnamed-chunk-112.png differ diff --git a/figure/unnamed-chunk-3.png b/figure/unnamed-chunk-3.png new file mode 100644 index 0000000..58aabec Binary files /dev/null and b/figure/unnamed-chunk-3.png differ diff --git a/figure/unnamed-chunk-4.png b/figure/unnamed-chunk-4.png new file mode 100644 index 0000000..1e93649 Binary files /dev/null and b/figure/unnamed-chunk-4.png differ diff --git a/figure/unnamed-chunk-5.png b/figure/unnamed-chunk-5.png index e1e2444..c10f118 100644 Binary files a/figure/unnamed-chunk-5.png and b/figure/unnamed-chunk-5.png differ diff --git a/figure/unnamed-chunk-6.png b/figure/unnamed-chunk-6.png index 39a6968..7553771 100644 Binary files a/figure/unnamed-chunk-6.png and b/figure/unnamed-chunk-6.png differ diff --git a/figure/unnamed-chunk-7.png b/figure/unnamed-chunk-7.png new file mode 100644 index 0000000..22ee129 Binary files /dev/null and b/figure/unnamed-chunk-7.png differ diff --git a/figure/unnamed-chunk-8.png b/figure/unnamed-chunk-8.png index 3fb7512..d1bd39c 100644 Binary files a/figure/unnamed-chunk-8.png and b/figure/unnamed-chunk-8.png differ diff --git a/vignettes/world-great-circles.Rmd b/vignettes/world-great-circles.Rmd index cd11c9b..72188e0 100644 --- a/vignettes/world-great-circles.Rmd +++ b/vignettes/world-great-circles.Rmd @@ -1,49 +1,118 @@ -Great circles lines on a world map +Great circles lines on a world map with **rworldmap** and **ggplot2** ======================================================== Sometimes you will want to plot maps on a much larger -scale that we have covered in the majority of -the [Creating-maps-in-R repository](https://github.com/Robinlovelace/Creating-maps-in-R/). +scale that we have covered previously in the +'Introduction to visualising spatial data in R' +[tutorial](https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf), +hosted on the +[Creating-maps-in-R github repository](https://github.com/Robinlovelace/Creating-maps-in-R/). For this there are a range of options, including packages called -[*maps*](http://cran.r-project.org/web/packages/maps/index.html), -[`map_data`](http://docs.ggplot2.org/0.9.3/map_data.html) from the -*ggplot2* package and [*rworldmap*](http://cran.r-project.org/web/packages/rworldmap/index.html). - -We will use the latter two options to show how maps of the entire world -can easily be produced in R. Amazingly, in each of the packages, the geographic -data for the world and many of its subregions have already been saved, saving the +[**maps**](http://cran.r-project.org/web/packages/maps/index.html), +a function called [`map_data`](http://docs.ggplot2.org/0.9.3/map_data.html) from +**ggplot2** package and [**rworldmap**](http://cran.r-project.org/web/packages/rworldmap/index.html). + +In this post we will use the latter two (newer) options +to show how maps of the entire world +can easily be produced in R and overlaid with shortest-line +paths called *great circles*. Amazingly, in each package, the geographic +data for the world and many of its subregions +are included, saving the need to download and store files of unknown quality from the internet. -# Plotting continents and great circle lines in base graphics +## Plotting continents and great circle lines in base graphics +The first stage is to load the packages we'll be using: ```{r} -library(rworldmap) -library(geosphere) +x <- c("rworldmap", "geosphere", "ggmap") +lapply(x, require, character.only = T) +``` -s <- getMap() -plot(s) -bbox(s) +Let us proceed by loading an entire map of the world from +the **rworldmap** function `getMap`: + +```{r, fig.keep='none'} +s <- getMap() # load the map data +class(s) # what type of are we dealing with? +nrow(s) # n. polygons +plot(s) # the data plotted (not shown) +bbox(s) # the bounding box... of the entire world +``` + +The above shows that in single line of code we have loaded +`s`, which represents the entire world and all its countries. +This impressive in itself, +and we can easily add further details like colour based on +the countries' attributes (incidentally, you can see +the attribute data by typing `s@data`). + +## Adding points randomly scattered over the face of the Earth + +But what if we want to add up points to the map of +the world and join them up? This can be done in +the same way as we'd add points to any R graphic. +Using our knowledge of `bbox` we can define the limits +of random numbers (from `runif`) to scatter points randomly +over the surface of the earth in terms of longitude. Note the use of +`cos(abs(l))` to avoid oversampling at the poles, +which have a much lower surface area than the equator, per +[line of longitude](http://en.wikipedia.org/wiki/Cylindrical_equal-area_projection). +```{r} set.seed(1984) n = 20 x <- runif(n=n, min=bbox(s)[1,1], max = bbox(s)[1,2] ) -y <- runif(n=n, min=bbox(s)[2,1], max = bbox(s)[2,2] ) +l <- seq(from = -90, to = 90, by = 0.01) +y <- sample(l, size = n, prob = cos(abs(l) * pi / 180)) p <- SpatialPoints(matrix(cbind(x,y), ncol=2), proj4string=CRS("+proj=longlat +datum=WGS84")) +plot(s) points(p, col = "red") +``` + +## Joining the dots + +So how to join these randomly scattered points on the planet? +A first approximation would be to join them with straight lines. +Let's join point 1, for example, to all others to test this method: + +```{r} +plot(s) +segments(x0 = rep(coordinates(p[1,])[1], n), y0 = rep(coordinates(p[1,])[2], n), + x1 = coordinates(p)[,1], y1 = coordinates(p)[,2]) +``` -head(gcIntermediate(p[1,], p[2])) # take a look at the output of the gcIntermediate function -lines(gcIntermediate(p[1,], p[2,])) +(Incidentally, isn't the use of `segments` here rather clunky - any suggestions +of a more elegant way to do this welcome.) +The lines certainly do join up, but something doesn't seem right in the map, right? +Well the fact that you have perfectly straight lines in the image means bendy +lines over the Earth's surface: these are not the shortest, +[great circle](http://en.wikipedia.org/wiki/Great_circle) lines. +To add these great circle lines, we must use the **geosphere** package: + +```{r} +head(gcIntermediate(p[1,], p[2]), 2) # take a look at the output of the gcIntermediate function +plot(s) +lines(gcIntermediate(p[1,], p[2,]), col = "blue", lwd = 3) # for loop to plot all lines going to zone 5 for(i in 1:length(p)){ - lines(gcIntermediate(p[5,], p[i,]), col = "green") + lines(gcIntermediate(p[1,], p[i,]), col = "green") } ``` +Fantastic. Now we have great circle lines represented on a +map with a [geographic coordinate system (CRS)](http://en.wikipedia.org/wiki/Geographic_coordinate_system) +(as opposed to a projected CRS, which approximates Euclidean distance). + +## Beautifying the map + +The maps we created so far are not exactly beautiful. +Let's try to make the map look a little nicer: + + ```{r} -# beautify the map a little names(s@data) library(rgdal) # s <- spTransform(s, CRSobj=CRS("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")) @@ -58,7 +127,7 @@ for(i in 1:length(p)){ par(bg = 'white') ``` -# Doing it in ggplot2 +## Doing it in ggplot2 The 'beautified' map above certainly is more interesting visually, with added colours. But it's difficult to call it truly beautiful. For that, as with @@ -72,8 +141,9 @@ m ``` When we add the lines in projected maps (i.e. with a Euclidean coordinate system) -based solely on origins and destinations, this works fine, but generates incorrect -shortest path lines in ggplot2. +based solely on origins and destinations, this works fine, but +as with the previous example, generates incorrect +shortest path lines: ```{r} # adding lines @@ -85,13 +155,15 @@ shortest path lines in ggplot2. p1 <- coordinates(p[5,])[rep(1, n),] p2 <- coordinates(p) -ggplot() + geom_segment(aes(x = p1[,1], y = p1[,2], xend = p2[,1], yend = p2[,2])) +# test plotting the lines +# ggplot() + geom_segment(aes(x = p1[,1], y = p1[,2], xend = p2[,1], yend = p2[,2])) + ggplot() + geom_polygon(data = s,aes(x=long, y=lat, group=group), fill="green", colour="black") + geom_segment(aes(x = p1[,1], y = p1[,2], xend = p2[,1], yend = p2[,2])) ``` -# Adding great circle lines +## Adding great circle lines to ggplot2 maps Adding great circle lines in ggplot2 is similar, but we must save all of the coordinates of the paths in advance before plotting, @@ -125,7 +197,6 @@ empty ggplot2 instances, ready to be filled with layers. This is more flexible than stating the data at the outset. ```{r} -ggplot() + geom_path(data = paths, aes(lon, lat , group = group)) ggplot() + geom_polygon(data = s, aes(x=long, y=lat, group=group), fill = "green", colour="black") + geom_path(data = paths, aes(lon, lat , group = group)) + @@ -150,12 +221,28 @@ m + coord_map() # remove fill as this clearly causes problems: m <- ggplot() + geom_path(data = s, aes(x=long, y=lat, group=group), colour="black") + geom_path(data = paths, aes(lon, lat , group = group)) -m + coord_map("bicentric", lon = 0) -m + coord_map("bonne", lat= 0) +# m + coord_map("bicentric", lon = 0) +# m + coord_map("bonne", lat= 0) m + coord_map("ortho", orientation=c(41, -74, 0)) # for ortho maps ``` - +## Conclusion + +We've seen 2 ways of plotting maps of the world and overlaying +'great circles' lines on them. There are probably more, but +these two options seem to work well, except with +the bugs in **ggplot2** for plotting polygons in +many map projections. The two methods are not incompatible +(see `fortify` for plotting **sp** objects in **ggplot2**) +and can be combined in many other ways. + +For more information on plotting spatial data in R, +I recommend checking out R's range of +[spatial packages](http://cran.r-project.org/web/views/Spatial.html). +For an introductory tutorial on visualising spatial data +in R, you could do much worse than start with +[Visualising Spatial Data in R](https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf) +by [James Cheshire](http://spatial.ly/) and [myself](http://robinlovelace.net/). diff --git a/vignettes/world-great-circles.md b/vignettes/world-great-circles.md index 2727672..dbc7fb0 100644 --- a/vignettes/world-great-circles.md +++ b/vignettes/world-great-circles.md @@ -1,39 +1,72 @@ -Great circles lines on a world map +Great circles lines on a world map with **rworldmap** and **ggplot2** ======================================================== Sometimes you will want to plot maps on a much larger -scale that we have covered in the majority of -the [Creating-maps-in-R repository](https://github.com/Robinlovelace/Creating-maps-in-R/). +scale that we have covered previously in the +'Introduction to visualising spatial data in R' +[tutorial](https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf), +hosted on the +[Creating-maps-in-R github repository](https://github.com/Robinlovelace/Creating-maps-in-R/). For this there are a range of options, including packages called -[*maps*](http://cran.r-project.org/web/packages/maps/index.html), -[`map_data`](http://docs.ggplot2.org/0.9.3/map_data.html) from the -*ggplot2* package and [*rworldmap*](http://cran.r-project.org/web/packages/rworldmap/index.html). - -We will use the latter two options to show how maps of the entire world -can easily be produced in R. Amazingly, in each of the packages, the geographic -data for the world and many of its subregions have already been saved, saving the +[**maps**](http://cran.r-project.org/web/packages/maps/index.html), +a function called [`map_data`](http://docs.ggplot2.org/0.9.3/map_data.html) from +**ggplot2** package and [**rworldmap**](http://cran.r-project.org/web/packages/rworldmap/index.html). + +In this post we will use the latter two (newer) options +to show how maps of the entire world +can easily be produced in R and overlaid with shortest-line +paths called *great circles*. Amazingly, in each package, the geographic +data for the world and many of its subregions +are included, saving the need to download and store files of unknown quality from the internet. -# Plotting continents and great circle lines in base graphics +## Plotting continents and great circle lines in base graphics +The first stage is to load the packages we'll be using: ```r -library(rworldmap) +x <- c("rworldmap", "geosphere", "ggmap") +lapply(x, require, character.only = T) ``` ``` -## Loading required package: sp -## ### Welcome to rworldmap ### -## For a short introduction type : vignette('rworldmap') +## [[1]] +## [1] TRUE +## +## [[2]] +## [1] TRUE +## +## [[3]] +## [1] TRUE ``` +Let us proceed by loading an entire map of the world from +the **rworldmap** function `getMap`: + + ```r -library(geosphere) +s <- getMap() # load the map data +class(s) # what type of are we dealing with? +``` -s <- getMap() -plot(s) -bbox(s) +``` +## [1] "SpatialPolygonsDataFrame" +## attr(,"package") +## [1] "sp" +``` + +```r +nrow(s) # n. polygons +``` + +``` +## [1] 244 +``` + +```r +plot(s) # the data plotted (not shown) +bbox(s) # the bounding box... of the entire world ``` ``` @@ -42,44 +75,98 @@ bbox(s) ## y -90 83.65 ``` -```r +The above shows that in single line of code we have loaded +`s`, which represents the entire world and all its countries. +This impressive in itself, +and we can easily add further details like colour based on +the countries' attributes (incidentally, you can see +the attribute data by typing `s@data`). + +## Adding points randomly scattered over the face of the Earth + +But what if we want to add up points to the map of +the world and join them up? This can be done in +the same way as we'd add points to any R graphic. +Using our knowledge of `bbox` we can define the limits +of random numbers (from `runif`) to scatter points randomly +over the surface of the earth in terms of longitude. Note the use of +`cos(abs(l))` to avoid oversampling at the poles, +which have a much lower surface area than the equator, per +[line of longitude](http://en.wikipedia.org/wiki/Cylindrical_equal-area_projection). + +```r set.seed(1984) n = 20 -x <- runif(n = n, min = bbox(s)[1, 1], max = bbox(s)[1, 2]) -y <- runif(n = n, min = bbox(s)[2, 1], max = bbox(s)[2, 2]) -p <- SpatialPoints(matrix(cbind(x, y), ncol = 2), proj4string = CRS("+proj=longlat +datum=WGS84")) +x <- runif(n=n, min=bbox(s)[1,1], max = bbox(s)[1,2] ) +l <- seq(from = -90, to = 90, by = 0.01) +y <- sample(l, size = n, prob = cos(abs(l) * pi / 180)) +p <- SpatialPoints(matrix(cbind(x,y), ncol=2), proj4string=CRS("+proj=longlat +datum=WGS84")) +plot(s) points(p, col = "red") +``` + +![plot of chunk unnamed-chunk-3](figure/unnamed-chunk-3.png) + +## Joining the dots + +So how to join these randomly scattered points on the planet? +A first approximation would be to join them with straight lines. +Let's join point 1, for example, to all others to test this method: + -head(gcIntermediate(p[1, ], p[2])) # take a look at the output of the gcIntermediate function +```r +plot(s) +segments(x0 = rep(coordinates(p[1,])[1], n), y0 = rep(coordinates(p[1,])[2], n), + x1 = coordinates(p)[,1], y1 = coordinates(p)[,2]) +``` + +![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-4.png) + +(Incidentally, isn't the use of `segments` here rather clunky - any suggestions +of a more elegant way to do this welcome.) +The lines certainly do join up, but something doesn't seem right in the map, right? +Well the fact that you have perfectly straight lines in the image means bendy +lines over the Earth's surface: these are not the shortest, +[great circle](http://en.wikipedia.org/wiki/Great_circle) lines. +To add these great circle lines, we must use the **geosphere** package: + + +```r +head(gcIntermediate(p[1,], p[2]), 2) # take a look at the output of the gcIntermediate function ``` ``` ## lon lat -## [1,] 57.15 14.024 -## [2,] 57.13 11.946 -## [3,] 57.11 9.867 -## [4,] 57.09 7.789 -## [5,] 57.07 5.710 -## [6,] 57.05 3.632 +## [1,] 55.16 -37.47 +## [2,] 53.16 -37.25 ``` ```r -lines(gcIntermediate(p[1, ], p[2, ])) +plot(s) +lines(gcIntermediate(p[1,], p[2,]), col = "blue", lwd = 3) # for loop to plot all lines going to zone 5 -for (i in 1:length(p)) { - lines(gcIntermediate(p[5, ], p[i, ]), col = "green") +for(i in 1:length(p)){ + lines(gcIntermediate(p[1,], p[i,]), col = "green") } ``` -![plot of chunk unnamed-chunk-1](figure/unnamed-chunk-1.png) +![plot of chunk unnamed-chunk-5](figure/unnamed-chunk-5.png) + +Fantastic. Now we have great circle lines represented on a +map with a [geographic coordinate system (CRS)](http://en.wikipedia.org/wiki/Geographic_coordinate_system) +(as opposed to a projected CRS, which approximates Euclidean distance). + +## Beautifying the map + +The maps we created so far are not exactly beautiful. +Let's try to make the map look a little nicer: ```r -# beautify the map a little names(s@data) ``` @@ -101,38 +188,24 @@ names(s@data) ```r library(rgdal) -``` - -``` -## rgdal: version: 0.8-14, (SVN revision 496) -## Geospatial Data Abstraction Library extensions to R successfully loaded -## Loaded GDAL runtime: GDAL 1.9.2, released 2012/10/08 -## Path to GDAL shared files: /usr/share/gdal -## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480] -## Path to PROJ.4 shared files: (autodetected) -``` - -```r -# s <- spTransform(s, CRSobj=CRS('+proj=robin +lon_0=0 +x_0=0 +y_0=0 -# +ellps=WGS84 +datum=WGS84 +units=m +no_defs')) +# s <- spTransform(s, CRSobj=CRS("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")) rcols <- terrain.colors(length(unique(s$REGION))) s$col <- as.numeric(factor(s$REGION)) -par(bg = "lightblue") +par(bg = 'lightblue') plot(s, col = rcols[s$col], xlim = c(-180, 180)) points(p, col = "red") -for (i in 1:length(p)) { - lines(gcIntermediate(p[5, ], p[i, ]), col = "black") +for(i in 1:length(p)){ + lines(gcIntermediate(p[5,], p[i,]), col = "black") } ``` -![plot of chunk unnamed-chunk-2](figure/unnamed-chunk-2.png) +![plot of chunk unnamed-chunk-6](figure/unnamed-chunk-6.png) ```r -par(bg = "white") +par(bg = 'white') ``` - -# Doing it in ggplot2 +## Doing it in ggplot2 The 'beautified' map above certainly is more interesting visually, with added colours. But it's difficult to call it truly beautiful. For that, as with @@ -141,44 +214,40 @@ so many things in R plotting, we turn to ggplot2. ```r s <- map_data("world") -m <- ggplot(s, aes(x = long, y = lat, group = group)) + geom_polygon(fill = "green", - colour = "black") +m <- ggplot(s, aes(x=long, y=lat, group=group)) + + geom_polygon(fill="green", colour="black") m ``` -![plot of chunk unnamed-chunk-3](figure/unnamed-chunk-3.png) - +![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-7.png) When we add the lines in projected maps (i.e. with a Euclidean coordinate system) -based solely on origins and destinations, this works fine, but generates incorrect -shortest path lines in ggplot2. +based solely on origins and destinations, this works fine, but +as with the previous example, generates incorrect +shortest path lines: ```r -# adding lines for all combinations of lines, use this code p1 <- -# do.call(rbind, rep(list(coordinates(p)),n )) p2 <- -# cbind(rep(coordinates(p)[,1], each=n ), rep(coordinates(p)[,2], each=n )) +# adding lines +# for all combinations of lines, use this code +# p1 <- do.call(rbind, rep(list(coordinates(p)),n )) +# p2 <- cbind(rep(coordinates(p)[,1], each=n ), rep(coordinates(p)[,2], each=n )) # for all lines goint to point 5: -p1 <- coordinates(p[5, ])[rep(1, n), ] +p1 <- coordinates(p[5,])[rep(1, n),] p2 <- coordinates(p) -ggplot() + geom_segment(aes(x = p1[, 1], y = p1[, 2], xend = p2[, 1], yend = p2[, - 2])) -``` - -![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-41.png) +# test plotting the lines +# ggplot() + geom_segment(aes(x = p1[,1], y = p1[,2], xend = p2[,1], yend = p2[,2])) -```r -ggplot() + geom_polygon(data = s, aes(x = long, y = lat, group = group), fill = "green", - colour = "black") + geom_segment(aes(x = p1[, 1], y = p1[, 2], xend = p2[, - 1], yend = p2[, 2])) +ggplot() + geom_polygon(data = s,aes(x=long, y=lat, group=group), + fill="green", colour="black") + + geom_segment(aes(x = p1[,1], y = p1[,2], xend = p2[,1], yend = p2[,2])) ``` -![plot of chunk unnamed-chunk-4](figure/unnamed-chunk-42.png) - +![plot of chunk unnamed-chunk-8](figure/unnamed-chunk-8.png) -# Adding great circle lines +## Adding great circle lines to ggplot2 maps Adding great circle lines in ggplot2 is similar, but we must save all of the coordinates of the paths in advance before plotting, @@ -192,20 +261,19 @@ top of the next: ```r -paths <- gcIntermediate(p[5, ], p[1, ]) +paths <- gcIntermediate(p[5,], p[1,]) paths <- data.frame(paths) paths$group <- 1 sel <- setdiff(2:length(p), 5) -for (i in sel) { - paths.tmp <- gcIntermediate(p[5, ], p[i, ]) - paths.tmp <- data.frame(paths.tmp) - paths.tmp$group <- i - paths <- rbind(paths, paths.tmp) +for(i in sel){ + paths.tmp <- gcIntermediate(p[5,], p[i,]) + paths.tmp <- data.frame(paths.tmp) + paths.tmp$group <- i + paths <- rbind(paths, paths.tmp) } ``` - To plot multiple paths, we can use the `geom_segment` command. Before plotting the lines on the map, it's sometimes best to first plot them on their own to ensure that everything is working. @@ -215,19 +283,13 @@ This is more flexible than stating the data at the outset. ```r -ggplot() + geom_path(data = paths, aes(lon, lat, group = group)) +ggplot() + geom_polygon(data = s, aes(x=long, y=lat, group=group), + fill = "green", colour="black") + + geom_path(data = paths, aes(lon, lat , group = group)) + + theme(panel.background = element_rect(fill = 'lightblue')) ``` -![plot of chunk unnamed-chunk-6](figure/unnamed-chunk-61.png) - -```r -ggplot() + geom_polygon(data = s, aes(x = long, y = lat, group = group), fill = "green", - colour = "black") + geom_path(data = paths, aes(lon, lat, group = group)) + - theme(panel.background = element_rect(fill = "lightblue")) -``` - -![plot of chunk unnamed-chunk-6](figure/unnamed-chunk-62.png) - +![plot of chunk unnamed-chunk-10](figure/unnamed-chunk-10.png) ## Changing projection in ggplot @@ -241,39 +303,41 @@ for plotting polygons. ```r -# to see the range of projections available using this method, try -# ?mapproject +# to see the range of projections available using this method, try ?mapproject m <- last_plot() m + coord_map() ``` -![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-71.png) +![plot of chunk unnamed-chunk-11](figure/unnamed-chunk-111.png) ```r - # remove fill as this clearly causes problems: -m <- ggplot() + geom_path(data = s, aes(x = long, y = lat, group = group), colour = "black") + - geom_path(data = paths, aes(lon, lat, group = group)) -m + coord_map("bicentric", lon = 0) -``` - -![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-72.png) - -```r -m + coord_map("bonne", lat = 0) +m <- ggplot() + geom_path(data = s, aes(x=long, y=lat, group=group), colour="black") + + geom_path(data = paths, aes(lon, lat , group = group)) +# m + coord_map("bicentric", lon = 0) +# m + coord_map("bonne", lat= 0) +m + coord_map("ortho", orientation=c(41, -74, 0)) # for ortho maps ``` -![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-73.png) - -```r -m + coord_map("ortho", orientation = c(41, -74, 0)) # for ortho maps -``` - -![plot of chunk unnamed-chunk-7](figure/unnamed-chunk-74.png) - - - - +![plot of chunk unnamed-chunk-11](figure/unnamed-chunk-112.png) + +## Conclusion + +We've seen 2 ways of plotting maps of the world and overlaying +'great circles' lines on them. There are probably more, but +these two options seem to work well, except with +the bugs in **ggplot2** for plotting polygons in +many map projections. The two methods are not incompatible +(see `fortify` for plotting **sp** objects in **ggplot2**) +and can be combined in many other ways. + +For more information on plotting spatial data in R, +I recommend checking out R's range of +[spatial packages](http://cran.r-project.org/web/views/Spatial.html). +For an introductory tutorial on visualising spatial data +in R, you could do much worse than start with +[Visualising Spatial Data in R](https://github.com/Robinlovelace/Creating-maps-in-R/raw/master/intro-spatial-rl.pdf) +by [James Cheshire](http://spatial.ly/) and [myself](http://robinlovelace.net/).