Skip to content

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

@balthasars

Description

@balthasars

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! :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions