简体   繁体   中英

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

(reproducible code given) I am studying Ugarte2016's "Probability and Statistics with R" 2E. The following code is run in R but Latex-like code is not processed. It seems that the code inside "$...$" is not processed. The code supplied below was from the authors of the book. There seems a problem somehow. What could be the problem?

######### 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)

The code result: 在此处输入图片说明

The image in the book: 在此处输入图片说明

Use package 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>>=

The solution of sandipan uses latex2exp::TeX . There is a solution that keeps the original code and does not use latex2exp::TeX at all.

When I contacted the authors of the book, they generously sent a code and specified that they used tikzDevice and knitr to produce the graphs. Being novice to both knitr / tkizDevice , I found a way to obtain the image just as in the book (italic LateX'ed chars on the plot); I am sure there must be a better approach:

The tikzDeviceAndKnitr.Rnw file is put in R's working directory (one may find it via 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'))
@

In MikTeX of Windows, install packages related with tikz and pgf .

Load the libraries in R and knit the related .Rnw file:

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.

The book's author's reply to me is:
Yes.... tikzDevice is used with knitr . The complete code looks like:

\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}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM