Skip to content

geom_ribbon() > geom_area() speed difference #5788

Description

@japhir

I was recently making some figures in which I wanted a simple geom_area() call on something with quite a few points. However, I noticed that my computer was waiting for a long time when calling the function. I had previously plotted something with geom_ribbon() instead, and it was almost instant there. After playing around, it seems that by default on my machine geom_area() is way slower than geom_ribbon() for some reason.

See below for an attempt at a reprex. Benchmark results are a bit iffy though.

library(ggplot2)
library(tibble)

dat <- tibble(
  x = 1:1e4,
  y = rnorm(seq_along(x)) + 5
)

dat |>
  ggplot() +
  geom_area(aes(x = x, y = y))

dat |>
  ggplot() +
  geom_ribbon(aes(x = x, ymin = 0, ymax = y))

# they seem identical
res <- bench::mark(
  area = dat |>
    ggplot() +
    geom_area(aes(x = x, y = y)),
  ribbon = dat |>
    ggplot() +
    geom_ribbon(aes(x = x, ymin = 0, ymax = y)),
  check = FALSE)

# until you actually print them!
res <- bench::mark(
  area = dat |>
    ggplot() +
    geom_area(aes(x = x, y = y)) |> print(), # sloooww
  ribbon = dat |>
    ggplot() +
    geom_ribbon(aes(x = x, ymin = 0, ymax = y)) |> print(), # fast
  check = FALSE)
#> mapping: x = ~x, y = ~y 
#> geom_area: na.rm = FALSE, orientation = NA, outline.type = upper
#> stat_align: na.rm = FALSE, orientation = NA
#> position_stack 
#> mapping: x = ~x, ymin = 0, ymax = ~y 
#> geom_ribbon: na.rm = FALSE, orientation = NA, outline.type = both
#> stat_identity: na.rm = FALSE
#> position_identity 
#> mapping: x = ~x, y = ~y 
#> geom_area: na.rm = FALSE, orientation = NA, outline.type = upper
#> stat_align: na.rm = FALSE, orientation = NA
#> position_stack 
#> mapping: x = ~x, y = ~y 
#> geom_area: na.rm = FALSE, orientation = NA, outline.type = upper
#> stat_align: na.rm = FALSE, orientation = NA
#> position_stack 
#> mapping: x = ~x, y = ~y 
#> geom_area: na.rm = FALSE, orientation = NA, outline.type = upper
#> stat_align: na.rm = FALSE, orientation = NA
#> position_stack 

# < omitted output >

#> geom_ribbon: na.rm = FALSE, orientation = NA, outline.type = both
#> stat_identity: na.rm = FALSE
#> position_identity

# geom_area seems to get many more gc/sec?
res
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 area         2.03ms   2.13ms      406.    44.3KB     6.19
#> 2 ribbon       2.03ms   2.23ms      375.    22.4KB     6.35

Created on 2024-03-20 with reprex v2.1.0

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions