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.
这似乎是一个简单的问题,所以我希望它是一个简单的答案。我正在绘制我的点并拟合线性模型,我可以做到。然后,我想在图上绘制一些汇总统计数据,例如R Squared值。我似乎只能在命令行中获得R Squared值。任何建议;我需要查看ggplot或其他什么吗?提前致谢。
#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))
2 个解决方案
#1
29
You can abuse legend()
because it has the handy logical placement:
您可以滥用legend(),因为它具有方便的逻辑布局:
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()
.
这里bty =“n”禁止显示框,你需要format()来缩短显示。其他text()也很好,参数main =和sub = to plot()。
#2
7
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.
文本功能将文本放入当前图中,它是将r平方值添加到图中的一个选项。另请查看grconvertX和grconvertY函数,了解查找放置文本位置的方法。
The corner.label
and emptyspace
functions in the plotrix
package may also help.
plotrix包中的corner.label和emptyspace函数也可能有所帮助。
#1
29
You can abuse legend()
because it has the handy logical placement:
您可以滥用legend(),因为它具有方便的逻辑布局:
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()
.
这里bty =“n”禁止显示框,你需要format()来缩短显示。其他text()也很好,参数main =和sub = to plot()。
#2
7
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.
文本功能将文本放入当前图中,它是将r平方值添加到图中的一个选项。另请查看grconvertX和grconvertY函数,了解查找放置文本位置的方法。
The corner.label
and emptyspace
functions in the plotrix
package may also help.
plotrix包中的corner.label和emptyspace函数也可能有所帮助。