4

如何使用roundorsprintf函数控制回归方程中的数值显示?dev="tikz"使用eq.with.lhs = "hat(Y)~=时,我也无法弄清楚如何使用~"

library(ggplot2)
library(ggpmisc)

# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x, 
                      y, 
                      group = c("A", "B"), 
                      y2 = y * c(0.5,2),
                      block = c("a", "a", "b", "b"))

str(my.data)

# plot
ggplot(data = my.data, mapping=aes(x = x, y = y2, colour = group)) +
        geom_point() +
        geom_smooth(method = "lm", se =  FALSE, formula = y ~ poly(x=x, degree = 2, raw = TRUE)) +
        stat_poly_eq(
                       mapping     = aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~"))
                     , data        = NULL
                     , geom        = "text"
                     , formula     = y ~ poly(x, 2, raw = TRUE)
                     , eq.with.lhs = "hat(Y)~`=`~"
                     , eq.x.rhs    = "X"
                     , label.x     = 0
                     , label.y     = 2e6
                     , vjust       = c(1.2, 0)
                     , position    = "identity"
                     , na.rm       = FALSE
                     , show.legend = FALSE
                     , inherit.aes = TRUE
                     , parse       = TRUE
                     ) +
        theme_bw()

在此处输入图像描述

4

2 回答 2

2

1)dev="tikz"如果与“ggpmisc”(版本> = 0.2.9)一起使用,下面的代码回答了问题的一部分

\documentclass{article}

\begin{document}

<<setup, include=FALSE, cache=FALSE>>=
library(knitr)
opts_chunk$set(fig.path = 'figure/pos-', fig.align = 'center', fig.show = 'hold',
               fig.width = 7, fig.height = 6, size = "footnotesize", dev="tikz")
@


<<>>=
library(ggplot2)
library(ggpmisc)
@

<<>>=
# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x,
                      y,
                      group = c("A", "B"),
                      y2 = y * c(0.5,2),
                      block = c("a", "a", "b", "b"))

str(my.data)
@

<<>>=
# plot
ggplot(data = my.data, mapping=aes(x = x, y = y2, colour = group)) +
  geom_point() +
  geom_smooth(method = "lm", se =  FALSE, 
              formula = y ~ poly(x=x, degree = 2, raw = TRUE)) +
  stat_poly_eq(
    mapping     = aes(label = paste("$", ..eq.label.., "$\\ \\ \\ $",
                       ..rr.label.., "$", sep = ""))
    , geom        = "text"
    , formula     = y ~ poly(x, 2, raw = TRUE)
    , eq.with.lhs = "\\hat{Y} = "
    , output.type = "LaTeX"
   ) +
  theme_bw()
@

\end{document}

在此处输入图像描述

感谢您提出此增强功能,我自己也一定会找到它的用途!

2)回答问题的roundsprintf部分。您不能使用roundsprintf更改位数,stat_poly_eq目前使用signif三个有效数字作为应用于整个系数向量的参数。如果您想要完全控制,那么您可以使用另一个统计数据,stat_fit_glance它也在ggpmisc(>= 0.2.8) 中,它在broom:glance内部使用。它更加灵活,但您必须在调用aes. 目前有一个问题,broom::glance似乎不能与 一起正常工作poly,您需要明确编写多项式方程以作为参数传递给formula

于 2016-05-22T20:49:46.880 回答
1

Myaseen208,

这是.tex使用ggpmisc::stat_poly_eq(). 我能够确认您目前无法将stat_poly_eq(), 和"hat(Y)~=~"library(tikzDevice)创建乳胶.tex输出。但是,我提供了一个解决方案来.tex在此期间创建正确的输出。

软件包的创建者 Pedro Aphaloggpmisc非常友好地接受了ggpmisc::stat_poly_eq(). 根据请求的错误报告提交并在下面引用。

代码示例:

以下代码将生成一个没有帽子符号的图形:

# Load required packages
requiredPackages <- requiredPackages <- c("ggplot2", "ggpmisc", "tikzDevice", "latex2exp")

# ipak - Check to see if the package is installed, if not install and then load...
ipak <- function(pkg)
{
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg))
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

ipak(requiredPackages)

# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x ^ 2 + x ^ 3) + rnorm(length(x), mean = 0, sd = mean(x ^ 3) / 4)
my.data <- data.frame(
  x, y,
  group = c("A", "B"),
  y2 = y * c(0.5, 2),
  block = c("a", "a", "b", "b")
)

# Define Formaula..
formulaDefined <- (y ~ (poly(x = x, degree = 2, raw = TRUE)))

gp <- ggplot(data = my.data, mapping = aes(x = x, y = y2, colour = group))
gp <- gp + geom_point()
gp <- gp + geom_smooth(method = "lm", se =  FALSE, formula = formulaDefined )
gp <- gp + stat_poly_eq(
  aes(label = paste(..eq.label.., "~~~", ..rr.label.., sep = "")),
#  eq.with.lhs = "italic(hat(y))~`=`~",
  formula     = formulaDefined,
  geom        = "text",
  label.x     = 0,
  label.y     = 2e6,
  vjust       = c(1.2, 0),
  position    = "identity",
  na.rm       = FALSE,
  show.legend = FALSE,
  inherit.aes = TRUE,
  parse       = TRUE)
gp <- gp + theme_bw()
gp

在此处输入图像描述

我们现在可以修改此代码及其tikz output以创建所需的结果:

Tikz 代码解决方案

第一步是修改代码以输出所需的.tex文件。完成此操作后,我们可以利用gsub().tex文件中找到所需的行并将其替换为{\itshape y};[{\^{y}};646693行]。

# Load required packages
requiredPackages <- requiredPackages <- c("ggplot2", "ggpmisc", "tikzDevice", "latex2exp")

# ipak - Check to see if the package is installed, if not install and then load...
ipak <- function(pkg)
{
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg))
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

ipak(requiredPackages)

# generate artificial data
set.seed(4321)
x <- 1:100
y <- (x + x ^ 2 + x ^ 3) + rnorm(length(x), mean = 0, sd = mean(x ^ 3) / 4)
my.data <- data.frame(
  x, y,
  group = c("A", "B"),
  y2 = y * c(0.5, 2),
  block = c("a", "a", "b", "b")
)

setwd("~/dev/stackoverflow/37242863")

texFile <- "./test2.tex"
# setup tex output file
tikz(file = texFile, width = 5.5, height = 5.5)

#Define Formaula..
formulaDefined <- (y ~ (poly(x = x, degree = 2, raw = TRUE)))

gp <- ggplot(data = my.data, mapping = aes(x = x, y = y2, colour = group))
gp <- gp + geom_point()
gp <- gp + geom_smooth(method = "lm", se =  FALSE, formula = formulaDefined )
gp <- gp + stat_poly_eq(
  aes(label = paste(..eq.label.., "~~~", ..rr.label.., sep = "")),
#  eq.with.lhs = "italic(hat(y))~`=`~",
  formula     = formulaDefined,
  geom        = "text",
  label.x     = 0,
  label.y     = 2e6,
  vjust       = c(1.2, 0),
  position    = "identity",
  na.rm       = FALSE,
  show.legend = FALSE,
  inherit.aes = TRUE,
  parse       = TRUE)
gp <- gp + theme_bw()
gp
dev.off()

## OK, now we can take the test.txt file and replace the relevant attributes to
## add the hat back to the y in the .tex output file...

texOutputFile <- readLines(texFile)
y <- gsub('itshape y', '^{y}', texOutputFile )
cat(y, file=texFile, sep="\n")

文本测试框架:

为了测试解决方案,我们可以创建一个小型乳胶测试工具。你可以在RStudio [ ]中加载这个文件t1.tex,然后编译它;它将test2.text通过前面提供的代码生成。

注意。RStudio 是用于从 R 编译的乳胶输出的绝佳平台。

\documentclass{article}

\usepackage{tikz}

\begin{document}

\begin{figure}[ht]
\input{test2.tex}
\caption{Sample output from tikzDevice 2}
\end{figure}

\end{document}

结果:

在此处输入图像描述

替代解决方案

另一种选择可能是使用geom_text(),这种方法的缺点是您必须自己编写回归线方程函数。这在您之前的帖子中讨论过:在图形上添加回归线方程和 R2

如果您需要 [with geom_text] 的详细解决方案,请联系我。另一种选择是使用 ggpmisc [由我完成] 提交错误报告,看看作者是否已经解决或可以解决。

错误报告:https ://bitbucket.org/aphalo/ggpmisc/issues/1/stat_poly_eq-fails-when-used-with

我希望以上内容有所帮助。

于 2016-05-22T18:27:00.240 回答