如何使用 R 在散点图上绘制 R 平方值? [英] How can I plot my R Squared value on my scatterplot using R?

查看:46
本文介绍了如何使用 R 在散点图上绘制 R 平方值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题,所以我希望它是一个简单的答案.我正在绘制我的点并拟合线性模型,我可以做到.然后,我还想在图上绘制一些汇总统计数据,例如 R 平方值.我似乎只能在命令行中获得 R Squared 值.任何建议;我需要查看 ggplot 或其他任何东西吗?提前致谢.

#是否情节情节(df $ VAR1,df $ VAR2)#添加行abline(lm(df$VAR2~df$VAR1), col="red")#在命令行上显示统计信息总结(lm(df$VAR2~df$VAR1))

解决方案

你可以滥用 legend() 因为它具有方便的逻辑位置:

R>DF <- 数据.frame(VAR1=rnorm(100), VAR2=rnorm(100))R>with(DF, plot(VAR1, VAR2))R>abline(fit <-lm(VAR2 ~ VAR1, data=DF), col='red')R>图例(右上角",bty =n",图例=粘贴(R2是",+ 格式(摘要(拟合)$adj.r.squared,数字=4)))

这里bty="n" 抑制框,需要format() 来缩短显示.其他 text() 很好,main=sub=plot() 的参数也是如此.

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 statistics, for example the R Squared value, on the plot also. I can only seem to get the R Squared value at the command line. Any advice; do I need to be looking at ggplot or anything else? Thanks in advance.

#Does the plot
plot(df$VAR1, df$VAR2)
#Adds the line
abline(lm(df$VAR2~df$VAR1), col="red")
#Shows stats on command line
summary(lm(df$VAR2~df$VAR1))

解决方案

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

这篇关于如何使用 R 在散点图上绘制 R 平方值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆