R 中的图中未处理类似乳胶的代码(未显示希腊字母等)

Latex-like codes were not processed (Greek letters etc. were not shown) in plot in R

(给出可重现代码)我正在研究 Ugarte2016 的 "Probability and Statistics with R" 2E。以下代码在 R 中是 运行 但未处理类似 Latex 的代码。 "$...$"里面的代码好像没有处理。下面提供的代码来自本书的作者。不知何故似乎有问题。可能是什么问题?

######### Chapter 12 #############
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
################ Figure 12.1 ###############    
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", xlim = c(0, 12), ylim = c(-2, 12),  xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),  labels = c("$E(Y|x_1) = \beta_0 + \beta_1x_1$",  "$E(Y|x_1) = \beta_0 + \beta_1x_1$", "$E(Y|x_1) = \beta_0 + \beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \beta_0 + \beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)

代码结果:

书中图片:

使用软件包 latex2exp:

######### Chapter 12 #############
library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
library(latex2exp)
################ Figure 12.1 ###############    
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", xlim = c(0, 12), ylim = c(-2, 12),  xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), labels = TeX(c("$x_1$", "$x_2$", "$x_3$")))
axis(side =2, at =c(1, 4, 10),  labels = TeX(c("$E(Y|x_1) = \beta_0 + \beta_1x_1$",  "$E(Y|x_1) = \beta_0 + \beta_1x_1$", "$E(Y|x_1) = \beta_0 + \beta_1x_1$") ))
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, TeX("$E(Y|x) = \beta_0 + \beta_1x$"))
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)

本书中的所有图表都是创建的 运行 knitr 选项 dev = "tikz"...专门针对有问题的图表:

<<c12slrModFIG, echo = FALSE, dev = "tikz", crop = TRUE, fig.align = 'center', results = 'hide', fig.height = 5, fig.width = 7, out.width='0.95\linewidth', warning = FALSE>>=

三地盘的解决方案使用latex2exp::TeX。有一种方案,保留原代码,完全不用latex2exp::TeX

当我联系本书的作者时,他们慷慨地发送了一个代码,并指定他们使用 tikzDeviceknitr 来生成图表。作为 knitr/tkizDevice 的新手,我找到了一种获取图像的方法,就像书中一样(图中的斜体 LateX 字符);我相信一定有更好的方法:

tikzDeviceAndKnitr.Rnw 文件放在 R 的工作目录中(可以通过 getwd() 找到它)。

tikzDeviceAndKnitr.Rnw:

<<PASWR2fCh12S1, echo=FALSE, dev="tikz", crop=TRUE, fig.align='center', results='hide', fig.height=5, fig.width=7, out.width='0.95\linewidth', warning=FALSE>>=
library(tikzDevice)
tikz('tikzDeviceAndKnitr.tex', standAlone=TRUE, width=5, height=5)
opar <- par(no.readonly = TRUE)
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", 
     xlim = c(0, 12), ylim = c(-2, 12),
     xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), 
     labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),
     labels = c("$E(Y|x_1) = \beta_0 + \beta_1x_1$",
                "$E(Y|x_1) = \beta_0 + \beta_1x_1$",
                "$E(Y|x_1) = \beta_0 + \beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \beta_0 + \beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
dev.off()
tools::texi2dvi('tikzDeviceAndKnitr.tex',pdf=T)
system(paste(getOption('pdfviewer'), 'tikzDeviceAndKnitr.pdf'))
@

在Windows的MikTeX中,安装tikzpgf相关的包。

加载 R 中的库和 knit 相关的 .Rnw 文件:

library(PASWR2); library(ggplot2); library(car); library(scatterplot3d)
library(gridExtra); library(multcomp); library(leaps); library(MASS)
library(latex2exp); library(knitr);library(tikzDevice);library(tools)
library(evaluate); library(markdown)


knit("tikzDeviceAndKnitr.Rnw") # The solution ended.

本书作者对我的回复是:
是的....tikzDeviceknitr 一起使用。完整代码如下:

\begin{figure}[!ht]
<<c12slrModFIG, echo = FALSE, dev = "tikz", crop = TRUE, fig.align = 'center', results = 'hide', fig.height = 5, fig.width = 7, out.width='0.95\linewidth', warning = FALSE>>=
opar <- par(no.readonly = TRUE) # copy of current settings
par(mar=c(2, 14, 2, 1), las = 1)
DF <- data.frame(x = c(1, 4, 9), y = c(1, 4, 9))
plot(y~x, data = DF, xaxt = "n", yaxt = "n", 
     xlim = c(0, 12), ylim = c(-2, 12),
     xlab = "", ylab = "", type = "n")
abline(lm(y~x, data = DF), lwd = 2)
axis(side =1, at =c(1, 4, 10), 
     labels = c("$x_1$", "$x_2$", "$x_3$"))
axis(side =2, at =c(1, 4, 10),
     labels = c("$E(Y|x_1) = \beta_0 + \beta_1x_1$",
                "$E(Y|x_1) = \beta_0 + \beta_1x_1$",
                "$E(Y|x_1) = \beta_0 + \beta_1x_1$") )
segments(1, -2, 1, 2.5, lty = "dashed")
segments(0, 1, 1 + 0.75, 1, lty = "dashed")
segments(4, -2, 4, 5.5, lty = "dashed")
segments(0, 4, 4 + 0.75, 4, lty = "dashed")
segments(10, -2, 10, 11.5, lty = "dashed")
segments(0, 10, 10 + 0.75, 10, lty = "dashed")
ys <- seq(-1.5, 1.5, length = 200)
xs <- dnorm(ys, 0, 0.5)
lines(xs + 1, ys + 1, type = "l",lwd = 2)
lines(xs + 4, ys + 4, type = "l",lwd = 2)
lines(xs + 10, ys + 10, type = "l",lwd = 2)
text(7.8, 5.5, "$E(Y|x) = \beta_0 + \beta_1x$")
arrows(8, 5.7, 7, 7, length = 0.1, lwd = 2)
par(opar)
@
\caption{Graphical representation of simple linear regression model 
depicting the distribution of $Y$ given x \label{SLRgraph}}
\end{figure}