inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
76 stars 21 forks source link

Update gg methods to recognise colour as alternative spelling to color #128

Closed ASeatonSpatial closed 2 years ago

ASeatonSpatial commented 2 years ago

Some of the code for gg methods does not recognise colour as an alternative spelling to color. Since this is standard ggplot2 behaviour we should update the gg plotting methods accordingly. Below is an example for gg.SpatialPolygons.

library(inlabru)
#> Loading required package: ggplot2
#> Loading required package: sp
data("gorillas", package = "inlabru")
bnd = gorillas$boundary

# undesired behaviour:
# colour is ignored
ggplot() +
  gg(bnd, colour = "red") +
  coord_equal()
#> Regions defined for each Polygons
#> Warning: Duplicated aesthetics after name standardisation: colour


# using alternative spelling
# gives desired behaviour
ggplot() +
  gg(bnd, color = "red") +
  coord_equal()
#> Regions defined for each Polygons


# options to fix

# Quick fix:
# add a check if colour is used in ...
# and if so set color = colour

gg.SpatialPolygons <- function(data, mapping = NULL, crs = NULL, color = "black", alpha = NULL, ...) {

  requireNamespace("ggplot2")
  if (!requireNamespace("ggpolypath", quietly = TRUE)) {
    stop("The 'ggpolypath' package is required for SpatialPolygons plotting, but it is not installed.")
  }
  if (!is.null(crs)) {
    data <- spTransform(data, crs)
  }
  df <- ggplot2::fortify(data)
  if (requireNamespace("ggpolypath", quietly = TRUE)) {
    dmap <- ggplot2::aes(x = .data$long, y = .data$lat, group = .data$group)
  } else {
    dmap <- ggplot2::aes(x = .data$long, y = .data$lat, group = .data$id, subgroup = .data$hole)
  }

  if (!("alpha" %in% names(dmap)) & is.null(alpha)) {
    alpha <- 0.1
  }
  if (!("color" %in% names(dmap)) & is.null(color)) {
    color <- "black"
  }

  if (!is.null(mapping)) {
    dmap <- modifyList(dmap, mapping)
  }

  # check for colour
  if ("colour" %in% names(list(...))){
    args = list(...)
    color = args$colour
  }

  if (requireNamespace("ggpolypath", quietly = TRUE)) {
    ggpolypath::geom_polypath(data = df, mapping = dmap, alpha = alpha, color = color, ...)
  } else {
    ggplot2::geom_polygon(data = df, mapping = dmap, alpha = alpha, color = color, ...)
  }
}

# Not so nice that warning message still appears
ggplot() +
  gg(bnd, colour = "red") +
  coord_equal()
#> Regions defined for each Polygons
#> Warning: Duplicated aesthetics after name standardisation: colour


# More complete solution:
# Remove color = 'black' as default argument
# and alpha = 0.1 as default argument
# and set these internally

gg.SpatialPolygons <- function(data, mapping = NULL, crs = NULL, ...) {

  requireNamespace("ggplot2")
  if (!requireNamespace("ggpolypath", quietly = TRUE)) {
    stop("The 'ggpolypath' package is required for SpatialPolygons plotting, but it is not installed.")
  }
  if (!is.null(crs)) {
    data <- spTransform(data, crs)
  }
  df <- ggplot2::fortify(data)
  if (requireNamespace("ggpolypath", quietly = TRUE)) {
    dmap <- ggplot2::aes(x = .data$long, y = .data$lat, group = .data$group)
  } else {
    dmap <- ggplot2::aes(x = .data$long, y = .data$lat, group = .data$id, subgroup = .data$hole)
  }

  if (!is.null(mapping)) {
    dmap <- modifyList(dmap, mapping)
  }

  params = list(...)
  arg = list(data = df,
             mapping = dmap)

  if (!("colour" %in% names(params) |
        "color" %in% names(params))){
    arg$colour = "black"
  }
  if (!("alpha" %in% names(params))) arg$alpha = 0.1
  arg = modifyList(arg, params)

  if (requireNamespace("ggpolypath", quietly = TRUE)) {
    do.call(ggpolypath::geom_polypath, arg)
  } else {
    do.call(gggplot2::geom_polygon, arg)
  }
}
# Desired behaviour, no warning message.
ggplot() +
  gg(bnd, colour = "red") +
  coord_equal()
#> Regions defined for each Polygons


# Alternative spelling works
ggplot() +
  gg(bnd, color = "red") +
  coord_equal()
#> Regions defined for each Polygons

# Defaults work
ggplot() +
  gg(bnd) +
  coord_equal()
#> Regions defined for each Polygons


# setting alpha works
ggplot() +
  gg(bnd, alpha = 0.5) +
  coord_equal()
#> Regions defined for each Polygons

Created on 2021-12-13 by the reprex package (v2.0.1)

finnlindgren commented 2 years ago

Thanks. I think I always use the R "feature" of partial matching, with col = ... instead of either colour or color, but allowing all versions would be better. A further issue is whether the colour setting should be in the aesthetic or not.

finnlindgren commented 2 years ago

The last version is appropriate; I'm adding it now.

finnlindgren commented 2 years ago

I believe 5bee951 solves this.