Search code examples
rggplot2latex

Adding a maths notation for matrix in ggplot


I was wondering if there is a way to write an expression for a matrix, for e.g., a matrix 2 by 2 matrix and display it in a plot like the way I could simply do in the following cases for equation: $Y_{ij} = 3+2.5t_{ij} +\epsilon_{ij}$ in R.

curve(dnorm, from = -3, to = 3, n = 1000, main = "Normal Probability Density Function")
text(-2, 0.3, expression(f(x) == paste(frac(1, sqrt(2 * pi * sigma^2)),
                                        " ", e^{
                                            frac(-(x - mu)^2, 2 * sigma^2)
                                       })), cex = 1.2)
ggplot() +
 ggtitle(expression(paste(Y[ij] == beta[0]+beta[1]*t[ij] + b[0][i] + epsilon[ij])))

Solution

  • Here's a base plot effort. There is no matrix plotmath facility but you can create one with a bit of fiddling using atop and supersized "pipe" (|) characters (or if you wish to have supersize arcuate fences just choose open-paren and close-paren)

    plot(1,1,col="transparent")
    text(1.2, 0.8, expression( atop(1~2~3,4~5~6) ) )
     text(1.15, .8, labels="|",cex=3 ) #Need to figure out what the coords should be
     text(1.25, .8, labels="|",cex=3 ) and adjust the other side's big-pipe
    
    
    

    enter image description here