Graphing Explicit Functions in R
Basic Syntax for Plotting Functions
To plot an explicit function in R, you typically use the curve() function, which is specifically designed for plotting mathematical expressions. Here’s the basic syntax:
curve(expr, from = NULL, to = NULL, n = 101, add = FALSE, ...)
- expr: An expression defining the function to plot.
- from: The starting x-value for the plot (optional).
- to: The ending x-value for the plot (optional).
- n: Number of points used to evaluate the function (default is 101).
- add: Logical value indicating if the curve should be added to an existing plot (default is FALSE).
- …: Additional graphical parameters (e.g., color, line type).
Plotting Basic Functions
Example 1: Plotting a Linear Function
# Plot a linear function y = 2x + 1 curve(2 * x + 1, from = -10, to = 10, col = "blue", lwd = 2, main = "Plot of y = 2x + 1", ylab = "y", xlab = "x")
- 2 * x + 1: The linear function.
- from = -10: Start x-value.
- to = 10: End x-value.
- col = “blue”: Color of the curve.
- lwd = 2: Line width.
Example 2: Plotting a Quadratic Function
# Plot a quadratic function y = x^2 - 4x + 4 curve(x^2 - 4 * x + 4, from = -5, to = 10, col = "red", lwd = 2, main = "Plot of y = x^2 - 4x + 4", ylab = "y", xlab = "x")
- x^2 – 4 * x + 4: The quadratic function.
Plotting Trigonometric Functions
Example 3: Plotting a Sine Function
# Plot a sine function y = sin(x) curve(sin(x), from = -2 * pi, to = 2 * pi, col = "green", lwd = 2, main = "Plot of y = sin(x)", ylab = "y", xlab = "x")
- sin(x): The sine function.
- from = -2 * pi: Start x-value to cover a few periods of the sine wave.
- to = 2 * pi: End x-value.
Example 4: Plotting a Cosine Function
# Plot a cosine function y = cos(x) curve(cos(x), from = -2 * pi, to = 2 * pi, col = "purple", lwd = 2, main = "Plot of y = cos(x)", ylab = "y", xlab = "x")
- cos(x): The cosine function.
Plotting Exponential and Logarithmic Functions
Example 5: Plotting an Exponential Function
# Plot an exponential function y = exp(x) curve(exp(x), from = -2, to = 2, col = "orange", lwd = 2, main = "Plot of y = exp(x)", ylab = "y", xlab = "x")
- exp(x): The exponential function.
Example 6: Plotting a Logarithmic Function
# Plot a logarithmic function y = log(x) curve(log(x), from = 0.1, to = 10, col = "brown", lwd = 2, main = "Plot of y = log(x)", ylab = "y", xlab = "x")
- log(x): The natural logarithm function.
- from = 0.1: Start x-value to avoid log(0).
Customizing the Plot
You can customize the plot using graphical parameters:
- col: Color of the curve.
- lwd: Line width.
- lty: Line type (e.g., 1 = solid, 2 = dashed).
- main: Title of the plot.
- xlab and ylab: Labels for the x and y axes.
Example 7: Customized Plot
# Custom plot with title, axis labels, and different line type curve(x^3 - 3 * x + 1, from = -3, to = 3, col = "blue", lwd = 2, lty = 3, main = "Customized Plot of y = x^3 - 3x + 1", xlab = "x values", ylab = "y values")
- x^3 – 3 * x + 1: The cubic function.
- lty = 3: Dashed line type.
Adding Multiple Functions to One Plot
You can plot multiple functions on the same graph by first creating an empty plot and then adding the curves using curve() with add = TRUE.
Example 8: Multiple Functions
# Plot a sine function curve(sin(x), from = -2 * pi, to = 2 * pi, col = "blue", lwd = 2, main = "Multiple Functions", ylab = "y", xlab = "x") # Add a cosine function to the existing plot curve(cos(x), from = -2 * pi, to = 2 * pi, col = "red", lwd = 2, add = TRUE) # Add a legend legend("topright", legend = c("sin(x)", "cos(x)"), col = c("blue", "red"), lwd = 2)
- add = TRUE: Adds the curve to the existing plot.
Advanced Example: Plotting with Conditional Expressions
Example 9: Conditional Function
# Plot a piecewise function curve(ifelse(x < 0, x^2, x^3), from = -2, to = 2, col = "green", lwd = 2, main = "Piecewise Function", ylab = "y", xlab = "x")
- ifelse(x < 0, x^2, x^3): Defines a piecewise function.
Summary
- Basic Syntax: Use curve() to plot explicit functions.
- expr: Function to plot.
- from and to: Range of x-values.
- Basic Examples: Plot linear, quadratic, trigonometric, exponential, and logarithmic functions.
- Customization: Adjust color, line width, line type, and labels.
- Multiple Functions: Use add = TRUE to plot multiple functions on the same graph.
- Conditional Functions: Plot piecewise or conditional functions.