wilkelab / ggtext

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

`element_markdown()` not working with `axis.title` #59

Open maia-sh opened 3 years ago

maia-sh commented 3 years ago

Thank you for this wonderful package! I am trying to add colors with html to title and axis labels. plot.title = element_markdown() works, however axis.title = element_markdown() does not. Based on the element_markdown() document I believe this should work for all text.


library(dplyr)
library(ggplot2)
library(ggtext)

mtcars %>% 
  ggplot(aes(x = mpg, y = cyl)) +
  geom_point() +
  labs(
    title = "I <span style='color:blue'>love</span> cars",
    x = "Look at this <span style='color:red'>mpg</span>!"
  ) +
  theme(
    plot.title = element_markdown(),
    axis.title = element_markdown()
  )

Created on 2021-02-10 by the reprex package (v0.3.0)

delabj commented 3 years ago

It looks like you can solve this particular issue by specifying the axis you want to change like this.

library(dplyr)
library(ggplot2)
library(ggtext)

mtcars %>% 
  ggplot(aes(x = mpg, y = cyl)) +
  geom_point() +
  labs(
    title = "I <span style='color:blue'>love</span> cars",
    x = "Look at this <span style='color:red'>mpg</span>!"
  ) +
  theme(
    plot.title = element_markdown(),
    axis.title.x = element_markdown()
  )

image