Skip to content

Commit

Permalink
remove maptools, rgeos from Suggests; remove maptools from vignette; …
Browse files Browse the repository at this point in the history
…other small fixes to make SOmap_introduction run v0.6.2.9005
  • Loading branch information
raymondben committed Mar 28, 2024
1 parent 2540849 commit ee1a0f3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
4 changes: 1 addition & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SOmap
Type: Package
Title: Southern Ocean maps
Version: 0.6.2.9004
Version: 0.6.2.9005
Authors@R: c(person("Dale", "Maschette", email = "dale.maschette@aad.gov.au", role = c("aut", "cre")),
person("Michael", "Sumner", role = "aut"),
person("Ben", "Raymond", role = "aut"))
Expand Down Expand Up @@ -40,8 +40,6 @@ Suggests:
ggnewscale,
knitr,
maps,
maptools,
rgeos,
rmarkdown,
testthat (>= 2.1.0),
palr,
Expand Down
9 changes: 6 additions & 3 deletions R/gg.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ SOgg_notauto <- function(x) {
out$bathy_legend <- c(SO_plotter(plotfun = "ggplot2::geom_line", plotargs = list(data = theticks, mapping = aes_string(x = "long", y = "lat", group = "group"), col = x$bathy_legend[[1]]$ticks[[1]]$plotargs$col, size = 1)),
SO_plotter(plotfun = "ggplot2::geom_polygon", plotargs = list(data = thecolors, mapping = aes_string(x = "long", y = "lat", group = "group"), fill = NA, col = x$bathy_legend[[1]]$ticks[[1]]$plotargs$col, size = 1)))
out$bathy_legend <- c(out$bathy_legend, unlist(lapply(seq_along(x$bathy_legend[[1]]$legend[[2]]$plotargs$col), function(ii) SO_plotter(plotfun = "ggplot2::geom_polygon", plotargs = list(data = thecolors[thecolors$cols == ii, ], mapping = aes_string(x = "long", y = "lat", group = "group"), fill = x$bathy_legend[[1]]$legend[[2]]$plotargs$col[ii], col = NA))), recursive = FALSE))
out$bathy_legend <- c(out$bathy_legend, SO_plotter(plotfun = "ggplot2::geom_text", plotargs = list(data = as.data.frame(x$bathy_legend[[1]]$tick_labels[[1]]$plotargs$x), mapping = aes_string(x = "lon", y = "lat", label = "a"), size = SOgg_cex(x$bathy_legend[[1]]$tick_labels[[1]]$plotargs$cex))))
temp <- as.data.frame(x$bathy_legend[[1]]$tick_labels[[1]]$plotargs$x)
out$bathy_legend <- c(out$bathy_legend, SO_plotter(plotfun = "ggplot2::geom_text", plotargs = list(data = temp, mapping = aes_string(x = if ("lon" %in% names(temp)) "lon" else "coords.x1", y = if ("lat" %in% names(temp)) "lat" else "coords.x2", label = "a"), size = SOgg_cex(x$bathy_legend[[1]]$tick_labels[[1]]$plotargs$cex))))
}
out$plot_sequence <- c(out$plot_sequence, "bathy_legend")
}
Expand Down Expand Up @@ -280,11 +281,13 @@ SOgg_legend <- function(x) {
out <- c(out, unlist(lapply(seq_along(x$legend[[2]]$plotargs$col), function(ii) SO_plotter(plotfun = "ggplot2::geom_polygon", plotargs = list(data = thecolors[thecolors$cols == ii, ], mapping = aes_string(x = "long", y = "lat", group = "group"), fill = x$legend[[2]]$plotargs$col[ii], col = NA))), recursive = FALSE))
}
if ("tick_labels" %in% x$plot_sequence) {
out <- c(out, SO_plotter(plotfun = "ggplot2::geom_text", plotargs = list(data = as.data.frame(x$tick_labels[[1]]$plotargs$x), mapping = aes_string(x = "lon", y = "lat", label = "a"), size = SOgg_cex(x$tick_labels[[1]]$plotargs$cex))))
temp <- as.data.frame(x$tick_labels[[1]]$plotargs$x)
out <- c(out, SO_plotter(plotfun = "ggplot2::geom_text", plotargs = list(data = temp, mapping = aes_string(x = if ("lon" %in% names(temp)) "lon" else "coords.x1", y = if ("lat" %in% names(temp)) "lat" else "coords.x2", label = "a"), size = SOgg_cex(x$tick_labels[[1]]$plotargs$cex))))
}
if ("legend_labels" %in% x$plot_sequence) {
tang <- if (is.null(x$legend_labels[[1]]$plotargs$srt)) 0 else x$legend_labels[[1]]$plotargs$srt
out <- c(out, SO_plotter(plotfun = "ggplot2::geom_text", plotargs = list(data = as.data.frame(x$legend_labels[[1]]$plotargs$x), mapping = aes_string(x = "lon", y = "lat", label = "a"), angle = tang, size = SOgg_cex(x$legend_labels[[1]]$plotargs$cex))))
temp <- as.data.frame(x$legend_labels[[1]]$plotargs$x)
out <- c(out, SO_plotter(plotfun = "ggplot2::geom_text", plotargs = list(data = temp, mapping = aes_string(x = if ("lon" %in% names(temp)) "lon" else "coords.x1", y = if ("lat" %in% names(temp)) "lat" else "coords.x2", label = "a"), angle = tang, size = SOgg_cex(x$legend_labels[[1]]$plotargs$cex))))
}
}
out
Expand Down
40 changes: 6 additions & 34 deletions vignettes/SOmap-introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,15 @@ abline(h = c(-90, 90), v = c(-180, 180))
```

A very similar and more modern data set is available in the `maptools` package.

```{r wrld-simpl}
data("wrld_simpl", package = "maptools")
library(sp)
plot(wrld_simpl)
```

This data set aligns exactly to the conventional -180/180 -90/90 extent of the longitude/latitude projection.

```{r lon180-lat90}
plot(0, type = "n", axes = FALSE, xlab = "", ylab = "", xlim = c(-180, 180), ylim = c(-90, 90))
rect(xleft = -180, ybottom = -90, xright = 180, ytop = 90, border = "darkred", lwd = 4, lty = 2)
plot(wrld_simpl, add = TRUE)
```

### Exercises

1. How can we find the longitude and latitude ranges of the maps data `m` and the maptools data `wrld_simpl`?
1. How can we find the longitude and latitude ranges of the maps data `m`?
2. Can we draw polygons with a fill colour with the maps package?


Answer 1: `range(m$x, na.rm = TRUE)` `range(m$y, na.rm = TRUE`) also `m$range`

Answer 2: `polygon(lonlat, col = "grey")` does not work, and `map(mp, fill = TRUE, col = "grey")` does not work, but `maps::map(fill = TRUE, col = "grey")` does seem to work.
Answer 2: `polygon(lonlat, col = "grey")` does not work, but `maps::map(fill = TRUE, col = "grey")` does seem to work.


What's going on? Look at the very south-eastern corner of the map. The "coastline" has been extended to the very south boundary of the available area.
Expand All @@ -81,13 +65,12 @@ What's going on? Look at the very south-eastern corner of the map. The "coastlin

```{r south-east}
plot(0, type = "n", axes = FALSE, xlab = "", ylab = "", xlim = c(-150, 180), ylim = c(-90, -60))
plot(wrld_simpl, add = TRUE, col = "grey")
rect(xleft = -180, ybottom = -90, xright = 180, ytop = 90, border = "darkred", lwd = 4, lty = 2)
maps::map(add = TRUE, col = "dodgerblue", lwd = 3)
```

When we add the old maps coastline see that it does not extend to 90S and it does not traverse the southern boundary.
The old maps coastline does not extend to 90S and it does not traverse the southern boundary.

One reason for this is that if we choose a projection where the east and west edges of the Antarctic coastline meet then we get what looks a fairly clean join.

Expand All @@ -99,17 +82,6 @@ plot(terra::project(lonlat, to = "+proj=laea +lat_0=-90 +datum=WGS84", from = "E
```

If we try the same with `wrld_simpl` it's not as neat. We have a strange "seam" that points exactly to the south pole (our projection is centred on longitude = 0, and latitude = -90.



```{r wrld}
plot(sp::spTransform(wrld_simpl, "+proj=laea +lat_0=-90 +datum=WGS84"), asp = 1,
xlim = c(-1, 1) * f, ylim = c(-1, 1) * f, xlab = "", ylab = "", lwd = 3)
abline(v = 0, h = 0, lty = 2, col = "grey")
```


### Let's use the maps data!

In `m` we have the maps data structure, and this looks promising.
Expand All @@ -118,7 +90,7 @@ In `m` we have the maps data structure, and this looks promising.
str(m)
mp <- m
pxy <- terra::project(lonlat, to = "+proj=laea +lat_0=-90 +datum=WGS84", from "EPSG:4326")
pxy <- terra::project(lonlat, to = "+proj=laea +lat_0=-90 +datum=WGS84", from = "EPSG:4326")
mp$x <- pxy[,1]
mp$y <- pxy[,2]
mp$range <- c(range(mp$x,na.rm = TRUE), range(mp$y, na.rm = TRUE))
Expand All @@ -133,7 +105,7 @@ maps::map(mp, add = TRUE, fill = TRUE, col = "grey")
```

The problem is that the maps database has enough internal structure to join lines correctly, with `NA` gaps between different connected linestrings, but not enough to draw these things as polygons. A similar problem occurs in the default projection. While `wrld_simpl` has been extend by placing two dummy coordinates at the east and west versions of the south pole, this data set does not have those.
The problem is that the maps database has enough internal structure to join lines correctly, with `NA` gaps between different connected linestrings, but not enough to draw these things as polygons. A similar problem occurs in the default projection.

We have to look quite carefully to understand what is happening, but this is wrapping around overlapping itself and so close to the southern bound we barely notice.

Expand Down Expand Up @@ -516,7 +488,7 @@ Remember that printing or plotting a `SOmap_gg` object produces a `ggplot` objec

```{r somap6, message = FALSE, warning = FALSE}
library(ggplot2)
my_ggplot + geom_point(data = as.data.frame(my_points), aes(lon, lat, colour = z), size = 3) +
my_ggplot + geom_point(data = as.data.frame(my_points), aes(coords.x1, coords.x2, colour = z), size = 3) +
scale_colour_distiller(palette = "Spectral")
```

Expand Down

0 comments on commit ee1a0f3

Please sign in to comment.