thomasp85 / ggforce

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

Feature request: add label_colour as an aesthetic #240

Closed MattCowgill closed 8 months ago

MattCowgill commented 3 years ago

Hello, Thank you for a very useful package.

It would be handy to be able to map label.colour to the data as an aesthetic. As an example:

library(ggplot2)
library(ggforce)

ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
  geom_point() +
  ggforce::geom_mark_ellipse(aes(label = cyl)) 

Created on 2021-06-11 by the reprex package (v2.0.0)

Here it would be useful to colour the 4, 6, and 8 labels the same as the points and ellipses, by mapping them to the data.

We can colour all annotations:

library(ggplot2)
library(ggforce)

ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
  geom_point() +
  ggforce::geom_mark_ellipse(aes(label = cyl),
                             label.colour = "red") 

Created on 2021-06-11 by the reprex package (v2.0.0)

But label.colour is not implemented as an aesthetic:

library(ggplot2)
library(ggforce)

ggplot(mtcars, aes(x = wt, y = mpg, col = factor(cyl))) +
  geom_point() +
  ggforce::geom_mark_ellipse(aes(label = cyl,
                                 label.colour = cyl)) 
#> Warning: Ignoring unknown aesthetics: label.colour

Created on 2021-06-11 by the reprex package (v2.0.0)

Making label.colour an aesthetic would be a really useful addition to the package.

rjake commented 3 years ago

I'm also looking for this...

stragu commented 2 years ago

I agree this would be very handy to increase readability of all `geommark*() functions when there are many categories or when they inconveniently overlap.

I think con.colour could similarly be made an aesthetic: I can see people preferring colouring the connector rather than the text of the label.

thomasp85 commented 2 years ago

It is unfortunately not as simple as it sounds, since label.colour (and con.colour) would need their own scales as well etc and it quickly becomes unwieldy.

How would people feel about a sentinel like 'inherit' that would indicate the colour coming from the main colour aesthetic? It is also not straightforward to make work, but is probably a better path to take

stragu commented 2 years ago

I think 'inherit' would be sufficient as I don't expect it would be particularly popular to colour the connectors and labels with a scale different to the ellipses'.

MattCowgill commented 2 years ago

I agree 'inherit' would do the job! Thank you @thomasp85