55

Using base graphics in R, how can I add superscripts to axis labels, as one might want to when plotting latitude and longitude axes on a map.

Consider this example:

plot(-100:-50, 50:100, type="n", xlab="", ylab="", axes=FALSE)
axis(1, seq(-100, -50, 10), labels=paste(abs(seq(-100, -50, 10)), "o", "W", sep=""))
axis(2, seq(50, 100, 10), labels=paste(seq(50,100,10), "o", "N", sep=""))
box()

Produces a nice frame around a map. It would be even nicer to make the degree symbol superscript.

This can usually be done in other plotting functions such as mtext() and text() using expression(paste(...)) or substitute() but how to do it in this case?

digitalmaps
  • 2,885
  • 3
  • 22
  • 28

4 Answers4

49

It works the same way for axes: parse(text='70^o*N') will raise the o as a superscript (the *N is to make sure the N doesn't get raised too).

labelsX=parse(text=paste(abs(seq(-100, -50, 10)), "^o ", "*W", sep=""))
labelsY=parse(text=paste(seq(50,100,10), "^o ", "*N", sep=""))
plot(-100:-50, 50:100, type="n", xlab="", ylab="", axes=FALSE)
axis(1, seq(-100, -50, 10), labels=labelsX)
axis(2, seq(50, 100, 10), labels=labelsY)
box()
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
42

This is a quick example

plot(rnorm(30), xlab = expression(paste("4"^"th")))
Alex
  • 4,030
  • 8
  • 40
  • 62
  • 1
    How would you write something not in superscript at the end, as in "4th moment" without superscripting the "moment"? – Pertinax Jul 17 '17 at 14:17
  • 2
    @TheThunderChimp Sorry for the very late reply. Maybe you've already figured out how to do this, but you can do this: `plot(rnorm(30), xlab = expression(paste("4"^"th", " moment")))` – Alex Aug 31 '17 at 16:43
  • I have the same puzzle....where do I put the "" in this instance to have the -1 (only) in superscript? i.e..... + ylab = expression (paste("Ba:Ca concentration (μmol:mol^-1)") – Angela Russell Oct 27 '21 at 05:15
4

@The Thunder Chimp You can split text in such a way that some sections are affected by super(or sub) script and others aren't through the use of *. For your example, with splitting the word "moment" from "4th" -

plot(rnorm(30), xlab = expression('4'^th*'moment'))
Stedy
  • 7,359
  • 14
  • 57
  • 77
R Ramsay
  • 130
  • 7
3

The other option in this particular case would be to type the degree symbol: ˚

R seems to handle it fine. Type Option-k on a Mac to get it. Not sure about other platforms.

Stewart Macdonald
  • 2,062
  • 24
  • 27
  • This does not work for other symbols like Unicode superscript 7 (http://www.fileformat.info/info/unicode/char/2077/index.htm) – Pertinax Jul 17 '17 at 14:12