Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use mapply on stat_poly_eq (i.e. equation and R-squared values) in ggplot2 R?

I am trying to add the R-squared equations, each with a different formula. I tried the mapply function modelling on a previous answer but nothing happens. There is no error, but not equation displayed either. I also want to plot the equation on one line, and the Rsquared in the next line, I don’t know where exactly to add the \n in the stat_poly_eq.

library(ggplot2)
library(ggpmisc)
set.seed(14)
df <- data.frame(
  var.test = c("T","T","T","T","M","M","M","M","A","A","A","A"),
  val.test = rnorm(12,8,5),
  x = c(1:12)
)

my.formula <- c(y~x + I(x^2), y~x, y~x + I(x^2))
ggplot(df, aes(x = x, y = val.test)) + 
  geom_point() +
  mapply(function(x, z) geom_smooth(method="glm", data=function(d) subset(d, var.test==z), formula = x, 
                                    method.args = list(family = "poisson"), color = "black" ), my.formula, c("A","M","T")) + facet_grid(.~var.test) + 
  mapply(function(x,z) stat_poly_eq(formula = x, aes(label = paste(..eq.label.., ..rr.label.., sep = "~~~")),  parse = TRUE, size = 2.5, col = "black", data=function(d) subset(d, var.test==z),my.formula, c("A","M","T")))

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 mapplys. 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)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading