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
>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