Displaying a greater than or equal sign
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))
See ?plotmath
for more details on creating mathematical expressions and
this related SO question and answer
Solution 3
plot(5, ylab=expression("T ">="5"))
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.
Related videos on Youtube
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, 2022Comments
-
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 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 over 11 yearshave you tried
expression("">=80)
? See?plotmath
. -
Robert Long over 11 years@plannapus - that does the trick nicely ! Thank you - if you make an answer, I will upvote and accept...
-
plannapus over 11 yearsIt seems that @mnel was quicker than me :)
-
mnel over 11 yearsI posted before I saw your comment (if that makes it better?)
-
plannapus over 11 yearsAnd I saw your answer just as I posted my comment!
-
-
MichaelChirico about 9 yearsProblem: the
pdf
device does not support this character and prints ... instead. I'm not working inggplot
so maybe this isn't a concern in that package, but it's troublesome for saving from base R. -
otsaw about 9 years@MichaelChirico: Solution: unless you have compelling reason to use
pdf
, usecairo_pdf
instead. -
MichaelChirico about 9 yearscan you give an example of what such a compelling reason might be?
-
Vincent about 6 years@otsaw I have the same problem with the
postscript
device for producing eps. Is there acairo_postscript
or similiar solution that you know of? -
Nova over 5 yearsWorks within a call to
paste
, so that's nice! -
tjebo almost 5 yearsThis wikipedia site lists all mathematical operators- some of the more common ones are listed at the bottom of the site!
-
tjebo almost 5 yearsAnd this blog gives helpful advice how to use cairo with
ggsave
-
Amer over 3 yearshow can we do lower that or equal sign?
-
logjammin about 2 yearsFYI this also works for the
forestplot
package, for anyone who's just been googling a similarissue!