5

I created scattergram using the plot() function in R.

Is there any possibility to draw on this graph?

I would like to add a straight line and get parameters of it, but in my opinion abline() can be inconvenient (I would like to draw many lines and choose one which will be most proper).

How can I accomplish this task?

Iterator
  • 20,250
  • 12
  • 75
  • 111
Mateusz
  • 71
  • 1
  • 2
  • I made a few edits for clarity, but I'm still not getting this: do you want to draw lines manually (via the mouse) and then determine their parameters? What are you finding inconvenient about `abline()`? One GUI method may be to add two sliders: one for an intercept and one for the slope. In RStudio, the `manipulate` package and function may be the answer. – Iterator Sep 02 '11 at 13:33
  • Can you clarify why you would like to add lines? It could be the case that there's a better way to get the lines that interest you, e.g. confidence bands. – Iterator Sep 02 '11 at 13:46

3 Answers3

8

Take a look at RStudio and this example:

library(manipulate)
data = matrix(rnorm(20), ncol = 2)

example <- function(data, a, b){
  plot(data[,1],data[,2])
  abline(a = a, b = b)
}

manipulate(
  example(data, a, b),
  a = slider(-5,5),
  b = slider(-5,5)
)

This will put a new line on the plot, and allow you to tweak its slope and intercept.

This was inspired by the example on this page: http://support.rstudio.org/help/discussions/questions/106-rstudio-manipulate-command

Note that this requires installing RStudio (it ships with the manipulate package, I believe). For more info, see the site.

Others' solutions with locator can be done in base R.

Iterator
  • 20,250
  • 12
  • 75
  • 111
7

Use locator(), a function that allows you to get the coordinates of the mouse pointer when clicking on a plot. Then use

plot(cars)
xy <- locator(n=2)
lines(xy, col="red", lwd=5)
lm(y~x, xy)
abline(coef(lm(y~x, xy)))
coef(lm(y~x, xy))
(Intercept)           x 
  33.142094    1.529687 

Of course the correct way of fitting lines through data is to use a proper model. Here is how you can do it with lm:

abline(coef(lm(dist~speed, cars)), col="blue")

I made the following graph with this code:

  • The thick red line is the line connecting my two mouse clicks
  • The black line is the abline through these points
  • The blue line is the line of best fit produced by lm

enter image description here

Warning 1: locator only works on some graphics devices. See ?locator for more details.

Warning 2: Drawing lines of fit by hand could well be a really stupid idea. Use a regression function like lm or a smoothing function like loess instead.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Andrie is correct about using such lines for fitting. However, they can be useful for showing bounds, albeit not very good for confidence intervals. Which raises the question: what is the point of adding the lines? – Iterator Sep 02 '11 at 13:45
  • 2
    @Iterator: The lines are probably useless for stats purposes, but you could make a nice dot-to-dot puzzle. – Richie Cotton Sep 02 '11 at 14:38
  • 1
    @Richie: Look for my next package `paintByNumbeRs`. :) Hmm, might be good for image tagging. – Iterator Sep 02 '11 at 14:42
  • 1
    @Iterator and @RichieCotton I have edited my answer to reflect a) how to extract the end points from `locator`, and b) how to draw the *proper* line of best fit. – Andrie Sep 02 '11 at 14:58
4

If you were hoping to add horizontal or vertical lines to your plot interactively, you may want to use the locator() function to capture the position of a mouse click on the plot.

For example, the following code would allow the repeated addition of vertical lines to an existing plot:

repeat {
  click.loc <- locator(1)
  if(!is.null(click.loc)) abline(v=click.loc$x)
  else break
}

You could adapt this for horizontal lines with abline(h=click.loc$y)

MatthewS
  • 526
  • 3
  • 4