Skip to content

Allow geoms to ignore some set aesthetics #1585

Closed
@jiho

Description

@jiho

Let us say I want to combine a few geoms to make a specific plot, I can define such a function:

library("ggplot2")

d <- data.frame(x=rnorm(10), y=rnorm(10), a=letters[1:10])

my_plot <- function(data, mapping, ...) {
  ggplot() +
    geom_line(data=data, mapping=mapping, ...) +
    geom_point(data=data, mapping=mapping, ...)
}

my_plot(d, aes(x=x, y=y))

But then, if I want to change the linetype, geom_point throws an error because linetype is meaningless for it:

my_plot(d, aes(x=x, y=y), linetype="dashed")
Error: Unknown parameters: linetype

If I recall correctly, this was just silently ignored in the past. Interestingly, mapped aesthetics do not throw this error:

my_plot(d, aes(x=x, y=y, linetype="dashed"))

But the result is pretty useless of course ;-). A more useful example would be

my_plot <- function(data, mapping, ...) {
  ggplot() +
    geom_line(data=data, mapping=mapping, ...) +
    geom_point(data=data, mapping=mapping, ...) +
    geom_text(data=data, mapping=mapping, ...)
}
my_plot(d, aes(x=x, y=y, label=a))

Still moving the text up is not possible after the fact, with something like:

my_plot(d, aes(x=x, y=y, label=a), vjust=0)

I understand the rationale in telling the user explicitly about useless aesthetics, but:

  • I think throwing an error is a bit too much, a warning would be enough. Stopping the execution of the function makes it very cumbersome to use ggplot programmatically to build new visualisations (the "proper" way may be to build new geoms but this requires a lot of sophistication for a simple goal).
  • Alternatively (or in addition) there should be a way to easily bypass the warning/errors, through an argument, FALSE by default (if a warning is used, the person creating the function can also wrap the call in suppressWarnings and that would help).
  • This should be consistent between set and mapped aesthetics (but probably ignore useless inherited aesthetics without warning).

PS: The rationale for this comment is that I often combine geoms in autoplot but want to retain the flexibility of mapping/setting aesthetics from the higher level function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions