Skip to content

n legend with single value 1 when using geom_bar(stat = "sum") #4848

Closed
@stragu

Description

@stragu

Using stat = "sum" in geom_bar() displays a count legend labelled "n" with a grey square as a default. It is particularly out of place when the bars are filled, resulting in what seems to be two fill legends, the grey not matching anything on the plot.

The issue has been brought up on StackOverflow, with a workaround: https://stackoverflow.com/questions/50378718/what-is-the-n-1-box-in-my-r-geom-bar-legend-and-how-do-i-remove

library(ggplot2)
exp_df <- data.frame(x = c("A", "B", "B", "C"),
           value = 1:4,
           group = c("Z", "Z", "Y", "Y"))
ggplot(exp_df, aes(x, value)) +
  geom_bar(stat = "sum")

# with fill
ggplot(exp_df, aes(x, value, fill = group)) +
  geom_bar(stat = "sum")

Created on 2022-05-17 by the reprex package (v2.0.1)

Should geom_bar() not display this legend at all?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions