Search code examples
rplotmath

how to add symbols or signs like ≤ or ≠ to a legend or axis in R


I want to add the symbol in to a legend in R as in

..."≤1",col="gray"...

but when I run the script it creates and = symbol as if I have typed

..."=1",col="gray"...

Is there a way I can tell R I want that ≤ symbol?

And to help anyone with a similar problem; is there a list of commands that can be used to tell R how to add unusual characters?


Solution

  • Thanks to @agstudy for pointing me in the right direction, took a little tweaking but got there with this:

       legend(...expression(""<="1.0"),col="gray"...)
    

    More symbols can be found in the using ?plotmath (see here) and are implemented using expression()

    Here is an example:

    enter image description here

    x <- 0:64/64
    y <- sin(3*pi*x)
    plot(x, y, type= "l", col= "blue",
         main= expression("How to add the symbol"<="to a legend"))
    points(x, y, pch= 1, bg= "white")
    legend(.4,1, expression(""<= "1.0"), pch= 1, pt.bg= "white", lty= 1, col= "blue")