Draw Multiple Function Curves to Same Plot in R
In this article, we will discuss how to plot multiple function curves to the same plot in the R programming language.
Method 1: In Base R
Base R supports a function curve() which can be used to visualize a required function curve. It supports various parameters to edit the curve according to requirements.
Syntax: curve(expression, to, from, col)
- expression: To be curved
- to, from: range of curve plotting
- col: color of curve
To draw multiple curves in one plot, different functions are created separately and the curve() function is called repeatedly for each curve function. The call for every other curve() function except for the first one should have added an attribute set to TRUE so that multiple curves can be added to the same plot. To differentiate among the different colors are used.
Method 2: Using ggplot
GGPLOT2 is an R library used to visualize plots with its various easy-to-use functions. To draw multiple curves using gglot functions are first created normally. But to draw them in the same plot, the functions are converted to dataframe and then visualized.