gpt4 book ai didi

r - 将 `round` 或 `sprintf` 函数用于 ggpmisc 和 `dev="tikz 中的回归方程"`

转载 作者:行者123 更新时间:2023-12-04 11:36:44 24 4
gpt4 key购买 nike

如何使用 round 控制回归方程中的数字显示或 sprintf功能?我也不知道如何使用 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()

enter image description here

最佳答案

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}

enter image description here

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

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

关于r - 将 `round` 或 `sprintf` 函数用于 ggpmisc 和 `dev="tikz 中的回归方程"`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37242863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com