Skip to content

Unexpected behaviour of geom_contour() #5215

Closed

Description

@ramiromagno

Hi!

Please scroll down, and look at the two calls of geom_contour(), with mapping of the col aesthetic and without. It seems I'm losing contours (the ones drawn in red).

I am missing something, or, except for the colour, both plots should look the same, right?

{ggplot2} version is 3.4.1.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
  
seq2 <- function(from, to, length.out, by = ((to - from)/(length.out - 1))) {
  
  c(from - by,
    seq(from = from, to = to, length.out = length.out),
    to + by
  )
}

nullclines_2d_data <- function(s, p, m, x, y, x_range, y_range, x_grid_step = 500, y_grid_step = 500) {
  
  x_nm <- names(s)[1L]
  y_nm <- names(s)[2L]
  
  x_grid_ <- seq2(x_range[1], x_range[2], x_grid_step)
  y_grid_ <- seq2(y_range[1], y_range[2], y_grid_step)
  
  nx <- length(x_grid_)
  ny <- length(y_grid_)
  n <- nx * ny
  
  s_ <- vector(mode = "list", 2L)
  s_[[1L]] <- rep(x_grid_, each = ny)
  s_[[2L]] <- rep(y_grid_, times = nx)
  s_ <- setNames(s_, nm = names(s))
  p_ <- lapply(p, rep, n)
  
  dv <- m(0, s_, p_)[[1]]
  nvar <- length(s)
  dim(dv) <- c(ny, nx, nvar)
  
  isolines1 <-
    tibble::tibble(x = s_[[1L]], y = s_[[2L]], z = as.vector(dv[, , 1]))
  
  colnames(isolines1) <- c(names(s), "z")
  
  isolines2 <-
    tibble::tibble(x = s_[[1L]], y = s_[[2L]], z = as.vector(dv[, , 2]))
  
  colnames(isolines2) <- c(names(s), "z")
  
  
  dplyr::bind_rows(isolines1, isolines2, .id = "nullcline")
}

model <- function(t, state, parms) {
  with(as.list(c(state, parms)), {
    dR <- r * R * (1 - R / K) - a * R * N
    dN <- c * a * R * N - delta * N
    return(list(c(dR, dN)))
  })
}

p <- c(
  r = 1,
  K = 1,
  a = 1,
  c = 1,
  delta = 0.5
)
s <- c(R = 1, N = 0.01)

surface <- nullclines_2d_data(s = s, p = p, m = model, 1L, 2L, c(0, 1), c(0, 1), 5, 5)

surface %>%
ggplot(aes(x = R, y = N, z = z, col = nullcline)) +
  geom_contour(breaks = 0)

surface %>%
  ggplot(aes(x = R, y = N, z = z)) +
  geom_contour(breaks = 0)

Created on 2023-03-05 with reprex v2.0.2

Activity

  1. teunbrand commented on Mar 5, 2023

    @teunbrand
    Collaborator

    Hi there,

    Using colour = nullcline introduces a grouping that draws separate contours for nullcline = "1" and nullcline = "2". If this grouping is dropped, you have multiple observations per x/y coordinate, in which contour behaviour is undefined. You can see this if you explicitly set a group (but leave out colour):

    surface %>%
      ggplot(aes(x = R, y = N, z = z, group = nullcline)) +
      geom_contour(breaks = 0)

    All-in-all, I don't think there is any misbehaviour on ggplot2's side.

  2. ramiromagno commented on Mar 5, 2023

    @ramiromagno
    Author

    Hi Teun,

    Thanks for the explanation.

    Generally speaking, most geoms won't make data disappear from your plot if you do not specify the color aesthetic. I understand now why this happens in the case of geom_contour(), i.e. because of how contours are constructed from regular data. I'd like to suggest to trigger a warning indicating that some contours might be missing from the plot because of the multiple observations per (x, y) point, just like those typical warnings one gets stemming from NA values.

  3. teunbrand commented on Mar 5, 2023

    @teunbrand
    Collaborator

    Triggering a warning upon duplicate xy-coordinates seems like an improvement over the current situation, and shouldn't be too hard to catch.

  4. added
    messages
    on Mar 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    layers 📈messages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions