5

On the face of it, points should add points to an existing R plot, while lines should add a line. But reading the documentation and experimenting tell me that you can use any of the plot type options with either. As a result, you can easily add points using lines and lines using points.

Is there actually a difference between these two commands, apart from the default value of type?

zx8754
  • 52,746
  • 12
  • 114
  • 209
Jessica B
  • 253
  • 2
  • 5
  • 2
    Short answer: no. Long answer: verify by yourself looking at `graphics:::lines.default` and `graphics:::points.default`. – nicola Jun 25 '19 at 09:39
  • Both functions call the same `plot.xy` internally, by passing type `"l"` or `"p"`. – zx8754 Jun 25 '19 at 09:42
  • Basically the difference is the readability of your code. It makes more sense to read `lines(...)` and interpret that you're going to plot line chart than `points(..., type = 'l' )` – patL Jun 25 '19 at 09:42
  • 1
    @nicola You are assuming I have sufficient knowledge of R to read the code for myself. – Jessica B Jun 25 '19 at 09:49
  • @JessicaB I added a few words and a reference on how to find the source code of a function in my answer. – nicola Jun 25 '19 at 09:54

1 Answers1

6

No, there isn't any difference other than the default type between points and lines. They are just wrappers of plot.xy, as one can easily verify from the source code:

graphics:::points.default
#function (x, y = NULL, type = "p", ...) 
#plot.xy(xy.coords(x, y), type = type, ...)
#<bytecode: 0x1ecccb8>
#<environment: namespace:graphics>
graphics:::lines.default
#function (x, y = NULL, type = "l", ...) 
#plot.xy(xy.coords(x, y), type = type, ...)
#<bytecode: 0x1ec7938>
#<environment: namespace:graphics>

Just an addendum: this isn't uncommon in R. For instance the read.csv, read.table and family are basically the same function which just differ for the default value of some arguments. These wrappers are quite handy and often add readability to your code.

Second addendum: How I found the source code of these functions? Both points and lines are generic functions, with methods that apply depending on the class of the object argument. You might want to read this famous question:

How can I view the source code for a function?

nicola
  • 24,005
  • 3
  • 35
  • 56
  • 1
    Perfect answer. I'd add the difference in the code readability. See coments in OP. – patL Jun 25 '19 at 09:47
  • Your second addendum is not helpful for answering the questions. Please could you roll it back? – Jessica B Jun 25 '19 at 09:57
  • 1
    @JessicaB This was added because of your comment. I think that it might be helpful to know how to get the source code of a function in this case, since the only way to tell if there is a difference between `points` and `lines` is to read the source code. – nicola Jun 25 '19 at 10:01
  • i also think it's helpful to show this link. This helps to reproduce your answer on different tasks for other users. – mischva11 Jun 25 '19 at 10:03
  • @nicola To me your wording comes across as 'you should have been able to work this out for yourself', which is not reasonable, rather that 'I know this is the answer because...' Note that finding the source code and understanding it are different things. – Jessica B Jun 25 '19 at 11:46
  • @JessicaB Had I thought that "'you should have been able to work this out for yourself" I wouldn't bother to write an answer and to add (twice!) relevant information. It was precisely because I wanted to explain how I reached the conclusion that `points` and `lines` are the same function that I wrote the second addendum. If you think that such information is so useless that I should even delete it, fine. But I don't get how you can really think that I had the kind of attitude you describe, after losing time writing stuff, making some research and producing a well received answer. – nicola Jun 25 '19 at 12:00
  • Regarding the edit of your comment: to state that `points` and `lines` are the same function, you don't need to understand the code; it suffices to realize that it's the same. If there is something that's not clear, just ask. – nicola Jun 25 '19 at 12:05
  • 2
    @nicola My apologies then. I've incountered such attitudes many times on other SE sites. Maybe SO is different. – Jessica B Jun 25 '19 at 12:13