Displaying a greater than or equal sign

70,637

Solution 1

An alternative to using expressions is Unicode characters, in this case Unicode Character 'GREATER-THAN OR EQUAL TO' (U+2265). Copying @mnel's example

.d <- data.frame(a = letters[1:6], y = 1:6)

ggplot(.d, aes(x=a,y=y)) + geom_point() + 
    scale_x_discrete(labels = c(letters[1:5], "\u2265 80"))

Unicode is a good alternative if you have trouble remembering the complicated expression syntax or if you need linebreaks, which expressions don't allow. As a downside, whether specific Unicode characters work at all depends on your graphics device and font of choice.

Solution 2

You can pass an expression (including phantom(...) to fake a leading >= within the label argument to scale_x_discrete(...)

for example

 .d <- data.frame(a = letters[1:6], y = 1:6)

 ggplot(.d, aes(x=a,y=y)) + geom_point() + 
    scale_x_discrete(labels = c(letters[1:5], expression(phantom(x) >=80))

enter image description here

See ?plotmath for more details on creating mathematical expressions and this related SO question and answer

Solution 3

plot(5, ylab=expression("T ">="5"))

enter image description here

Solution 4

You can use

expression("">=80)

So your full axis label like would look like:

scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",expression("">=80),"All")) +

I have had trouble exporting plots when using unicode, but the expression function is more consistent.

Share:
70,637

Related videos on Youtube

Robert Long
Author by

Robert Long

Data Scientist, Statistician, Developer. Functional Programmer. These days I use Haskell, R, SQL and the Snowflake Data Cloud platform. In days gone by I was a C programmer, then a C++ programmer, and have dabbled in Python, Rust, and a few others. Then a Linux sysadmin working with bash, sed, awk etc. Also formerly an academic (Statistical Epidemiology at Leeds University UK). Currently a Data Engineer/Scientist. https://www.linkedin.com/in/roblongleeds/ https://scholar.google.co.uk/citations?user=pcZNzRkAAAAJ

Updated on July 09, 2022

Comments

  • Robert Long
    Robert Long almost 2 years

    I have a plot which is generated thus:

    ggplot(dt.2, aes(x=AgeGroup, y=Prevalence)) + 
        geom_errorbar(aes(ymin=lower, ymax=upper), colour="black", width=.2) +
        geom_point(size=2, colour="Red")
    

    I control the x axis labels like this:

    scale_x_discrete(labels=c("0-29","30-49","50-64","65-79",">80","All")) +
    

    This works but I need to change the ">80" label to "≥80".

    However "≥80" is displayed as "=80".

    How can I display the greater than or equal sign ?

    • Robert Long
      Robert Long over 11 years
      @JanDvorak, unfortunately not. It's for publication, so it needs to look as good as possible. I wanted to go with >79 but the senior authors specifically wants ≥80
    • plannapus
      plannapus over 11 years
      have you tried expression("">=80)? See ?plotmath.
    • Robert Long
      Robert Long over 11 years
      @plannapus - that does the trick nicely ! Thank you - if you make an answer, I will upvote and accept...
    • plannapus
      plannapus over 11 years
      It seems that @mnel was quicker than me :)
    • mnel
      mnel over 11 years
      I posted before I saw your comment (if that makes it better?)
    • plannapus
      plannapus over 11 years
      And I saw your answer just as I posted my comment!
  • MichaelChirico
    MichaelChirico about 9 years
    Problem: the pdf device does not support this character and prints ... instead. I'm not working in ggplot so maybe this isn't a concern in that package, but it's troublesome for saving from base R.
  • otsaw
    otsaw about 9 years
    @MichaelChirico: Solution: unless you have compelling reason to use pdf, use cairo_pdf instead.
  • MichaelChirico
    MichaelChirico about 9 years
    can you give an example of what such a compelling reason might be?
  • Vincent
    Vincent about 6 years
    @otsaw I have the same problem with the postscript device for producing eps. Is there a cairo_postscript or similiar solution that you know of?
  • Nova
    Nova over 5 years
    Works within a call to paste, so that's nice!
  • tjebo
    tjebo almost 5 years
    This wikipedia site lists all mathematical operators- some of the more common ones are listed at the bottom of the site!
  • tjebo
    tjebo almost 5 years
    And this blog gives helpful advice how to use cairo with ggsave
  • Amer
    Amer over 3 years
    how can we do lower that or equal sign?
  • logjammin
    logjammin about 2 years
    FYI this also works for the forestplot package, for anyone who's just been googling a similarissue!