jonocarroll / ggeasy

ggplot2 shortcuts (transformations made easy)
https://jonocarroll.github.io/ggeasy/
272 stars 21 forks source link

Reverse Legend Order #47

Open thomas-neitmann opened 4 years ago

thomas-neitmann commented 4 years ago

When creating a horizontal grouped bar chart the order in the legend does not match the order of the bars in the plot.

library(ggplot2)
library(dplyr)

p <- mtcars %>%
  group_by(cyl, am) %>%
  summarise(n = n()) %>%
  ggplot(aes(cyl, n, fill = factor(am))) +
  geom_col(position = "dodge") +
  coord_flip()

image

Changing this involves some functions I almost never use (and thus always forget).

p + guides(fill = guide_legend(reverse = TRUE))

image

My proposal is to have something like easy_reverse_legend(). What do you think @jonocarroll?