Mathematical notation in R plots 2

In this post I will show some more examples of annotating an R plot with mathematical notation using expressions. In part one, I used pdf’s (probability density functions). See that post for more information on adding mathematical annotations to R plots in general. Here I have expanded the list of pdf’s, and I have added some pmf’s (probability mass functions) for discrete distributions.

As mentioned in the last post, the right side of the expression assignments are going to be messy, and apologies in advance for the code not fitting in the code box below. You should just copy and paste it into Notepad++ or another text editor.

# Continuous distributions:
cexpr.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, atop(paste(-infinity<mu) <infinity, paste(0<sigma^scriptscriptstyle("2")) <infinity)))
					)))

cexpr.unif <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,b-a)
					~~~~displaystyle(paste(-infinity<paste(a<=paste(x<=b))) <infinity))
					)))

cexpr.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(atop(paste(-infinity<x) <infinity, nu > 0))
					)))

cexpr.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(atop(paste(0<=x) <infinity,list(d[1],d[2])~"="~list(1,2,...)))
					)))

cexpr.gam <- expression(italic(paste(displaystyle(f(x)~"="~frac(beta^alpha,Gamma(alpha))*x^{alpha-1}*e^{-beta*x})
					~~~~displaystyle(list(paste(0<x) <infinity, atop(paste(0<alpha) <infinity, paste(0<beta) <infinity)))
					)))

cexpr.exp <- expression(italic(paste(displaystyle(f(x)~"="~lambda*e^{-lambda*x})
					~~~~displaystyle(atop(paste(0<=x) <infinity,lambda>0))
					)))

cexpr.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(atop(paste(0<=x) <infinity, nu~"="~list(1,2,...)))
					)))

cexpr.lognorm <- 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, atop(paste(-infinity<log(mu)) <infinity, paste(0<sigma^scriptscriptstyle("2")) <infinity)))
					)))

cexpr.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, atop(paste(0<alpha) <infinity, paste(0<beta) <infinity)))
					)))

cexpr.cauchy <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,pi*sigma)~frac(1,1+bgroup("(",frac(x-theta,sigma),")")^2))
					~~~~displaystyle(list(paste(-infinity<x) <infinity, atop(paste(-infinity<theta) <infinity, sigma > 0)))
					)))

cexpr.lap <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,2*sigma)~e^{-frac(abs(x-mu),sigma)})
					~~~~displaystyle(list(paste(-infinity<x) <infinity, atop(paste(-infinity<mu) <infinity, sigma > 0)))
					)))

cexpr.logi <- expression(italic(paste(displaystyle(f(x)~"="~frac(1,beta)~frac(e^{-frac(x-mu,beta)},bgroup("[",1+e^{-frac(x-mu,beta)},"]")^2))
					~~~~displaystyle(list(paste(-infinity<x) <infinity, atop(paste(-infinity<mu) <infinity, beta > 0)))
					)))

cexpr.pareto <- expression(italic(paste(displaystyle(f(x)~"="~frac(beta*alpha^beta,x^{beta+1}))
					~~~~displaystyle(atop(paste(alpha<x) <infinity, list(alpha,beta) > 0))
					)))

cexpr.weib <- expression(italic(paste(displaystyle(f(x)~"="~frac(k,lambda)~bgroup("(",frac(x,lambda),")")^{k-1}*e^(-x/lambda)^k)
					~~~~displaystyle(atop(paste(0<=x) <infinity, list(k,lambda) > 0))
					)))

# Discrete distributions:
dexpr.bern <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~p^x*(1-p)^{1-x})
					~~~~displaystyle(atop(x~"="~list(0,1), paste(0<=p)<=1))
					)))

dexpr.bin <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~bgroup("(",atop(n,x),")")~p^x*(1-p)^{n-x})
					~~~~displaystyle(atop(x~"="~list(0,1,...,n), paste(0<=p)<=1))
					)))

dexpr.dunif <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~frac(1,N))
					~~~~displaystyle(x~"="~list(1,2,...,N))
					)))

dexpr.geom <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~p*(1-p)^x)
					~~~~displaystyle(atop(x~"="~list(1,2,...), paste(0<=p)<=1))
					)))

dexpr.hgeom <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~frac(bgroup("(",atop(M,x),")")~bgroup("(",atop(N-M,K-x),")"),bgroup("(",atop(N,K),")")))
					~~~~displaystyle(list(x~"="~list(0,1,...,K), atop(paste(M-(N-K)<=x)<=M, list(N,M,K)>=0)))
					)))

dexpr.nbin <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~frac(Gamma(x+n),Gamma(n)*x*"!")~p^r*(1-p)^x)
					~~~~displaystyle(atop(x~"="~list(0,1,...), paste(0<=p)<=1))
					)))

dexpr.poi <- expression(italic(paste(displaystyle(P(X~"="~x)~"="~frac(e^{-lambda}*lambda^x,x*"!"))
					~~~~displaystyle(atop(x~"="~list(0,1,...), paste(0<=lambda)<infinity))
					)))

As before, these are just examples; this is not a tutorial. Here is some code for displaying all of these on a multiframe plot. Note that I am assuming you have no other objects in your workspace except what is defined in the code in this post. I used my exact png settings this time instead of just making the plot in the R session so you can reproduce the same file shown below.

n <- 21
png("C:/tmp/plotmathtest3.png",width=3200,height=2400,res=240)
par(mar=c(1,1,2,0),oma=c(1,1,2,0),mfrow=c(1,3))

plot(0,0,type="n",xlim=c(-1,1),ylim=c(2*n/3+1,n),axes=F,xlab="",ylab="",main="")
for(i in (2*n/3+1):n) text(0,i,get(ls()[ls()!="i" & ls()!="n"][n+2*n/3+1-i]),cex=1.2)
mtext(c("Bernoulli","Binomial","Discrete Uniform","Geometric","Hypergeometric","Negative Binomial","Poisson"),2,at=n:(2*n/3+1),cex=1)

plot(0,0,type="n",xlim=c(-1,1),ylim=c(1,n/3),axes=F,xlab="",ylab="",main="")
for(i in 1:(n/3)) text(0,i,get(ls()[ls()!="i" & ls()!="n"][(n/3)+1-i]),cex=1.2)
mtext(c("Beta","Cauchy","Chi-Squared","Exponential","F","Gamma","Laplace"),2,at=(n/3):1,cex=1)

plot(0,0,type="n",xlim=c(-1,1),ylim=c(n/3+1,2*n/3),axes=F,xlab="",ylab="",main="")
for(i in (n/3+1):(2*n/3)) text(0,i,get(ls()[ls()!="i" & ls()!="n"][2*n/3+(n/3)+1-i]),cex=1.2)
mtext(c("Logistic","Log-Normal","Normal","Pareto","t","Uniform","Weibull"),2,at=(2*n/3):(n/3+1),cex=1)

mtext("Adding pmf's and pdf's to an R plot",outer=T,cex=1.5)
dev.off()

And here is the plot:
R_plotmath_distributions

And there you have it! Note that distributions may be parameterized differently. The parameterizations of a few of these distributions are not the ones I always use, and may differ from what you will see in some texts, but I’ve tried here to use parameterizations used by R, or common R packages in the case where base R does not provide the typical d*, p*, q*, and r* functions we know and love. Hopefully I achieved what I set out to do and did not miss something somewhere.

All of these distributions are available in base R with the exception of the Laplace and Pareto distributions, both of which I access via the VGAM package.

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: