How to use curve() in R
Overview
The curve() function in R is part of the graphic package, which provides basic data visualization elements like box plots, bar plots, and so on.
curve() is used to draw a curve for the equation specified in the arguments.
Syntax
curve(expr, to, from, col)
Parameters
expr: This is the expression to be curved.from: This is the start of the range to be evaluated for the expression.to: This is the end of the range to be evaluated for the expression.col: This is to specify the color of the curve. This is an optional parameter.
Note: Other graphical parameters like
xlable,ylabel,main, and so on, can also be passed as parameters.
Example
f1 <- function(x) {sin(x) # Calculating Sine Value for the Plot.}curve(f1, -4, 4, col ="red") # Specifing the range between -4, 4.
Explanation
- Line 2: We define a function to calculate the sine value for the plot using
sin(x). We store this inf1. - Line 5: We make the function call to
curve. Here,f1is passed as the expression, with a range from-4to4. The optional parametercolis set to"red".
Free Resources
Copyright ©2026 Educative, Inc. All rights reserved