teunbrand
teunbrand

Reputation: 38063

Rotate strip text with ggtext

I'm trying to make a plot with a two layer strip. I want the first layer of strips to have a horizontal text orientation and the second layer to have a vertical text orientation.

In the example below, I want the strip layers that say 'horizontal' to be horizontal and I want '1999' and '2008' to remain vertical.

library(ggplot2)
library(ggtext)
library(glue)

df <- mpg
df$outer <- "horizontal"

p <- ggplot(df, aes(displ, cty)) +
  geom_point() +
  theme(
    strip.text.y.left = element_markdown()
  )

p + facet_grid(
  outer + year ~ ., 
  switch = "y"
)

The ggtext package is great, because it allows us to use ggtext::element_markdown() to conditionally format layers of a strip with html tags, such as in the example below:

p + facet_grid(
  glue("<span style = 'color:red'>{outer}</span>") + year ~ .,
  switch = "y"
)

Created on 2021-07-11 by the reprex package (v1.0.0)

Instead of applying a red color, is there an (HTML) tag I could use to make the text orientation horizontal? I'm not very fluent in HTML. After googling some options, I've tried the following spans with no success:

"<span style = 'transform:rotate(90deg)'>"
"<span style = 'text-orientation:sideways'>"

As a side-note: I know that I can edit the gtable of a plot to manually make edits to labels and whatnot. That is exactly what I'm trying not to do!

In addition to a solution to my problem, there are two other ways I'd consider my question answered.

* I'm aware of the paragraph in ggtext's readme that reads the following:

As a general rule, any Markdown, HTML, or CSS feature that isn’t shown in any of the ggtext or gridtext documentation likely doesn’t exist.

I'm fishing for a more explicit statement that says text cannot be rotated with tags.

Upvotes: 1

Views: 285

Answers (0)

Related Questions