首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ggpmisc和`dev=“tikz”中回归方程的“圆”或“`sprintf`”函数

ggpmisc和`dev=“tikz”中回归方程的“圆”或“`sprintf`”函数
EN

Stack Overflow用户
提问于 2016-05-15 19:38:33
回答 2查看 936关注 0票数 4

如何使用roundsprintf函数控制回归方程中的数字显示?在使用dev="tikz" =~"时,我也想不出如何使用~"

代码语言:javascript
复制
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()

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-22 20:49:46

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

代码语言:javascript
复制
\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

票数 2
EN

Stack Overflow用户

发布于 2016-05-22 18:27:00

Myaseen208,

下面是用.tex创建ggpmisc::stat_poly_eq()输出的问题的解决方法。我能够确认,您目前无法将stat_poly_eq()"hat(Y)~=~"library(tikzDevice)组合起来,以创建latex .tex输出。不过,我提供了一个解决方案,以便在过渡期间创建正确的.tex输出。

ggpmisc包的创建者佩德罗·阿法罗非常友好地接受了对ggpmisc::stat_poly_eq()的增强请求。根据所要求的错误报告归档并引用如下。

代码示例:

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

代码语言:javascript
复制
# 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}}; Line、646693

代码语言:javascript
复制
# 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")

Tex测试框架:

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

注:RStudio是一个用于编译R.

代码语言:javascript
复制
\documentclass{article}

\usepackage{tikz}

\begin{document}

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

\end{document}

结果:

交替解

另一种选择可能是使用geom_text(),这种方法的缺点是您必须自己编写一个回归线方程函数。这一点在您上一篇文章中讨论过:Adding Regression Line Equation and R2 on graph

如果你需要一个详细的解决方案与geom_text,然后平我。另一种选择是用我所做的ggpmisc提交一个bug报告,看看作者是否已经或能够解决。

Bug报告:eq-fails-when-used-with

我希望上面的内容能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37242863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档