Use superscripts in R axis labels

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

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?

回答1:

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() 


回答2:

This is a quick example

plot(rnorm(30), xlab = expression(paste("4"^"th"))) 


回答3:

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



回答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')) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!