Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ggplot2 plotmath: using bquote to add (<=) sign in label error

I’m trying to add a less than or equal sign "<=" label in my ggplot but keep getting the error:

Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y,  : 
  invalid mathematical annotation

Any suggestions on how I can fix this?

lower_limit <- 15

ggplot(mtcars, aes(wt, mpg)) + 
geom_point() + 
scale_y_continuous(limits = c(lower_limit, 35), 
breaks = c(lower_limit, 25, 35), 
labels = c(bquote(""<= .(lower_limit)), "25", "35"))

thank you in advance

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to pass an expression vector as labels, and your code gives a list. Use as.expression() to convert it:

library(ggplot2)

lower_limit <- 15

ggplot(mtcars, aes(wt, mpg)) + 
  geom_point() + 
  scale_y_continuous(limits = c(lower_limit, 35), 
                     breaks = c(lower_limit, 25, 35), 
                     labels = as.expression(c(bquote("" <= .(lower_limit)), "25", "35")))
#> Warning: Removed 5 rows containing missing values (`geom_point()`).

Created on 2023-07-03 with reprex v2.0.2

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading