Skip to content

Warnings with geom_area() with axis limits corresponding to min/max values being plotted introduced by ggplot2 v4.0.0 #6667

@agranholm

Description

@agranholm

I have encountered new warnings triggered by using geom_area() in combination with scale_x_continuous() with the limits argument set to the actual minimum/maximum values of the data being plotted.

These warnings are new with ggplot2 version 4.0.0 and reverting to 3.5.2 removes the warnings. The warnings seem to occur due to the new stat_align() function being the new default for the stat parameter, and they occur due to stat_align() interpolating beyond the range of the input data.

The warnings were encountered when running the examples in this function: https://inceptdk.github.io/adaptr/reference/plot_status.html

A minimal reproducible example is provided here:

library(tibble)
library(ggplot2)

dta <- tribble(~x, ~status, ~p,
               0, "A", 1,
               0, "B", 0,
               50, "A", 0.75,
               50, "B", 0.25,
               100, "A", 0.45,
               100, "B", 0.55)

# With limits: warnings occur
p <- ggplot(dta, aes(x = x, y = p, fill = status)) + 
  geom_area() +
  scale_x_continuous(limits = c(0, 100), expand = c(0, 0))
p
# Warning:
# Removed 4 rows containing missing values or values outside the scale range
# (`geom_area()`). 

# Generated data contains xmin/xmax values of NA
ggplot_build(p)$data[[1]]

# Without limits: no warnings
p <- ggplot(dta, aes(x = x, y = p, fill = status)) + 
  geom_area() +
  scale_x_continuous(expand = c(0, 0))
p

# Generated data contains x/xmin/xmax values corresponding to the actual min/max
# values -/+ the range between them divided by 1000
ggplot_build(p)$data[[1]]

# With stat = "identity": no warnings
p <- ggplot(dta, aes(x = x, y = p, fill = status)) + 
  geom_area(stat = "identity") +
  scale_x_continuous(limits = c(0, 100), expand = c(0, 0))
p
# Warning:
# Removed 4 rows containing missing values or values outside the scale range
# (`geom_area()`). 

# Generated data contains xmin/xmax values of NA
ggplot_build(p)$data[[1]]

As seen when using ggplot_build(), geom_area() adds rows to the generated datasets with x/xmin/xmax values corresponding to the actual minimum/maximum values -/+ the range between them divided by 1000 (same with values in between the min/max).
If these values are outside the x-axis limits, they will be NA, and lead to the warnings. This can of course be handled in multiple ways - adding an explicit stat = "identity" to the call solves is as shown, but this behaviour was unexpected to me. Even if you want to use the functionality provided by stat_align(), it seems unexpected that it interpolates beyond the range of the input data.

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