How to Plot Two Functions in R


Often you may want to plot two functions on the same graph in R to compare the shape and behavior of the functions.

The easiest way to do so is by using the curve() function as follows:

curve(x^3, from=1, to=50, xlab='x', ylab='y') 
curve(x^4, from=1, to=50, xlab='x', ylab='y', add=TRUE) 

This particular example plots the following functions on the same graph:

  • y = x3
  • y = x4

Note that we must use the argument add=TRUE in the second curve() function to specify that we’d like the second function to be added to the first plot.

Also note that you can use the curve() function as many times as you would like to add even more plots to the same graph.

Note that we used the from and to arguments to specify the x-axis range for the plot and to ensure that both functions are plotted along the same range.

The following example shows how to plot two functions on the same graph in R in practice.

Example: How to Plot Two Functions on Same Graph in R

Suppose that we would like to plot the following two functions on the same graph in R:

  • y = x3
  • y = x4

We can use the following syntax to do so:

curve(x^3, from=1, to=50, xlab='x', ylab='y') 
curve(x^4, from=1, to=50, xlab='x', ylab='y', add=TRUE) 

This produces the following chart:

Notice that both functions are shown on the same graph.

Each line represents one of the functions.

In order to distinguish which line represents which function, it can be helpful to use the col argument to change the color of each line and the lty argument to specify a different line “type” pattern for one of the lines.

For example, we can use the following syntax to plot one function with a red dashed line and the other function with a solid blue line:

curve(x^3, from=1, to=50, xlab='x', ylab='y', col='red', lty='dashed') 
curve(x^4, from=1, to=50, xlab='x', ylab='y', col='blue', add=TRUE) 

This produces the following chart:

It’s much easier to distinguish between the two lines now that they each have their own unique line color and line style.

Lastly, we can use the legend() function in base R to add a legend to the plot to explain which lines represent which functions.

We can use the legend() function with the following syntax to do so:

#plot two functions on the same graph
curve(x^3, from=1, to=50, xlab='x', ylab='y', col='red', lty='dashed') 
curve(x^4, from=1, to=50, xlab='x', ylab='y', col='blue', add=TRUE) 

#add legend to display which lines represent which function
legend('bottomright', fill=c('red', 'blue'), legend=c('x^3', 'x^4'))

This produces the following chart:

The legend in the bottom right corner helps us clearly see that the blue line represents the function x^4 while the red line represents the function x^3.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Label Points on a Scatterplot in R
How to Add Text Outside of a Plot in R
How to Add Points to an Existing Plot in R
How to Create a Scatterplot with a Regression Line in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *