Skip to content

geom_hline() and geom_vline() overwrite provided data frame #3090

@dariyasydykova

Description

@dariyasydykova

This comes from a discussion with @clauswilke. The issue here is related to #2950 but somewhat different.

When a data frame is provided to geom_hline(), it is overwritten. The problem seems to be in the following code chunk:

ggplot2/R/geom-hline.r

Lines 12 to 17 in 3c3062d

# Act like an annotation
if (!missing(yintercept)) {
data <- new_data_frame(list(yintercept = yintercept))
mapping <- aes(yintercept = yintercept)
show.legend <- FALSE
}

Here is a reprex to demonstrate this issue:

This works as expected

library(ggplot2)
library(tidyr)

d <- data.frame(c = c("a", "b"),
                y = c(1, 3))

d %>% ggplot() +
  geom_hline(data = d, aes(yintercept = y, color = c)) +
  facet_wrap(~c)

This doesn't work as expected. The provided data frame is dropped, and colors aren't mapped.

d %>% ggplot() +
  geom_hline(data = d, aes(color = c), yintercept = 1) +
  facet_wrap(~c)

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

No warning is given when the original data frame is being overwritten.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions