Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

curve
r
graph

How to use curve() in R

Fahad Akhtar

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. 
Example code for the curve function

Explanation

  • Line 2: We define a function to calculate the sine value for the plot using sin(x). We store this in f1.
  • Line 5: We make the function call to curve. Here, f1 is passed as the expression, with a range from -4 to 4. The optional parameter col is set to "red".

RELATED TAGS

curve
r
graph

CONTRIBUTOR

Fahad Akhtar
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring