使用简单漂亮的代码在绘图中插入公式

Jon*_*ein 1 symbols r

Gnu R以非常奇怪的方式发布公式和符号.它经常在这里讨论并在R helppage中提到?plotmath.对于任何曾经LaTeX为R中的简单公式编写代码的人看起来都不可读并且写错了.

是否有更好的方法来注释公式?有这样的函数tex2r("x_2")会生成奇怪的代码吗?

编辑:

我正在寻找一个没有TikZdevice的解决方案,因为TikZdevice仍然非常脆弱,并且printoout看起来并不完全相同.

Jos*_*ien 7

使用tikzDevice包(目前仅可从CRAN存档获得),您可以使用直接的LaTeX标记来注释您的图.(包装附带一个漂亮的小插图,可以让你起床和跑步).

以下示例直接从此页面中提取,该页面还显示其生成的图形:

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)
Run Code Online (Sandbox Code Playgroud)