4

I want to add a unit of m^2 m^-2 (but as an expression) next to a variable using gganimate. For example the following gives me my desired output when I do it using only ggplot2:

library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + 
        geom_point() +
        labs(title = "Hp" ~ m^2 ~ m^-2)

However, when I do the following using gganimate I do not get a changing value of Hp, but {closest_state}

library(gganimate)
ggplot(mtcars, aes(wt, mpg)) + 
        geom_point() + 
        transition_states(hp) +
        labs(title = expression('Hp = {closest_state}' ~ m^2 ~ m^-2))

How could I solve this?

bird
  • 2,938
  • 1
  • 6
  • 27
  • When I run your `gganimate` code I get an error `Error: Provided file does not exist` – Ronak Shah Apr 15 '21 at 01:14
  • @RonakShah can you please check with the updated code? I updated a few things in the `gganimate` code (the older code still works for me though). – bird Apr 15 '21 at 06:12
  • Do you have to pass the string as `expression` ? What if you pass it as string directly? Something like `labs(title = 'Hp = {closest_state} m²')` – Ronak Shah Apr 15 '21 at 14:43
  • 1
    @RonakShah I would strongly prefer passing it as an expression. Because this is just a simple example, in reality I have to write physical formulas and units. Btw, how did you write `m²` like that directly in R? – bird Apr 15 '21 at 15:01
  • 1
    I just found `m²` somewhere on the internet and copy pasted it in R. – Ronak Shah Apr 15 '21 at 15:15

1 Answers1

7

You could use ggtext::element_markdown() which allows a limited subset of Markdown format.
This is enough for exponents and indices as in your example, but unfortunately Latex math isn't yet available.

library(ggtext)
library(gganimate)

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() + 
  transition_states(hp) +
  labs(title = "Hp : {closest_state} m^2  m^-2")+
  theme(plot.title = element_markdown())

enter image description here

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • 1
    very nice. Thanks. See how this bounty goes but you're already on a good position :) - on the other hand, OP still needs to see if this also works with their maybe more complicated expressions. – tjebo Apr 22 '21 at 20:28
  • 1
    @tjebo thanks for you comment. See my edit : unfortunately Latex $math doesn' work yet :-( – Waldi Apr 22 '21 at 20:58
  • 1
    FWIW I've raised an issue / feature request on GitHub https://github.com/thomasp85/gganimate/issues/439. I have continued trying with all kinds of combinations to come up with a way to correctly parse this to no avail. Curious to hear what the package maintainer says about this. – tjebo Apr 26 '21 at 06:51
  • @Waldi 's method (using `ggtext`) worked perfectly for some of the units I used. But it does not work when things get slightly complicated. Although I have accepted @Waldi 's answer (because it does solve the specific problem example I have originally posted) I am still looking for other methods that will work for all mathematical expression. – bird Apr 26 '21 at 07:27