Table of Contents
To plot a function curve in R, you can use the “plot” function, which takes in the x and y values as arguments. This will create a scatter plot of the points. To create a smooth curve, you can use the “curve” function, which takes in the function equation as well as the range of x values to be plotted. This will generate a continuous curve based on the specified function. Here is an example:
x
Plot a Function Curve in R (With Example)
You can use the following methods to plot a function curve in R:
Method 1: Use Base R
curve(x^3, from=1, to=50, xlab='x', ylab='y')
Method 2: Use ggplot2
library(ggplot2) df <- data.frame(x=c(1, 100)) eq = function(x){x^3} #plot curve in ggplot2 ggplot(data=df, aes(x=x)) + stat_function(fun=eq)
Both methods will produce a plot that shows the curve of the function y = x3.
The following examples show how to use each method in practice.
Example 1: Plot Function Curve Using Base R
The following code shows how to plot the curve of the function y = x3 using the curve() function from base R:
#plot curve using x-axis range of 1 to 50 curve(x^3, from=1, to=50, xlab='x', ylab='y')

Note that you can use the following arguments to modify the appearance of the curve:
- lwd: Line width
- col: Line color
- lty: Line style
The following code shows how to use these arguments in practice:
#plot curve using x-axis range of 1 to 50 curve(x^3, from=1, to=50, xlab='x', ylab='y', lwd=3, col='red', lty='dashed'))

Feel free to play around with the values for these arguments to create the exact curve you’d like.
Example 2: Plot Function Curve Using ggplot2
The following code shows how to plot the curve of the function y = x3 using the stat_function() function from ggplot2:
library(ggplot2) #define data frame df <- data.frame(x=c(1, 100)) #define function eq = function(x){x^3} #plot curve in ggplot2 ggplot(data=df, aes(x=x)) + stat_function(fun=eq)

You can also use the lwd, col, and lty functions within the stat_function() function to modify the appearance of the curve:
library(ggplot2) #define data frame df <- data.frame(x=c(1, 100)) #define function eq = function(x){x^3} #plot curve in ggplot2 with custom appearance ggplot(data=df, aes(x=x)) + stat_function(fun=eq, lwd=2, col='red', lty='dashed')

Note: You can find the complete documentation for the ggplot2 stat_function() function .
Cite this article
stats writer (2024). How can I plot a function curve in R? Can you provide an example?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-plot-a-function-curve-in-r-can-you-provide-an-example/
stats writer. "How can I plot a function curve in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 24 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-plot-a-function-curve-in-r-can-you-provide-an-example/.
stats writer. "How can I plot a function curve in R? Can you provide an example?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-plot-a-function-curve-in-r-can-you-provide-an-example/.
stats writer (2024) 'How can I plot a function curve in R? Can you provide an example?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-plot-a-function-curve-in-r-can-you-provide-an-example/.
[1] stats writer, "How can I plot a function curve in R? Can you provide an example?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I plot a function curve in R? Can you provide an example?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
