thomasp85 / ggforce

Accelerating ggplot2
https://ggforce.data-imaginist.com
Other
916 stars 105 forks source link

facet_wrap_paginate(): dropping colors in the legend #298

Open adamolko opened 1 year ago

adamolko commented 1 year ago

Hello,

Thank you for a very nice package! I found function facet_wrap_paginate() especially useful, but would be great if it would also allow for dropping irrelevant colors from the legend..

As an example: here is a plot using starwars dataset from dplyr package, where I want to assign a specific color to every name.

library(ggplot2)
library(ggforce)
library(dplyr)

plot_data <- starwars[1:20, ]
g <- ggplot(data = plot_data, aes(x = homeworld, y = height, color = name)) + 
  geom_point(size = 3) + 
  scale_color_manual(values = c("#89C5DA", "#DA5724", "#74D944", "#CE50CA", "#3F4921", "#C0717C", "#CBD588", "#5F7FC7", 
                                         "#673770", "#D3D93E", "#38333E", "#508578", "#D7C1B1", "#689030", "#AD6F3B", "#CD9BCD", 
                                         "#D14285", "#6DDE88", "#652926", "#7FDCC0"),
                     labels = unique(plot_data$name)) +
  theme(axis.text.x = element_text(angle = 90))

Then I use facet_wrap_paginate() function to have at most 6 facets per page:

g + facet_wrap_paginate(facets = "hair_color", nrow = 3, ncol = 2, page = 1)

image

And then the second page: g + facet_wrap_paginate(facets = "hair_color", nrow = 3, ncol = 2, page = 2) image

As you can see, color legend on both pages is the same, even though the names in the dataset are unique and are only present on one of the two pages. It would be handy to have an option to drop those irrelevant elements from the legend.

AngelicaEMB commented 1 year ago

Hi! Same question here. It would definitively be handy to have the option to drop the irrelevant elements from the legend.