International College of Digital Innovation, CMU
September 1, 2025
The curve()
function in R is used to plot mathematical functions over a specified interval.
It’s particularly useful for visualizing the shape of a function or for adding function curves to existing plots.
The curve()
function is a powerful tool for visualizing mathematical expressions and functions directly in R.
Parameters:
expr
: The mathematical expression to plot. This can be a function or an expression in terms of x
.
from
: The starting value of x
for which the function will be plotted.
to
: The ending value of x
for which the function will be plotted.
add
: If TRUE
, the curve will be added to an existing plot. If FALSE
(default), a new plot will be created.
col
: The color of the curve.
Let’s plot the function \(y = x^2\) over the interval [-10, 10].
Note: If we want to hide the box around the graph, we can set the argument bty = "n"
.
You can add a curve to an existing plot by setting
add = TRUE
.
The lwd
argument controls the thickness of lines in plots.
A larger lwd
value increases the line thickness, while a smaller value decreases it.
You can use lwd
in any function that involves drawing lines, such as plot()
, curve()
, lines()
, and abline()
.
You can also plot a custom function using the curve()
function.
In R, the lty
argument is used in plotting functions to specify the line type, such as solid
, dashed
, or dotted
.
The common values for lty:
0: Blank (no line)
1: Solid line (default)
2: Dashed line
3: Dotted line
4: Dot-dash line
5: Long-dash line
6: Two-dash line
Question
From the example above change the lty
from 1 to another number.
1. Linear Function:
\[ y = 2x + 1 ,~ x\in[-5,5] \]
2. Quadratic Function:
\[ y = x^2 - 4x + 3,~ x\in[-5,10] \]
3. Cubic Function:
\[ y = x^3 - 3x^2 + 2,~ x\in[-5,5] \]
4. Exponential Function:
\[ y = e^{-0.5x} ,~ x\in[-5,5] \]
5. Logarithmic Function:
\[ y = \log(x) ,~ x\in[0.001,10] \]
6. Sine Function:
\[ y = \sin(x),~ x\in[-4\pi,4\pi] \]
7. Cosine Function:
\[ y = \cos(x) ~ x\in[-4\pi,4\pi] \]
8. Tangent Function:
\[ y = \tan(x), ~ x\in[-4\pi,4\pi] \]
9. Reciprocal Function:
\[ y = \frac{1}{x}, ~ x\in[0.0001,2] \]
10. Gaussian (Normal) Distribution:
\[y = \frac{1}{\sqrt{2\pi}} e^{-\frac{x^2}{2}},~ x\in[-4,4]\]
Sine function:
\[ y = \sin(x),~ x\in[-4\pi,4\pi] \]
Cosine function:
\[ y = \cos(x) ~ x\in[-4\pi,4\pi] \]
Solution:
Objective: Plot a simple mathematical function.
Use the curve()
function to plot the function \(y = x^2\) over the interval \([-10, 10]\).
Label the x-axis as “x values” and the y-axis as “y values”.
Add a main title to the plot that says “Plot of y = x^2”.
Solution:
# Plot y = x^2 and y = x^3 on the same graph
curve(x^2, from = -5, to = 5,
col = "blue",
lty = 1,
ylab = "y values",
main = "Plot of y = x^2 and y = x^3")
curve(x^3, from = -5, to = 5,
col = "red",
lty = 2,
add = TRUE)
# Add a legend
legend("topleft", legend = c("y = x^2", "y = x^3"),
col = c("blue", "red"),
lty = 1:2)
Objective: Overlay multiple curves on the same plot.
Use the curve()
function to plot \(y = x^2\) and \(y = x^3\) on the same graph over the interval \([-5, 5]\).
Use different colors and line types for each curve.
Add a legend to distinguish between the two curves.
Solution:
Objective: Create and plot a custom function.
Define a custom function in R, say \(f(x) = \sin(x) \times \exp(-0.1x)\).
Use the curve()
function to plot this custom function over the interval \([0, 20]\).
Use a dashed line type and a blue color for the plot.
Solution:
# Plot y = log(x) over different intervals
curve(log(x), from = 0.1, to = 10,
col = "blue",
ylab = "log(x)",
main = "Plot of y = log(x) over different intervals")
curve(log(x), from = 1, to = 100,
col = "red",
add = TRUE)
curve(log(x), from = 10, to = 1000,
col = "green",
add = TRUE)
# Add a legend
legend("topleft", legend = c("[0.1, 10]", "[1, 100]", "[10, 1000]"),
col = c("blue", "red", "green"),
lty = 1)
Objective: Understand the impact of different intervals.
Plot the function \(y = \log(x)\) using the curve()
function over three different intervals: \([0.1, 10]\), \([1, 100]\), and \([10, 1000]\).
Plot all three curves on the same graph using different colors.
Add a legend to explain which color corresponds to which interval.
Solution:
# Plot y = sqrt(x) over the interval [0, 25]
curve(sqrt(x), from = 0, to = 25,
col = "purple",
ylab = "sqrt(x)",
main = "Plot of y = sqrt(x) with points and annotations")
# Add points at x = 5, 10, 15, 20
points(c(5, 10, 15, 20), sqrt(c(5, 10, 15, 20)),
pch = 19,
col = "red")
# Annotate each point with (x, y) values
text(c(5, 10, 15, 20), sqrt(c(5, 10, 15, 20)) + 0.5,
labels = c("(5, sqrt(5))", "(10, sqrt(10))",
"(15, sqrt(15))", "(20, sqrt(20))"),
pos = 3)
Objective: Enhance the plot with points (point()
function) and annotations (text()
function).
Use the curve()
function to plot the function \(y = \sqrt{x}\) over the interval \([0, 25]\).
Add points at \(x = 5, 10, 15,\) and \(20\) on the curve.
Annotate each point with its corresponding \((x, y)\) value.
Add a title, and label the axes accordingly.
RColorBrewer
is an R package that provides a set of color palettes for use in R graphics.
It is especially useful for creating visually appealing and colorblind-friendly plots.
The palettes in RColorBrewer
are designed to be used with categorical, sequential, or diverging data.
Key Features of RColorBrewer:
Color Palettes: The package offers three types of color palettes:
Sequential: These are suited for ordered data that progresses from low to high (e.g., Blues
, Reds
).
Diverging: These are designed for data that diverges from a central point, with two contrasting colors on either end (e.g., RdBu
, Spectral
).
Qualitative: These palettes are used for categorical data, where each category should be represented by a different color (e.g., Set1
, Pastel2
).
Colorblind-Friendly: Many of the palettes are designed to be colorblind-friendly, making your plots more accessible.
Installation
You can install RColorBrewer
from CRAN using:
Usage
To use RColorBrewer
, you first need to load the package and then explore the available palettes using functions like display.brewer.all()
or brewer.pal()
.
This will show you all available color palettes in the package, organized by type (sequential
, diverging
, qualitative
).
brewer.pal(n, name)
: Generates a palette with n
colors from the specified palette name
.
display.brewer.all()
: Displays all available palettes.
display.brewer.pal(n, name)
: Displays a specific palette with n
colors.
colorRampPalette(colors)
: Creates a continuous color ramp from a palette.
Blues
BuGn (Blue-Green)
BuPu (Blue-Purple)
GnBu (Green-Blue)
Greens
Greys
Oranges
OrRd (Orange-Red)
PuBu (Purple-Blue)
PuBuGn (Purple-Blue-Green)
PuRd (Purple-Red)
Purples
RdPu (Red-Purple)
Reds
YlGn (Yellow-Green)
YlGnBu (Yellow-Green-Blue)
YlOrBr (Yellow-Orange-Brown)
YlOrRd (Yellow-Orange-Red)
These are used for data that diverges around a central value:
BrBG (Brown-Blue-Green)
PiYG (Pink-Yellow-Green)
PRGn (Purple-Green)
PuOr (Purple-Orange)
RdBu (Red-Blue)
RdGy (Red-Grey)
RdYlBu (Red-Yellow-Blue)
RdYlGn (Red-Yellow-Green)
Spectral
These are used for categorical data where each category needs to be represented by a different color:
Accent
Dark2
Paired
Pastel1
Pastel2
Set1
Set2
Set3
Blues
BuGn (Blue-Green)
BuPu (Blue-Purple)
GnBu (Green-Blue)
Greens
Greys
Oranges
OrRd (Orange-Red)
PuBu (Purple-Blue)
PuBuGn (Purple-Blue-Green)
PuRd (Purple-Red)
Purples
RdPu (Red-Purple)
Reds
YlGn (Yellow-Green)
YlGnBu (Yellow-Green-Blue)
YlOrBr (Yellow-Orange-Brown)
YlOrRd (Yellow-Orange-Red)
These are used for data that diverges around a central value:
BrBG (Brown-Blue-Green)
PiYG (Pink-Yellow-Green)
PRGn (Purple-Green)
PuOr (Purple-Orange)
RdBu (Red-Blue)
RdGy (Red-Grey)
RdYlBu (Red-Yellow-Blue)
RdYlGn (Red-Yellow-Green)
Spectral
These are used for categorical data where each category needs to be represented by a different color:
Accent
Dark2
Paired
Pastel1
Pastel2
Set1
Set2
Set3