Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overlay scatterplots in R?

Tags:

r

scatter-plot

If I have three sets of data :

a1= rnorm(10)
a2= rnorm(10)
a3= rnorm(10)

rather than looking at these side by side using:

par(mfrow=c(1,3))
plot(a1)
plot(a2)
plot(a3)

How do I get all these points on the same plot?

like image 824
Cath Penfold Avatar asked Oct 01 '13 16:10

Cath Penfold


People also ask

How do you overlap a scatterplot in R?

To overlay a scatter plot in the R language, we use the points() function. The points() function is a generic function that overlays a scatter plot by taking coordinates from a data frame and plotting the corresponding points.

Can you do a scatter plot with multiple variables?

You can create a scatter plot in R with multiple variables, known as pairwise scatter plot or scatterplot matrix, with the pairs function. In addition, in case your dataset contains a factor variable, you can specify the variable in the col argument as follows to plot the groups with different color.


1 Answers

Just use the points function:

plot(a1)
points(a2, col=2)
points(a3, col=3)

This is equivilent to:

plot(1:length(a1), a1)
points(1:length(a2), a2, col=2)
points(1:length(a3), a3, col=3)

If the vectors have unequal lengths, then you should specify the x-axis limit:

plot(a1, xlim=c(1, max(length(a1), length(a2), length(a3))))
like image 68
csgillespie Avatar answered Oct 18 '22 21:10

csgillespie