Reduce spacing of frac in R expression for ggplot legend

150 Views Asked by At

I am trying to reduce the space between the numerator, denominator, and fraction line in R expression. Here is my MWE generating a very ugly legend text for the fraction:

tibble(
  t = seq(0,60,.1),
  osc = cos(log2(1+t)*pi+pi/4)
) %>% 
  ggplot(aes(t, osc))+
  geom_line(aes(color = 'red'))+
  scale_color_manual(values = 'red', labels = expression(cos(log[2](t+1)*pi+frac(pi,4))+10))+
  theme(
    legend.position = 'bottom'
  )

The fraction introduces huge white space between the fraction line and numerator and denominator. Is there anyway to reduce this?

1

There are 1 best solutions below

3
user2554330 On

I don't think there's a way to control spacing in plotmath, but you could use paste(pi,"/",4) instead of frac(pi, 4):

Before:

enter image description here

After:

enter image description here