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

类似乳胶的代码未在R中的绘图中处理(未显示希腊字母等),r,plot,latex,non-ascii-characters,R,Plot,Latex,Non Ascii Characters,(给出了可复制代码)我正在研究Ugarte2016的“概率和统计与R”2E。以下代码在R中运行,但不处理类似Latex的代码。似乎未处理“$…$”中的代码。下面提供的代码来自本书的作者。似乎有个问题。有什么问题吗 ######### Chapter 12 ############# library(PASWR2); library(ggplot2); library(car); library(scatterplot3d) library(gridExtra); library(multcomp

(给出了可复制代码)我正在研究Ugarte2016的“概率和统计与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)
第12章#############
图书馆(PASWR2);图书馆(GG2);图书馆(汽车);库(散点图3D)
图书馆(额外);图书馆(multcomp);图书馆(飞跃);图书馆(弥撒)
################图12.1
opar使用最新包装EXP:

######### 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)
第12章#############
图书馆(PASWR2);图书馆(GG2);图书馆(汽车);库(散点图3D)
图书馆(额外);图书馆(multcomp);图书馆(飞跃);图书馆(弥撒)
图书馆(latex2exp)
################图12.1
opar使用最新包装EXP:

######### 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)
第12章#############
图书馆(PASWR2);图书馆(GG2);图书馆(汽车);库(散点图3D)
图书馆(额外);图书馆(multcomp);图书馆(飞跃);图书馆(弥撒)
图书馆(latex2exp)
################图12.1

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

本书中的所有图形都是运行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>>=
=

sandipan的解决方案使用
latex2exp::TeX
。有一种解决方案保留原始代码,并且根本不使用
latex2exp::TeX

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

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

TikzDevice和Knitr.Rnw:

这本书的作者给我的答复是:
是….
tikzDevice
knitr
一起使用。完整的代码如下所示:

\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}
\begin{figure}[!ht]
=

oparsandipan的解决方案使用
latex2exp::TeX
。有一种解决方案保留原始代码,并且根本不使用
latex2exp::TeX

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

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

TikzDevice和Knitr.Rnw:

这本书的作者给我的答复是:
是….
tikzDevice
knitr
一起使用。完整的代码如下所示:

\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}
\begin{figure}[!ht]
=

opar可能他们使用了tikzDevice?根据您的建议,我安装并制作了
库(tikzDevice)
。然后再次运行代码。它出现在
“R图形:设备”
窗口中。我无法成功使它出现在
tikzDevice
设备中。因此,无法确定问题是否源于未使用
tikzDevice
设备。您需要实际使用
tikz()
设备,并通过最新处理。他们可能使用tikzDevice?根据您的建议,我安装并制作了
库(tikzDevice)
。然后再次运行代码。它出现在
“R图形:设备”
窗口中。我无法成功使它出现在
tikzDevice
设备中。因此,无法确定问题是否源于未使用
tikzDevice
设备。您需要实际使用
tikz()
设备,并通过LaTeXIt对其进行处理。我安装了您指定的软件包,并按照您的建议制作了
库(latex2exp)
。然后,在重新运行代码时,情节与书中一样出现。万分感谢……它成功了。我安装了您指定的软件包,并按照您的建议制作了
库(latex2exp)
。然后,在重新运行代码时,情节与书中一样出现。万分感谢。。。