CHOP-CGTInformatics / ggswim

Create swimmer plots with ggplot2
https://chop-cgtinformatics.github.io/ggswim/
Other
16 stars 3 forks source link

ggplot2 key_glyphs could be helpful at some point #26

Open ddsjoberg opened 7 months ago

ddsjoberg commented 7 months ago

https://ggplot2.tidyverse.org/reference/draw_key.html

ddsjoberg commented 7 months ago

Here's a blog post that illustrates how to create a custom key glyph. This has the potential to immensely simply the internals https://emilhvitfeldt.com/post/changing-glyph-in-ggplot2/

(Just making notes for the future...it doesn't sound like anyone has bandwidth at the moment)

ddsjoberg commented 7 months ago

The symbol used in the legend can be customized using the key_glyph argument. There are a bunch of pre-defined ones (example below). But I am curious if we could simply write a custom one one the fly.

For example, if we exported a new geom geom_marker(aes(...), marker = "\u1234") that could write a custom glyph function using the marker symbol passed by the user. These functions look like they are simple grob objects, so perhaps not too complicated (that said, everything with legends is complicated!).

library(tidyverse)

ggplot(mtcars) +
  geom_point(aes(x = hp, y = mpg, color = "Death"))


ggplot(mtcars) +
  geom_point(aes(x = hp, y = mpg, color = "Death"), key_glyph = draw_key_boxplot)

Created on 2024-02-23 with reprex v2.1.0

rsh52 commented 7 months ago

Yea I think this could work! I want to look into this and how the new guides updates can simplify the brute-force things I was trying to achieve originally.

You're envisioning that marker symbols get passed to glyphs, or that we expose key_glyphs as an option for users? I'm not sure how much I like having something in the legend that doesn't exactly appear in the plot:

image

Silly example, but there are no smileys to find in the plot itself.

ddsjoberg commented 7 months ago

oh users should never know about glyphs. for them, they just pass to us that they want this symbol/emoji at these points, and we take care of the rest.

rsh52 commented 7 months ago

oh users should never know about glyphs. for them, they just pass to us that they want this symbol/emoji at these points, and we take care of the rest.

Got it, makes sense to me! Hope to explore more in the next week or two.