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

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

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.

Category:r Views:1 Time:2010-09-21

Related post

• plotting a circle inside a square in R 2011-03-23

I am trying to make a simple illustration where a circle is plotted inside a square. I have used the rect() function from the grid package and the draw.circle() function from the plotrix package before so I thought this would be simple. But clearly I

• how to plot a x-y grid of e.g. squares with colours read from an array 2011-04-19

I have separate arrays of x and y coordinates, and a z-array of corresponding values. I wish to make a plot that has squares at each x and y coordinate that have colours set from the z array - something similar to . I have searched quite hard on goog

• PRoblems Running Core Plot Tuorial 2010-06-10

I am following the tutorial here about core plot here.... http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application I am getting errors with the following lines of code //SAYING INCOMPATIBLE TYPE FOR AURGUMENT 1 'setMajorInter

• Matlab choose random color for plotting 2010-07-12

I have 6 vectors which I want to plot. How I can make each plot with different color (random)? With the code below, the plot limited to one color for all six vectors. plot(x,y,'-rs','LineWidth',1,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',..

• Changing the shapes of points in scatter plot 2011-07-12

I have thousands of points to Plot on a JFreeChart scatter plot. The problem right now is that my program is plotting points with "squares", but I need some help on how to change the Shape of points from "squares" to "dots/circles". Any help would be

• Getting EXC_BAD_ACCESS when trying to compile with core plot 2011-08-09

As a beginner in objective C and xcode, I am trying to do a tutorial program that can be found there: http://www.switchonthecode.com/tutorials/using-core-plot-in-an-iphone-application I did some changes so it would finally compile, so here's my .h (p

• matplotlib problems plotting logged data and setting its x/y bounds 2012-01-17

I am using log plots as follows in matplotlib, roughly as follows. plt.scatter(x, y) # use log scales plt.gca().set_xscale('log') plt.gca().set_yscale('log') # set x,y limits plt.xlim([-1, 3]) plt.ylim([-1, 3]) The first problem is that without x,y l

• Python scatter plot. Size and style of the marker 2012-01-31

I have a set of data that I want to show as a scatter plot. I want each point to be plotted as a square of size dx. x = [0.5,0.1,0.3] y = [0.2,0.7,0.8] z = [10.,15.,12.] dx = [0.05,0.2,0.1] scatter(x,y,c=z,s=dx,marker='s') The problem is that the siz

• Line of best fit scatter plot 2010-01-08

I'm trying to do a scatter plot with a line of best fit in matlab, I can get a scatter plot using either scatter(x1,x2) or scatterplot(x1,x2) but the basic fitting option is shadowed out and lsline returns the error 'No allowed line types found. Noth

• Scatter plot of 10k record extracted from database 2012-01-04

I am trying to make a scatter plot in Python. I supposed it will be fairly simple but got stuck with understanding in scatterplot (x and y value) while plotting. ==My mission == I have database and more then 10k record (all float) till now and will i

• Scatter plot of handwritten digits 2012-01-20

I want to create a scatter plot of handwritten digits of 0 and 1 (http://yann.lecun.com/exdb/mnist/). I took 4 samples ie two 0's and two 1's. Each handwritten digits are having pixel values having dimension 1 cross 778 matrix. Now I want to do scatt

• Problem compiling project with CorePlot 2010-08-14

Today I tried to compile a project with CorePlot. All the time I get the following errors: ".objc_class_name_NSNotificationCenter", referenced from: [email protected][email protected][email protected] in libCorePlot.a(CPGraph.o) [email protected]__

• pairs() equivalent in ggplot2? 2010-09-17

Is it possible to plot a matrix of scatter plots with ggplot2, using ggplot's nice features like mapping additional factors to color, shape etc. and adding smoother? I am thinking about something similar to pairs(). --------------Solutions-----------

• Matplotlib 2D polygon in 3D axes 2010-10-31

I'm using matplotlib to plot some data as 2D curves in a 3D axes. I use the ax = axes3d.Axes3D() to define my axes and ax.plot(x, y, zs='z') to plot my 2D curves in the 3D space. Now I want to add something that looks like what is on this page http:/

• How to set equal scale's length in gnuplot 2011-03-08

Hi Is it possible in gnuplot to set scales so that length of scaleX and scaleY would be the same?? I tried different tricks (set size, set ratio etc) but even if the range of scaleX and scaleY is the same - the length of that scales are different. It

• what is the relationship between image edges and gradient? 2012-01-12

Is there anybody can help me interpret "Edge points may be located by the maxima of the module of the gradient, and the direction of edge contour is orthogonal to the direction of the gradient." --------------Solutions------------- Paul R has given y

• R - idiomatic way to deal with lists of data frames 2010-02-12

I have 30 runs of data, each stored in a separate CSV file, runi.csv, i = 0:29. Let's say I want to collect them all into a list. Best way I know how to do this is runs = list() for (i in 1:30) { runs[[i]] = read.csv(paste("run", i-1, ".csv")); } Now

• 2d geometry drawing tool 2010-08-12

I'm looking for some tool/library that is able to draw simple 2d geometries from text file or programatically. I already found List of interactive geometry software but that not quite what I'm looking for. I would prefer something more similar in usa

• scatterplot instead of a lineplot while using the plotfile function of matplotlib 2011-06-13

I know the general usage of plotfile: import matplotlib.pyplot as plt plt.plotfile(csvfile,sometuple) But this produces a line plot by default. I want a scatterplot. Is there some special argument that i need to pass to this method? I have already lo