Table of Contents
Plotting an equation in R involves using the built-in function “plot” to create a graphical representation of the equation. The process typically involves specifying the equation, defining the range of values for the x-axis, and then using the “plot” function to generate a corresponding plot. Examples of plotting equations in R include creating a scatter plot, line plot, or bar plot to visualize relationships between variables. Additional features such as adding labels, titles, and legends can also be incorporated into the plot to enhance its presentation. Overall, plotting an equation in R allows for a visual understanding of the relationship between variables and can aid in analyzing data and making conclusions.
Plot an Equation in R (With Examples)
You can use the following basic syntax to plot an equation or function in R:
Method 1: Use Base R
curve(2*x^2+5, from=1, to=50, , xlab="x", ylab="y")
Method 2: Use ggplot2
library(ggplot2) #define equation my_equation <- function(x){2*x^2+5} #plot equation ggplot(data.frame(x=c(1, 50)), aes(x=x)) + stat_function(fun=my_equation)
Both of these particular examples plot the equation y = 2x2 + 5.
The following examples show how to use each method in practice.
Example 1: Plot Equation in Base R
Suppose you’d like to plot the following equation:
y = 2x2 + 5
You can use the following syntax in base R to do so:
curve(2*x^2+5, from=1, to=50, , xlab="x", ylab="y")
This produces the following plot:

If you’d like to plot points instead, simply specify type=”p” in the curve() function:
curve(2*x^2+5, from=1, to=50, , xlab="x", ylab="y", type="p")
This produces the following plot:

Example 2: Plot Equation in ggplot2
Suppose you’d like to plot the following equation:
y = 2x2 + 5
You can use the following syntax in to do so:
library(ggplot2) #define equation my_equation <- function(x){2*x^2+5} #plot equation ggplot(data.frame(x=c(1, 50)), aes(x=x)) + stat_function(fun=my_equation)
This produces the following plot:

Notice that this plot matches the one we created in the previous example in base R.
Note: To plot a different equation, simply change the values defined for the my_equation variable.
Additional Resources
The following tutorials explain how to perform other common tasks in R:
Cite this article
stats writer (2024). How do you plot an equation in R, and can you provide examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-plot-an-equation-in-r-and-can-you-provide-examples/
stats writer. "How do you plot an equation in R, and can you provide examples?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-plot-an-equation-in-r-and-can-you-provide-examples/.
stats writer. "How do you plot an equation in R, and can you provide examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-plot-an-equation-in-r-and-can-you-provide-examples/.
stats writer (2024) 'How do you plot an equation in R, and can you provide examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-plot-an-equation-in-r-and-can-you-provide-examples/.
[1] stats writer, "How do you plot an equation in R, and can you provide examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How do you plot an equation in R, and can you provide examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
