Home > Software design >  ggplot2, getting a % symbol in subscript within axis labels
ggplot2, getting a % symbol in subscript within axis labels

Time:02-23

I'm trying to get a % symbol in the axis as subscript. Typically, using expression and square brakets [] you can get subscripts. But symbols don't seem to be parsing correctly as I get an error. Does anyone know how to fix or a work around? For instance, what would I do if I want % in subscript below:

library(tidyverse)
  
iris %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width))   
  geom_point(aes(colour = Species))   
  geom_smooth(method = loess)   
  labs(y = expression(Blah[1*d]*"%"))

When the % sign is moved in the brackets

iris %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width))   
  geom_point(aes(colour = Species))   
  geom_smooth(method = loess)   
  labs(y = expression(Blah[1d%]))

the following error message appears:

Error: unexpected symbol in: " geom_smooth(method = loess)
labs(y = expression(Blah[1d"

CodePudding user response:

Moving the % sign in the square brackets and using quotation marks"" removes the error message

iris %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width))   
  geom_point(aes(colour = Species))   
  geom_smooth(method = loess)   
  labs(y = expression(Blah["1d%"]))

and yields this plot:

enter image description here

  • Related