wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

Behavior of `hjust = c(0, 1)` in `ggtext::element_markdown()` is different from that of `ggplot2::element_text()` #116

Open balthasars opened 4 months ago

balthasars commented 4 months ago

I'm trying to profit from ggtext's more generous behavior of vectorized input to element_text() to plot multiple subtitles on the same line (see f.e. here).

I'm wondering about the behavior of hjust = c(0, 1) in element_markdown() however, that deviates from the ggplot2-native element_text() function:

library(ggplot2)
gg <- ggplot(data = mtcars, aes(x = disp, y = mpg)) +
  geom_line() +
  labs(subtitle = c("left-aligned subtitle", "right-aligned subtitle"))

# ggplot2 version
gg +
  theme(
    plot.subtitle = element_text(hjust = c(0, 1)) 
  )
#> Warning: Vectorized input to `element_text()` is not officially supported.
#> ℹ Results may be unexpected or may change in future versions of ggplot2.


# ggtext
# how I was expecting it to work
gg +
  theme(
    plot.subtitle = ggtext::element_markdown(hjust = c(0, 1))
  )

# how it actually worked
gg +
  theme(
    plot.subtitle = ggtext::element_markdown(hjust = c(1, 5))
  )

Created on 2024-05-14 with reprex v2.1.0

Thanks in advance for any pointers! :)

balthasars commented 4 months ago

I'd be happy do a PR and improve the documentation @bwiernik if hjust = c(1, 5) is how it should work.

Also had a quick look at the source code (also for richtext_grob()) from gridtext but it wasn't immediately clear to me what the arguments should be to get the vectorized input working as demonstrated above,