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?
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.
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.
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))))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With