I am trying to add the R-squared equations, each with a different formula. I tried the mapply function modelling on a p
CodePudding user response:
The issue with your code was a wrong closing paranthesis, i.e. you included my.formula
and c("A","M","T")
as arguments of stat_poly_eq
. That's why no labels were plotted as you looped over nothing.
Concerning your second question. TBMK you can't have a line break in a math expression. One approach to deal with that would be to add the equation and the R^2 via two separate stat_poly_eq
layers.
Additionally I simplified your code a bit. It's not necessary to have multiple mapply
s. One is sufficient. You could return multiple layers by wrapping them inside a list
.
library(ggplot2)
library(ggpmisc)
ggplot(df, aes(x = x, y = val.test))
geom_point()
mapply(function(x, z) {
data <- subset(df, var.test == z)
list(
geom_smooth(
method = "glm", data = data, formula = x,
method.args = list(family = "poisson"), color = "black"
),
stat_poly_eq(formula = x, aes(label = ..eq.label..),
parse = TRUE, size = 2.5, col = "black", data = data, vjust = -0.1),
stat_poly_eq(formula = x, aes(label = ..rr.label..),
parse = TRUE, size = 2.5, col = "black", data = data, vjust = 1.1)
)
}, my.formula, c("A", "M", "T"))
facet_grid(. ~ var.test)