Mathematical notation in R plots

In this post I will show some examples of annotating an R plot with mathematical notation using expressions. Specifically, I will use pdf’s (probability density functions). A pdf is not complete without a description of its support, so I will include that as well with each pdf. Your primary resource for plotting mathematical expressions in R: type ?plotmath at the R console. Additionally, demo(plotmath) is very helpful. Let’s take a look at some expression objects I have prepared for our plot.

The right side of the expression assignments are going to be a bit messy. Not much can be done about that. Apologies in advance for the code not fitting in the code box on this page. Best to copy and paste into Notepad++ or another editor.

expr.norm <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,sqrt(2*pi*sigma^scriptscriptstyle("2")))~e^{frac(-1,2*sigma^{scriptscriptstyle("2")})*(x-mu)^scriptscriptstyle("2")})
					~~~~displaystyle(list(paste(-infinity<x) <infinity, paste(-infinity<mu) <infinity, paste(0<sigma^scriptscriptstyle("2")) <infinity))
					)))
					
expr.unif <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,b-a)
					~~~~displaystyle(paste(-infinity<paste(a<=paste(x<=b))) <infinity))
					)))
					
expr.t <- expression(italic(paste(displaystyle(f(x)~"="~frac(Gamma~bgroup("(",frac(nu+1,2),")"),sqrt(nu*pi)~Gamma~bgroup("(",frac(nu,2),")"))~bgroup("(",1+frac(x^2,nu),")")^{-frac(nu+1,2)})
					~~~~displaystyle(list(paste(-infinity<x) <infinity, nu > 0))
					)))
					
expr.F <- expression(italic(paste(displaystyle(f(x)~"="~frac(Gamma~bgroup("(",frac(nu[1]+nu[2],2),")"),Gamma~bgroup("(",frac(nu[1],2),")")~Gamma~bgroup("(",frac(nu[2],2),")"))
					~bgroup("(",frac(nu[1],nu[2]),")")^{frac(nu[1],2)}~frac(x^{frac(nu[1],2)-1},bgroup("(",1+frac(d[1],d[2])*x,")")^{frac(d[1]+d[2],2)}))
					~~~~displaystyle(paste(0<=x) <infinity~and~list(d[1],d[2]) > 0)
					)))
					
expr.gam <- expression(italic(paste(displaystyle(f(x)~"="~frac(beta^alpha,Gamma(alpha))*x^{alpha-1}*e^{-beta*x})
					~~~~displaystyle(list(paste(0<x) <infinity, paste(0<alpha) <infinity, paste(0<beta) <infinity))
					)))
					
expr.exp <- expression(italic(paste(displaystyle(f(x)~"="~lambda*e^{-lambda*x})
					~~~~displaystyle(list(paste(0<=x) <infinity,lambda>0))
					)))
					
expr.chisq <- expression(italic(paste(frac(1,2^{frac(nu,2)}*Gamma~bgroup("(",frac(nu,2),")"))*x^{frac(nu,2)-1}*e^{-frac(x,2)}
					~~~~displaystyle(list(paste(0<=x) <infinity, nu %in% bold(N)))
					)))
					
expr.lnorm <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,x*sigma*sqrt(2*pi))~e^{-frac((log(x)-mu)^2,2*sigma^2)})
					~~~~displaystyle(list(paste(0<x) <infinity, paste(-infinity<log(mu)) <infinity, paste(0<sigma^scriptscriptstyle("2")) <infinity))
					)))
					
expr.beta <- expression(italic(paste(displaystyle(f(x)~"="~frac(Gamma(alpha+beta),Gamma(alpha)*Gamma(beta))*x^{alpha-1}*(1-x)^{beta-1})
					~~~~displaystyle(list(paste(0<=x) <=1, paste(0<alpha) <infinity, paste(0<beta) <infinity))
					)))

The point here is to provide examples, so I won’t explain what all of the commands in the expressions above do. That is for you to explore using the above, in addition to all the broken down plotmath examples. It is important to note some things. First, my use of italicis just personal preference. Also, paste behaves differently in the context of plotting expressions than how paste works in general. Sometimes I use the ~ character to explicitly put additional spacing between terms. Finally, among the supports, note the small hoop I have to jump through to show a < x < b. You will get an error if you attempt to make more than just a left and right side for an inequality statement.

Here is some code for displaying all of these on a plot:

win.graph(8,12)
par(mar=c(1,1,2,1))
plot(0,0,type="n",xlim=c(-1,1),ylim=c(1,9),axes=F,xlab="",ylab="",main="Adding pdf's to an R plot")
for(i in 1:9) text(0,i,get(ls()[ls()!="i"][10-i]),cex=1.2)
mtext(c("Beta","Chi Sq","Exp","F","Gamma","Log-Norm","Normal","t","Unif"),2,at=9:1)

And here is the plot:

Entry_14_plotmath1

That’s all there is to it. Explore plotmath, play around with these expressions, and use them at your convenience. Make your own and see what you can do. A well-annotated plot is a nice thing when don’t right.

This entry was posted by Matt Leonawicz.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: