52

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 ?

plannapus
  • 18,529
  • 4
  • 72
  • 94
Robert Long
  • 5,722
  • 5
  • 29
  • 50

4 Answers4

64

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.

otsaw
  • 1,034
  • 8
  • 13
  • 7
    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. – MichaelChirico Mar 28 '15 at 19:33
  • 8
    @MichaelChirico: Solution: unless you have compelling reason to use `pdf`, use `cairo_pdf` instead. – otsaw Mar 30 '15 at 06:43
  • can you give an example of what such a compelling reason might be? – MichaelChirico Mar 30 '15 at 12:35
  • @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? – Vincent Jun 13 '18 at 13:44
  • Works within a call to `paste`, so that's nice! – Nova Mar 04 '19 at 19:30
  • [This wikipedia site lists all mathematical operators](https://en.wikipedia.org/wiki/Mathematical_operators_and_symbols_in_Unicode)- some of the more common ones are listed at the bottom of the site! – tjebo Aug 09 '19 at 10:27
  • And [this blog](https://www.andrewheiss.com/blog/2017/09/27/working-with-r-cairo-graphics-custom-fonts-and-ggplot/) gives helpful advice how to use cairo with `ggsave` – tjebo Aug 09 '19 at 10:43
  • how can we do lower that or equal sign? – Amer Dec 07 '20 at 00:45
  • FYI this also works for the `forestplot` package, for anyone who's just been googling a similarissue! – logjammin Jun 02 '22 at 05:19
22

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

stefan
  • 90,330
  • 6
  • 25
  • 51
mnel
  • 113,303
  • 27
  • 265
  • 254
13
plot(5, ylab=expression("T ">="5"))

enter image description here

Ven Yao
  • 3,680
  • 2
  • 27
  • 42
3

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.

mikey
  • 1,066
  • 9
  • 17