Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use superscripts in R axis labels

Tags:

r

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?

like image 352
digitalmaps Avatar asked May 17 '12 00:05

digitalmaps


People also ask

How do you superscript in R labels?

Superscript is “started” by the caret ^ character. Anything after ^ is superscript. The superscript continues until you use a * or ~ character. If you want more text as superscript, then enclose it in quotes.

How do you superscript an axis title?

Right-click on the highlighted text and go to “Format axis title”. 5. Select the “Font” tab. To make characters into superscripts or subscripts, check the appropriate box in the lower left portion of the dialogue box.

How do I customize axis labels in R?

Key ggplot2 R functionsp + xlab(“New X axis label”): Change the X axis label. p + ylab(“New Y axis label”): Change the Y axis label. p + labs(x = “New X axis label”, y = “New Y axis label”): Change both x and y axis labels.


2 Answers

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()
like image 145
mathematical.coffee Avatar answered Oct 16 '22 20:10

mathematical.coffee


This is a quick example

plot(rnorm(30), xlab = expression(paste("4"^"th")))
like image 40
Alex Avatar answered Oct 16 '22 21:10

Alex