Skip to content

Warning Failed to apply after_scale() modifications to legend #6567

@MichaelRFox

Description

@MichaelRFox

I'm receiving a warning when I attempt to build a plot that has three things:

  • geom_rect
  • a categorical axis
  • a fill aesthetic

I would expect the following reproducible example to work. It does display the plot properly, but I always receive the following warning:

Warning message: Failed to apply after_scale()modifications to legend Caused by error: ! object 'ymin' not found

If I remove the fill aesthetic, the plot displays without the warning. I tried staging the fill aesthetic, but received the same results.

Reproducible example:

library(ggplot2)

data <- tibble::tibble(
  Question = factor(c("Question A", "Question A", "Question A", "Question A", "Question A", 
               "Question B", "Question B", "Question B", "Question B", "Question B", 
               "Question C", "Question C", "Question C", "Question C", "Question C", 
               "Question D", "Question D", "Question D", "Question D", "Question D", 
               "Question E", "Question E", "Question E", "Question E", "Question E")),
  Response = factor(c('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree',
               'Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree',
               'Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree',
               'Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree',
               'Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree')),
  Min = c(0.5, 5.5, 8.5, 12.5, 15.5, -25.0, -4.0, 8.0, 13.0, 19.0, -3.0, 5.0, 7.0, 
          14.0, 19.0, -1.5, 8.5, 8.5, 12.5, 16.5, -16.0, -5.0, 10.0, 11.0, 26.0),
  Max = c(5.5, 8.5, 12.5, 15.5, 17.5, -4.0, 8.0, 13.0, 19.0, 29.0, 5.0, 7.0, 14.0,
          19.0, 23.0, 8.5, 8.5, 12.5, 16.5, 21.5, -5.0, 10.0, 11.0, 26.0, 33.0)
)

mapping <- aes(fill = Response,
               xmin = Min, 
               xmax = Max, 
               ymin = stage(Question, after_scale = ymin - 0.3), 
               ymax = stage(Question, after_scale = ymax + 0.3))

plt <- ggplot(data = data) +
  geom_rect(mapping = mapping) +
  scale_y_discrete()

plt
#> Warning: Failed to apply `after_scale()` modifications to legend
#> Caused by error:
#> ! object 'ymin' not found

Created on 2025-08-04 with reprex v2.1.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions