Skip to content

geom_label locations affected by "fill" argument #6761

@emjonaitis

Description

@emjonaitis

I found a problem with the location of labels created by geom_label in the context of polar coordinates.

When I do not use the "fill" argument in geom_label, the labels are located correctly. However, when I set "fill" to a color, the labels appear to swap positions, as though theta had been set to minus theta. I expected fill color and location to be able to be changed independently.

Here is the code to reproduce the bug:

penguin_sum <- penguins %>%
  group_by(species) %>%
  summarize(y=n(), .groups="drop") %>%
  merge(summarize(., N=sum(y))) %>%
  mutate(pct=round(100*y/N),
         label=paste0("N=", y, "\n(", pct, "%)"))

penguin_sum
# Counts:
# Adelie 152
# Chinstrap 68
# Gentoo 124

# Labels display on correct segment, with Adelie N=152
ggplot(penguin_sum, aes(x="", y=y, fill=species)) +
  geom_col(color="white") +
  geom_label(aes(label=label), position=position_stack(vjust=0.5)) +
  coord_polar(theta="y") 

# Labels print in mirror, so it looks like Adelie N=124
ggplot(penguin_sum, aes(x="", y=y, fill=species)) +
  geom_col(color="white") +
  geom_label(aes(label=label), fill="white", position=position_stack(vjust=0.5)) +
  coord_polar(theta="y")

reprex::reprex()

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