rnotation

why scientific notation leaves an extra character "1" after e in R?


An extra character of "1" is printing on the figure. Why so and how to remove this?

MWE:

data <- ChickWeight

par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)

data.lm <- lm(weight ~ Time, data = data)

plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (gms)", xlab = "Time (days)", ylim = c(25, 375), xlim=c(0,22))

abline(data.lm, col="blue", lty = 2, lwd = 2)

a <- round(data.lm$coefficients[[1]], 2)
b <- round(data.lm$coefficients[[2]], 2)

# err <- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # its not working leaving a "1" at the end of the equation. No idea, why?

err <- round(mean(data.lm$residuals),17)
# equation of the line : 
eq <- paste("Weight =",a,"+",b,"\u00D7 Time",err,cex=1)

mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=0.8)

enter image description here


Solution

  • You could use the cex once in your mtext like this:

    data <- ChickWeight
    
    par(mar = c(4,4,0.5,0.5) + 0.1, mgp = c(2.75,1,0), las = 1, cex = 1.2)
    
    data.lm <- lm(weight ~ Time, data = data)
    
    plot(weight ~ Time, data = data, col = "magenta", pch = 19, ylab = "Weight (gms)", xlab = "Time (days)", ylim = c(25, 375), xlim=c(0,22))
    
    abline(data.lm, col="blue", lty = 2, lwd = 2)
    
    a <- round(data.lm$coefficients[[1]], 2)
    b <- round(data.lm$coefficients[[2]], 2)
    
    # err <- format(mean(data.lm$residuals), scientific = TRUE, digits = 3) # its not working leaving a "1" at the end of the equation. No idea, why?
    
    err <- round(mean(data.lm$residuals),17)
    # equation of the line : 
    eq <- paste("Weight =",a,"+",b,"\u00D7 Time",err)
    
    mtext(paste(eq),side=3,line=-1.1,padj=-0.25,adj=0.05,cex=1)
    

    Created on 2023-05-17 with reprex v2.0.2