It's a common scenario to plot multiple datasets together on a single graph. For example, we may want to plot the daily returns from multiple stocks on a single chart to understand how they trend vis-a-vis each other. Similarly, we may want to plot multiple normal distribution curves with different mean and standard deviations.
To plot multiple datasets, we first draw a graph with a single dataset using the plot() function. Then we add the second data set using the points() or lines() function.
Let's learn this with the help of an example where we will plot multiple normal distribution curves.
Generate x-axis data
First we will generate data for x-axis which will be a sequence of 200 evenly spaced numbers ranging from -5 to 5. We can do this using the seq() function in R.
> x<-seq(-5,5,length=200)
Calculate Values for Normal Distribution
We can do this in two ways: 1) Generate random numbers using rnorm() and then apply the density() function to the data. 2) Alternatively we can do this directly using the dnorm() function which gives the density of the distribution function. In more simple terms, this function gives height of the probability distribution at each point for a given mean and standard deviation. For our purpose, we will generate multiple datasets with different means and standard deviations.
Note that if we were plotting just the scatter graph without lines, we could add more data points to it using the points() function instead of the lines() function.
Setting Canvas Size
Sometimes when we want to add multiple datasets to a single plot, it is important to correctly specify the size of the canvas. Let's say the first dataset that you plot has an x-value range of 0 to 100. Once this is plotted, the graph will draw the x-axis with the 0-100 range. However, assume now that the second dataset that you want to plot has x values ranging from 0 to 200. Since the initial plot doesn't consider this, the points from the second dataset will be plotted off the chart and will be cut-off. To correct this problem, we need to set the coordinates for the graph in the beginning itself. This can be done using the xlim and ylim arguments.
Suppose we want to plot two datasets (x1,y1) and (x2,y2). We can compute the limits using the range function and then set them using xlim and ylim.