From 2c27a0db8f8a98ae67d4ae01821e7888fbbde346 Mon Sep 17 00:00:00 2001 From: aho Date: Wed, 24 Nov 2021 18:37:33 +0100 Subject: [PATCH] Plot shapefile after plotting the data, so the shapefile is on top of everything --- R/PlotEquiMap.R | 36 ++++++++++++++++++++++++++---------- inst/doc/FAQ.md | 14 +++++++------- man/PlotEquiMap.Rd | 9 +++++++-- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/R/PlotEquiMap.R b/R/PlotEquiMap.R index ee9945f..3de30e9 100644 --- a/R/PlotEquiMap.R +++ b/R/PlotEquiMap.R @@ -76,8 +76,9 @@ #'@param shapefile A character string of the path to a .rds file or a list #' object containinig shape file data. If it is a .rds file, it should contain #' a list. The list should contains 'x' and 'y' at least, which indicate the -#' location of the shape. If it is specified, 'country.borders' won't be -#' used. The default value is NULL. +#' location of the shape. The default value is NULL. +#'@param shapefile_color Line color of the shapefile. +#'@param shapefile_lwd Line width of the shapefile. The default value is 1. #'@param contours Array of same dimensions as 'var' to be added to the plot #' and displayed with contours. Parameter 'brks2' is required to define the #' magnitude breaks for each contour curve. Disregarded if 'square = FALSE'. @@ -238,7 +239,7 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, square = TRUE, filled.continents = NULL, filled.oceans = FALSE, country.borders = FALSE, coast_color = NULL, coast_width = 1, lake_color = NULL, - shapefile = NULL, + shapefile = NULL, shapefile_color = NULL, shapefile_lwd = 1, contours = NULL, brks2 = NULL, contour_lwd = 0.5, contour_color = 'black', contour_lty = 1, contour_draw_label = TRUE, contour_label_scale = 1, @@ -472,7 +473,7 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } } - # shapefile + # Check shapefile if (!is.null(shapefile)) { if (is.list(shapefile)) { shape <- shapefile @@ -506,6 +507,18 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, } } + # Check shapefile_col + if (is.null(shapefile_color)) { + if (filled.continents) { + shapefile_color <- continent_color + } else { + shapefile_color <- 'black' + } + } + if (!.IsColor(shapefile_color)) { + stop("Parameter 'shapefile_color' must be a valid colour identifier.") + } + # Check contours if (!is.null(contours)) { if (dim(contours)[1] != dims[1] || dim(contours)[2] != dims[2]) { @@ -888,15 +901,10 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, # UPDATE: Use maps::map for both cases. The difference between GEOmap and # maps is trivial. The only thing we can see for now is that # GEOmap has better lakes. - if (!is.null(shapefile)) { - coast <- maps::map(shape, interior = country.borders, wrap = TRUE, - xlim = xlim_conti, ylim = c(-89.99, 89.99), - fill = filled.continents, add = TRUE, plot = FALSE) - } else { coast <- maps::map(continents, interior = country.borders, wrap = TRUE, xlim = xlim_conti, ylim = c(-89.99, 89.99), fill = filled.continents, add = TRUE, plot = FALSE) - } + if (filled.continents) { polygon(coast, col = continent_color, border = coast_color, lwd = coast_width) } else { @@ -922,6 +930,14 @@ PlotEquiMap <- function(var, lon, lat, varu = NULL, varv = NULL, par(lwd = old_lwd) } + # Plot shapefile + if (!is.null(shapefile)) { + maps::map(shape, interior = country.borders, #wrap = TRUE, + xlim = xlim_conti, ylim = c(-89.99, 89.99), + fill = filled.continents, add = TRUE, plot = TRUE, + lwd = shapefile_lwd, col = shapefile_color) + } + box() # Draw rectangle on the map if (!is.null(boxlim)) { diff --git a/inst/doc/FAQ.md b/inst/doc/FAQ.md index 885e49c..fe37ecc 100644 --- a/inst/doc/FAQ.md +++ b/inst/doc/FAQ.md @@ -53,8 +53,7 @@ PlotEquiMap() provides the option to plot the country boundaries by argument the shapefile data as an RDS file or read the shapefile as a list, and use argument `shapefile` to plot it. -If you have more than one shapefile to plot, or you want to add additional -shapefile with `country.borders = TRUE`, you have to plot it on top of the +If you have more than one shapefile to plot, you have to plot it on top of the figure after PlotEquiMap(). The following script shows an example of plotting the Douro region on top of default country boundaries. The data in this example is synthetic. @@ -82,12 +81,13 @@ str(douro) dat <- 1 : (21 * 10) dim(dat) <- c(lon = 21, lat = 10) -# Plot Douro region on top of figure that has country boundaries plotted -PlotEquiMap(dat, lon = c(1:10, 350:360), lat = 36 : 45, drawleg = FALSE, filled.continents = FALSE, country.border = TRUE) -map(douro, interior = FALSE, add = TRUE, lwd = 1, col = "red") +# Plot Douro region using argument 'shapefile' +PlotEquiMap(dat, lon = c(1:10, 350:360), lat = 36 : 45, drawleg = FALSE, + filled.continents = FALSE, country.border = TRUE, + shapefile = douro, shapefile_col = 'blue') -# Plot Douro region solely, using argument 'shapefile' -PlotEquiMap(dat, lon = c(1:10, 350:360), lat = 36 : 45, drawleg = FALSE, filled.continents = FALSE, shapefile = douro) +# Enhance the border line and change the color +map(douro, interior = FALSE, add = TRUE, lwd = 2, col = "red") ``` ## 2. Something goes wrong... diff --git a/man/PlotEquiMap.Rd b/man/PlotEquiMap.Rd index 5263710..31fe4d8 100644 --- a/man/PlotEquiMap.Rd +++ b/man/PlotEquiMap.Rd @@ -29,6 +29,8 @@ PlotEquiMap( coast_width = 1, lake_color = NULL, shapefile = NULL, + shapefile_color = NULL, + shapefile_lwd = 1, contours = NULL, brks2 = NULL, contour_lwd = 0.5, @@ -164,8 +166,11 @@ The default value is NULL.} \item{shapefile}{A character string of the path to a .rds file or a list object containinig shape file data. If it is a .rds file, it should contain a list. The list should contains 'x' and 'y' at least, which indicate the -location of the shape. If it is specified, 'country.borders' won't be -used. The default value is NULL.} +location of the shape. The default value is NULL.} + +\item{shapefile_color}{Line color of the shapefile.} + +\item{shapefile_lwd}{Line width of the shapefile. The default value is 1.} \item{contours}{Array of same dimensions as 'var' to be added to the plot and displayed with contours. Parameter 'brks2' is required to define the -- GitLab