Jessica B
Jessica B

Reputation: 253

Difference between points and lines

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?

Upvotes: 5

Views: 717

Answers (1)

nicola
nicola

Reputation: 24480

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?

Upvotes: 6

Related Questions