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

Marquee guide #28

Merged
merged 9 commits into from
Jun 10, 2024
Merged

Marquee guide #28

merged 9 commits into from
Jun 10, 2024

Conversation

teunbrand
Copy link
Contributor

Hi Thomas,

This is my proposal for a marquee guide.
Instead of going into details, I'll render the examples which are hopefully somewhat self-expanatory.
Please let me know if you'd wish to change some things.

library(ggplot2)
library(marquee)
# A standard plot
base <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()

# Using key glyphs
base + aes(shape = drv) +
  scale_shape_discrete(
    # Same as using <<1>>, <<2>> and <<3>>,
    # or ![](1), ![](2) and ![](3)
    # or ![](4), ![](f) and ![](r)
    name = "Cars with four wheel <<4>>, forward <<f>> or reverse <<r>> drive.",
    guide = "marquee"
  )

# Recolouring text
base <- base +
  aes(colour = drv) +
  labs(
    colour = "Cars with {.4 four wheel}, {.f forward} or {.r reverse} drive."
  )
base + guides(colour = "marquee")

# Adjust display of labels
st <- style(weight = "bold", italic = TRUE, background = NA)
base + guides(colour = guide_marquee(style = st))

# Using background instead of text colour by setting it to NULL
st <- style(color = "black", background = NULL)
base + guides(colour = guide_marquee(style = st))

# Customising style of each label through style sets
# Note: tag names must be universal per `vctrs::vec_as_names` and
# prefixed with `lab_`.
st <- classic_style()
st <- modify_style(st, tag = "lab_f", background = NULL, color = "black")
st <- modify_style(st, tag = "lab_r", border_size = trbl(1),
                   color = "black", background = NA)
base + guides(colour = guide_marquee(style = st))

# Alternatively:
base + guides(colour = "marquee") +
  theme(plot.subtitle = element_marquee(style = st))

# Splicing in labels
base + aes(colour = class) +
  labs(colour = "Cars including !!2 and !!6 vehicles") +
  guides(colour = "marquee")

# Using automatic detection
base + aes(colour = class) +
  labs(colour = "Cars including suv and minivan vehicles") +
  guides(colour = guide_marquee(detect = TRUE))

# Automatic detection is not always a good idea
base +
  labs(colour = "Kerfuffles 4 the riffraff from frankfurt") +
  guides(colour = guide_marquee(detect = TRUE))

Created on 2024-06-06 with reprex v2.1.0

@thomasp85
Copy link
Member

This looks very promising. Some thoughts:

  • I think the automatic detection can focus on whole words so as to avoid the last example. I can't see when it would ever be useful not want it to detect full words
  • breaks can include spaces while marquee classes cannot. How is this handled?
  • How can this be combined with a regular legend? If possible at all?
  • It's not clear how the splicing in of labels happens. What are 2 and 6 referring to?

@teunbrand
Copy link
Contributor Author

I think the automatic detection can focus on whole words so as to avoid the last example. I can't see when it would ever be useful not want it to detect full words

I'll look into finding the right regex incantation for this.

breaks can include spaces while marquee classes cannot. How is this handled?

Through the repair = "universal" in vec_as_names() for class names. It ensures the tag is both syntactic and unique.

x <- "I'm displaying spaces."
tolower(vctrs::vec_as_names(x, repair = "universal", quiet = TRUE))
#> [1] "i.m.displaying.spaces."

Created on 2024-06-07 with reprex v2.1.0

How can this be combined with a regular legend? If possible at all?

It cannot, I don't think you can have >1 guide per aesthetic in ggplot2.
You can hack it of course by using a guide for a different aesthetic with the same mapping and using override.aes to emulate the original aesthetic.

It's not clear how the splicing in of labels happens. What are 2 and 6 referring to?

Using !!n will insert the nth label, so !!2 and !!6 will insert the 2nd and 6th label.
It would be the same as "Cars including !!compact and !!subcompact vehicles". Perhaps the example should mix the two ways to showcase them both. This pattern is included in the 'text formatting' section of the docs.

@thomasp85
Copy link
Member

Ok, let's merge this in and take it for a spin. Can you add a topic to the pkgdown site so that doesn't fail

@teunbrand
Copy link
Contributor Author

Gotcha

@thomasp85 thomasp85 merged commit 5c65bc9 into r-lib:main Jun 10, 2024
11 checks passed
@thomasp85
Copy link
Member

Thank you

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.

2 participants