How can I plot my R Squared value on my scatterplot using R?

后端 未结 2 1691
旧时难觅i
旧时难觅i 2020-12-05 07:56

This seems a simple question, so I hope its a simple answer. I am plotting my points and fitting a linear model, which I can do OK. I then want to plot some summary statisti

相关标签:
2条回答
  • 2020-12-05 08:15

    The text function places text into the current plot, it is one option for adding the r-squared value to a plot. Also look at the grconvertX and grconvertY functions for ways to find the location to place the text.

    The corner.label and emptyspace functions in the plotrix package may also help.

    0 讨论(0)
  • 2020-12-05 08:22

    You can abuse legend() because it has the handy logical placement:

    R> DF <- data.frame(VAR1=rnorm(100), VAR2=rnorm(100))
    R> with(DF, plot(VAR1, VAR2))
    R> abline(fit <- lm(VAR2 ~ VAR1, data=DF), col='red')
    R> legend("topright", bty="n", legend=paste("R2 is", 
    +         format(summary(fit)$adj.r.squared, digits=4)))
    

    Here bty="n" suppresses the box, and you need format() to shorten the display. Other text() is good, as are arguments main= and sub= to plot().

    0 讨论(0)
提交回复
热议问题