Skip to content

Unexpected behaviour of geom_contour() #5215

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

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 📈messagesrequests for improvements to error, warning, or feedback messages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions