1

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.

  • A link to some documentation that says it is not (yet) possible to do this with ggtext. Please post it as an answer with a small description so I can accept it, if this is the case. A post by ggtext's creator Claus O. Wilke commenting on this, is also fine.*

  • A code example where an attempt to use canonical HTML tags (besides the two I already tried) fails to rotate the text. I'd then know that someone with more knowledge than me about HTML tried and my question has no apparent solution.

* 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.

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Hi @teunbrand! Aren't the two README sentences right before the one you quote rather explicit? Perhaps I misunderstood your requirements. Cheers – Henrik Jul 11 '21 at 13:44
  • You're right of course that these sentences are rather discouraging to my quest, but I'm rather reluctant to accept them before I've tried all reasonable angles to circumvent them. I'm not familiar enough with HTML to define 'all reasonable angles', so that's why I've posted this question on SO. Maybe I'm a bit too stubborn and should just accept that it can't be done this way. – teunbrand Jul 11 '21 at 13:55
  • Sorry if I appeared negative. My comment only referred to the readme per se, not your overall quest to solve the problem. Which I fully support! – Henrik Jul 11 '21 at 14:07
  • No worries, I didn't interpret your comment as negative! It's a reasonable thing to point out :) – teunbrand Jul 11 '21 at 14:08

0 Answers0