Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 用简单漂亮的代码在绘图中插入公式_R_Symbols - Fatal编程技术网

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

R 用简单漂亮的代码在绘图中插入公式,r,symbols,R,Symbols,Gnu R以一种非常奇怪的方式来记录公式和符号。它经常出现在R帮助页面?plotmath中。对于任何曾经编写过LaTeX的人来说,R中简单公式的代码看起来不可读,而且容易出错 有没有更好的方法用公式进行注释?是否有类似于tex2r(“x_2”)的函数会生成奇怪的代码 编辑: 我正在寻找一种没有TikZdevice的解决方案,因为TikZdevice仍然非常脆弱,打印输出看起来也不完全一样。使用(当前仅从CRAN存档中提供)可以使用直接向上的LaTeX标记来注释绘图。(该软件包附带了一个美丽的小插

Gnu R以一种非常奇怪的方式来记录公式和符号。它经常出现在R帮助页面
?plotmath
中。对于任何曾经编写过
LaTeX
的人来说,R中简单公式的代码看起来不可读,而且容易出错

有没有更好的方法用公式进行注释?是否有类似于
tex2r(“x_2”)
的函数会生成奇怪的代码

编辑: 我正在寻找一种没有TikZdevice的解决方案,因为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)
require(tikzDevice)
tikz('normal.tex',standAlone=TRUE,宽度=5,高度=5)
#正态分布曲线

x我刚刚找到一个包,它完全符合OP的要求:
latex2exp
,其中包含函数
TeX

例如:

库(latex2exp)
图书馆(berryFunctions)
种子(1)

milk The tikzdevice是个好主意,但我认为它仍然是测试版软件。一个情节看起来非常不同,而且文件非常大(如果我有一个的话)@JonasStein——这是真的。这是我见过的最接近你问题的答案(这也是我经常希望自己得到的)。正在使用
expression()
向绘图添加数学下标。示例:
plot(1,1,main=expression('title'[2]))
。您可能还想看看,一些答案也可以应用于标准R图。用tilde格式化开始看起来像latex数学:
表达式(Value~is~sigma~R^{2}==0.6)
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()