How can I control the numeric display in Regression equation by using round
or sprintf
function? I also could not figure out how to use dev="tikz"
when using 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()
1) The code below answers the
dev="tikz"
part of the question if used with the 'ggpmisc' (version >= 0.2.9)Thanks for suggesting this enhancement, I will surely also find a use for it myself!
2) Answer to the
round
andsprintf
part of the question. You cannot useround
orsprintf
to change the number of digits,stat_poly_eq
currently usessignif
with three significant digits as argument applied to the whole vector of coefficients. If you want full control then you could use another statistics,stat_fit_glance
, that is also inggpmisc
(>= 0.2.8), which usesbroom:glance
internally. It is much more flexible, but you will have to take care of all the formating by yourself within the call toaes
. At the moment there is one catch,broom::glance
does not seem to work correctly withpoly
, you will need to explicitly write the polynomial equation to pass as argument toformula
.