I have this code for plotting x,y and showing the regression line and fit statistics:
for (i in unique(Data$SeasAlltxt)){
print (i)
subdata1 <- subset(Data, SeasAlltxt == i)
for (j in unique(Data$ALSCIDtxtall)){
subdata2 <- subset(subdata1, ALSCIDtxtall == j)
plot(subdata2$PeakToGone, subdata2$NO3_AVG, xlim = c(0, 70))
abline(fit <- lm(NO3_AVG~PeakToGone, data = subdata2))
rmse <- round(sqrt(mean(resid(fit)^2)), 2)
r2 <- round(summary(fit)$r.squared, 3)
ycoord <- max(subdata2$NO3_AVG)
eqn <- bquote(~~ r^2 == .(r2) * "," ~~ RMSE == .(rmse))
text(40, ycoord, eqn, pos = 4)
}
}
This works for most of plots and shows the text on the plot. However, some plots do not show the text. The axes of these plots encompass the values for the coordinates of origin (40, ycoord) for the text, so I am confused as to why the text does not appear. I tried changing these coordinates to be in the center of the plots, and the text still does not appear on the same plots.
Any thoughts?