Skip to content

Better error messages for incorrect aesthetic mappings #3060

Closed
@clauswilke

Description

@clauswilke

I encountered some cryptic error messages that pop up in response to fairly normal user mistakes, where the user maps an aesthetic to a data column that doesn't exist. A better error message could help a lot here.

My code example below first shows the desired output and then two incorrect ways in which a user might try to generate it.

library(ggplot2)

# desired plot
ggplot(mtcars, aes(mpg, 1)) +
  geom_tile(aes(fill = stat(density)), stat = "density") +
  geom_point(position = "jitter")

# wrong, `fill` aesthetic mapped to stat-generated data column that
# doesn't exist in `geom_point()` layer
ggplot(mtcars, aes(mpg, 1, fill = stat(density))) +
  geom_tile(stat = "density") +
  geom_point(position = "jitter")
#> Error in is.finite(x): default method not implemented for type 'closure'

# wrong, `fill` aesthetic mapped to data column that doesn't exist
ggplot(mtcars, aes(mpg, 1, fill = density)) +
  geom_tile(stat = "density") +
  geom_point(position = "jitter")
#> Don't know how to automatically pick scale for object of type function. Defaulting to continuous.
#> Error: Column `fill` must be a 1d atomic vector or a list

Created on 2019-01-07 by the reprex package (v0.2.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions