Skip to content

geom_hline() syntax inconsistency - cannot specify yintercept outside of aes when using additional aesthetics #2945

@RichardJActon

Description

@RichardJActon

It seems that you cannot use additional aesthetics with geom_hline if you leave yintercept outside of the aes call, unlike say y in geom_line.

df <- data.frame(
    "x"=rep(c(1,2,3),2),
    "y"=rep(c(1,2,3),2),
    "g"=rep(c(TRUE,FALSE),each=3)
)

This does not work contrary to what you would expect from things like: geom_line(y=1,aes(colour=x))

ggplot(df,aes(x=x,y=y)) + 
    geom_point() +
    geom_hline(yintercept = 1.5,aes(colour=g)) +
    facet_wrap(~g)

goem_hline

This does work but seems counter intuitive. Especially if you wanted, for example, 2 hlines one with a fixed yintercept and one that varied when it would seem more natural to leave the fixed intercept outside of the aesthetic and the variable one in.

ggplot(df,aes(x=x,y=y)) + 
  geom_point() +
  geom_hline(aes(yintercept = 1.5, colour=g)) +
  facet_wrap(~g)

geom_hline_aes

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