Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend keys #112

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Legend keys #112

wants to merge 5 commits into from

Conversation

teunbrand
Copy link

This PR aims to fix #111.

Briefly, it adds a new function: draw_key_richtext() that can draw legend keys with formatted text. As a demonstration, we can render a similar reprex to the one given in #111:

library(ggplot2) # dev version
devtools::load_all("~/packages/ggtext/")
#> ℹ Loading ggtext

ggplot(iris, aes(Sepal.Width, Sepal.Length, colour = Species)) +
  geom_point(key_glyph = 'richtext') +
  scale_colour_discrete(
    labels = NULL,
    guide = guide_legend(override.aes = list(
      label = paste0(
        "<i><span style='color:black'>Iris</span> ", 
        c("setosa", "versicolor", "virginica"), "</i>"
      ),
      size = 11 / .pt, hjust = 0, label.colour = NA
    ))
  )

Created on 2023-12-16 with reprex v2.0.2

There are a few questions/remarks I have about how to proceed:

  • As a remark, the function was designed with Custom key glyph sizes tidyverse/ggplot2#5465 in mind.
  • Should this be the default key for geom_richtext() and geom_textbox()?
  • The visual test only makes sense with the development version of ggplot2.
  • I didn't see any actual snapshots of the visual tests in the testthat folder. Should I omit the svg file that the included snapshot test generates?

Thanks for reading!

R/draw-keys.R Outdated
Comment on lines 70 to 83
# Calculate offsets that account for textbox size
xoffset <- x[1] * (1 - just$hjust) + x[2] * just$hjust
yoffset <- y[1] * (1 - just$vjust) + y[2] * just$vjust

# We apply offsets to the grob's viewport so that textbox is remains within
# the bounds of the key area
grob <- editGrob(
grob,
vp = viewport(
x = unit(0.5, "npc") - unit(xoffset, "pt"),
y = unit(0.5, "npc") - unit(yoffset, "pt")
)
)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These steps are necessary to ensure that the grobs stay within their designated area when the angle is not 0.
Here is an example if I omit these lines:

@clauswilke
Copy link
Collaborator

I think this can be the default key glyph if it's backwards compatible. I don't know what the status of visual tests is. Haven't looked into it in a long time. I had tons of issues with visual tests (as they would always break with any minor update in the rendering stack), so I've mostly given up on them.

Also, looks like the CI framework is outdated and needs to be fixed before tests pass.

@teunbrand teunbrand mentioned this pull request Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request / offer: legend keys with markdown formatting
2 participants