In prior builds of R/R-Studio I've used, when applying a regression formula to a ggplot, I would get a graph with the regression equation properly rendered. However, now that I've switched to R v3.5.3, I'm getting extra characters in the regression line. I've modified a prior question (Adding Regression Line Equation and R2 on SEPARATE LINES graph) as an example:
library(ggplot2)
set.seed(5)
df <- data.frame(x = c(1:50))
df$y <- df$x + rnorm(50, sd=5)
lm_eqn <- function(df){
m <- lm(y~x, df)
eq <- substitute(italic(hat(y)) == a + b %.% italic(x)*","~~italic(r)^2~"="~r2,
list(a = format(coef(m)[1], digits=3),
b = format(coef(m)[2], digits=3),
r2 = format(summary(m)$r.squared, digits=3)))
as.character(as.expression(eq))}
ggplot(data=df, aes(x=x, y=y))+
geom_smooth(method="lm", se=FALSE, color="black", formula=y~x)+
geom_point()+
geom_text(x=10, y=50, label=lm_eqn(df), parse=TRUE)
I expect the regression line text to be
y^=-0.162+1.02·x, r²=0.886
However, what shows up is
y^=c(-0.162)+c(1.02)·x, r²=0.886
Is there a way to remove the c
and ()
, which did not show in the previous ggplot, or is this a bug?