tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.47k stars 2.02k forks source link

Add feature to give legend items row/column locations to appear. #6026

Open Maschette opened 1 month ago

Maschette commented 1 month ago

In situations when we are plotting factors with (in this case points) different types of categories, we would like to be able to have more control on how things appear in the legend. Whilst you can give ggplot the number of rows and columns to use for the legend you cannot (as far as I can find) give it the row/column locations of the factors which would allow you to have empty spaces in the legend like this:

image

I tried to look at the legend source code to suggest a potential implementation but could not work out exactly where the rows and columns are implemented.

teunbrand commented 1 month ago

I like this idea, but maybe this is a better fit for a guide extension rather than built into guide_legend(). Out of curiosity, how would you expect to feed the group structure to the legend?

Maschette commented 1 month ago

I had been thinking about this, my first thought is similar to the named vector option used for values in scale_*_manual() or potentially a list. My guess is that it would eventually need to fit into the params list either as an addition or replacement of the key option and then worked into/ an alternative to the arrange_layout function. https://github.com/tidyverse/ggplot2/blob/7fb4c382f9ea332844d469663a8047355a88dd7a/R/guide-legend.R#L474C3-L474C18

teunbrand commented 1 month ago

I've been experimenting with a similar thing if you're interested. I don't know if I'm going to move that function around though, so it may not be very stable.

smouksassi commented 1 month ago

I was thinking more like a patchwork of individual legend key grobs ? and where user can have complete control of the layout ?