Skip to content

grouped_df should not require a ".group" variable for data on the other layers #2378

Closed
@yutannihilation

Description

@yutannihilation

Due to the change in response to #2351, a grouped_df now creates a variable called .group. This may be problematic because ggplot2 treats the first layer so special as all the following layers are required to contain all mapped variables except when another mapping is specified individually. Here is an example:

reprex::reprex_info()
#> Created by the reprex package v0.1.1.9000 on 2017-12-22

library(dplyr, warn.conflicts = FALSE)
library(ggplot2)

df <- data.frame(
  x = 1:10,
  y = 1:10,
  type = rep(c("a", "b"), 5),
  stringsAsFactors = FALSE
) %>%
  group_by(type)

df_labels <- data.frame(
  x = 1,
  y = 2,
  text = "foo!"
)

ggplot(df, aes(x, y)) +
  geom_point(aes(colour = type)) +
  geom_label(data = df_labels, aes(label = text))
#> Error in FUN(X[[i]], ...): object '.group' not found

ggplot(ungroup(df), aes(x, y)) +
  geom_point(aes(colour = type)) +
  geom_label(data = df_labels, aes(label = text))

Though I'm not sure this is just a bug, I'm afraid #2351 needs a bit more considerations (better error messages, at least).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorlayers 📈

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions