YefR
YefR

Reputation: 369

Add points at a certain X values in R

How can I add points at a certain x value? For example I have the following data:

c(1,2,3)
c(5,6,7)

Now, I want the following plot: enter image description here

I.e., I want to add points (5,6,7), at X values (6,7,8)

The following code does not help:

plot(c(1,2,3),xlim=c(1,8),ylim=c(1,10))
points(c(5,6,7))

What should be done?

Upvotes: 1

Views: 452

Answers (1)

hatze
hatze

Reputation: 531

As a one-liner you can achieve the plotting of all six coordinates like this:

plot(c(1,2,3,6,7,8),c(1,2,3,5,6,7),xlim=c(1,8),ylim=c(1,10),xlab="x index", ylab="y values")

Moreover, you can add additional points to your plot with the points function, see the official R documentation for reference. With points function the

coordinates can be passed in a plotting structure (a list with x and y components), a two-column matrix, a time series, ....

This way, the example becomes:

plot(c(1,2,3),xlim=c(1,8),ylim=c(1,10),xlab="x index", ylab="y values")
points(c(6,7,8),c(5,6,7))

Upvotes: 4

Related Questions