LoveDanny
LoveDanny

Reputation: 15

How to format ggplot `geom_text` with formula, getting unwanted "c(...)"

In my code below, I want to show the formula for a fit on my plot with geom_text, but I get unwanted c before the values of a and b, how do I prevent this?

enter image description here

p <- ggplot(data=Algae, aes(x=a254, y=DOC))+
      geom_point(color="blue",stat="identity") +
      geom_smooth(method="lm",se=FALSE,color="red",formula=y~x)

model.lm <- lm(DOC~a254, data=Algae)

l <- list(a=format(coef(model.lm)[1], digits=4),
        b=format(coef(model.lm)[2], digits=4),
        r2=format(summary(model.lm)$r.squared, digits=4),
        p=format(summary(model.lm)$coefficients[2,4], digits=4))

eq <- substitute(italic(DOC) == a - b %*% italic(a254)~","~italic(R)^2~"="~r2~",
                 "~italic(P)~"="~p, l)
p1 <- p + geom_text(aes(x =6, y = 0, label = as.character(as.expression(eq))), parse = TRUE)
p1

Upvotes: 0

Views: 3077

Answers (1)

jay.sf
jay.sf

Reputation: 72813

The reason for this is that you first format() your data into character format and then try to calculate with strings. You could solve the problem this way:

First, it is more convenient to transform your list into a data.frame, using:

d <- as.data.frame(l)

The values should be converted back to numeric, since you yet want to do arithmetics inside the formula:

d[] <- lapply(d, function(x) as.numeric(as.character(x)))

Then it should work fine:

eq <- substitute(italic(Sepal.Length) == a - b %*% italic(Petal.Length)~","~italic(R)^2~"="~r2~",
                 "~italic(P)~"="~p, d)
p + geom_text(aes(x =5, y = 0, label = as.character(as.expression(eq))), parse = TRUE)

You could also use annotate() to add the formula to the plot, which might look a little nicer:

p + annotate('text', 7, 4, 
             label=paste("italic(Sepal.Length)==", d$a, "~-~", d$b, "~x~", 
                         "~italic(Petal.Length)~';'~italic(r)^2==", d$r2, 
                         "~italic(P)==", d$p), 
             parse=TRUE, 
             hjust=1, size=3.5)

Yielding:

enter image description here


Data:

library(ggplot2)
p <- ggplot(data=iris, aes(x=Petal.Length, y=Sepal.Length)) +
  geom_point(color="blue", stat="identity") + 
  geom_smooth(method="lm", se=FALSE, color="red", formula=y~x)

model.lm <- lm(Sepal.Length ~ Petal.Length, data=iris)

l <- list(a=format(coef(model.lm)[1], digits=4), 
          b=format(coef(model.lm)[2], digits=4), 
          r2=format(summary(model.lm)$r.squared, digits=4), 
          p=format(summary(model.lm)$coefficients[2, 4], digits=4))

Upvotes: 1

Related Questions