2

Gnu R ships with a very odd way to note formulas and symbols. It is often discussed here and mentioned in the R helppage ?plotmath. For anyone who ever wrote LaTeX the code for a simple formula in R looks unreadable and is errorprone to write.

Is there a better way to annotate with formulas? Is there a function like tex2r("x_2") that will generate the strange code?

edit:

I am looking for a solution without TikZdevice, because TikZdevice is still very fragile and the printoout does not look exactly the same.

Community
  • 1
  • 1
Jonas Stein
  • 6,826
  • 7
  • 40
  • 72
  • [Another answer](http://stackoverflow.com/questions/10156417/subscripts-in-plots-in-r) is using `expression()` to add math subscripts to plots. Example: `plot(1,1, main=expression('title'[2]))`. You might also want to look at [this question on mathematical symbols in ggplot](http://stackoverflow.com/questions/15125628/putting-mathematical-symbols-and-subscripts-mixed-with-regular-letters-in-r-ggpl), some answers can also be applied to standard R plots. Formatting with tilde starts to look like latex math: `expression(Value~is~sigma~R^{2}==0.6)`. – Paul Rougieux Aug 13 '15 at 07:37

2 Answers2

7

With the tikzDevice package (currently available only from the CRAN archive) you can use straight-up LaTeX markup to annotate your plots. (The package comes with a beautiful vignette that'll get you up and running).

The example below was lifted directly from this page, which also displays the figure it produces:

require(tikzDevice)

tikz('normal.tex', standAlone = TRUE, width=5, height=5)

# Normal distribution curve
x <- seq(-4.5,4.5,length.out=100)
y <- dnorm(x)

# Integration points
xi <- seq(-2,2,length.out=30)
yi <- dnorm(xi)

# plot the curve
plot(x,y,type='l',col='blue',ylab='$p(x)$',xlab='$x$')
# plot the panels
lines(xi,yi,type='s')
lines(range(xi),c(0,0))
lines(xi,yi,type='h')

#Add some equations as labels
title(main="$p(x)=\\frac{1}{\\sqrt{2\\pi}}e^{-\\frac{x^2}{2}}$")
int <- integrate(dnorm,min(xi),max(xi),subdivisions=length(xi))
text(2.8, 0.3, paste("\\small$\\displaystyle\\int_{", min(xi),
    "}^{", max(xi), "}p(x)dx\\approx", round(int[['value']],3),
    '$', sep=''))

#Close the device
dev.off()

# Compile the tex file
tools::texi2dvi('normal.tex',pdf=T)
Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • The tikzdevice is a nice idea, but i think it is still beta software. A plot looks very different, and the files are very large (if i get one) – Jonas Stein Oct 30 '12 at 20:57
  • @JonasStein -- Quite true. This is just the closest thing to an answer to your question that I've seen (and it is something I've often wished for myself). – Josh O'Brien Oct 30 '12 at 21:01
1

I just found a package that does exactly what OP was asking for: latex2exp and in there the fuction TeX.

E.g.:

library(latex2exp)
library(berryFunctions)
set.seed(1)
milk <- data.frame(Datum = as.Date(rep(seq(17500, 18460, by = 30), each = 30), origin = "1970-01-01"),
                   Milch = abs(rnorm(990, mean = 20, sd = 8)))
X11(width = 12, height = 7)
par(mar = c(3,5.5,3,1))
with(milk, plot(Datum, Milch, pch = "-", cex=1.5, col = rgb(red = 0, green = 0.5, blue = 0.5, alpha = 0.7),
                xaxt = "n", xlab = "", ylab = "",
                main = "Milchleistung am Wolkenhof"))
title(ylab = TeX("Milchmenge $\\,$ $\\left[\\frac{\\mathrm{kg}}{\\mathrm{Kuh} \\cdot \\mathrm{Tag}}\\right]$"), line = 2.5)
monthAxis()

yields:

milch Edit: Now there is a space between "Milchmenge" and the left bracket "[", but I didn't want to upload a new picture therefore.

mattu
  • 318
  • 2
  • 16