Plotting Basic Functions & Curves in R

In these notes, we briefly survey how to
plot a
variety of functions in R, from basic linear functions to slightly more
complex
curves.

First, we plot a simple linear function. We first specify the range on the y-axis using the "seq" (sequence) function, then plot the line:

> par(mar = c(2.0, 2.0, 2.0, 2.0)+0.1)

> x <- seq (0, 20, length = 10)

> plot(x)

Notice
as
well that to plot this image, we specified par(mar
=
c(2.0, 2.0, 2.0, 2.0)+0.1). This
was to prepare the plotting space to have 2 lines of white space at the
bottom of the image, 2 at the left, 2 at the top, and 2 at the right,
in that exact order.
We could adjust this space as we wish. For instance, if we wanted less
space at the top of the image, we could ask R to reduce this space to 1
line:

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> x <- seq (0, 20, length = 10)

> plot(x)

**DATA
& DECISION**, Copyright 2010,
Daniel
J. Denis, Ph.D. Department
of Psychology, University of Montana. Contact Daniel
J. Denis by e-mail daniel.denis@umontana.edu.

First, we plot a simple linear function. We first specify the range on the y-axis using the "seq" (sequence) function, then plot the line:

> par(mar = c(2.0, 2.0, 2.0, 2.0)+0.1)

> x <- seq (0, 20, length = 10)

> plot(x)

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> x <- seq (0, 20, length = 10)

> plot(x)

We now have slightly less space at
the top of the image than we did before (the size of the image has also
increased slightly, but that was simply due to a slightly different
re-sizing of the graph window when saving the image, and is not a
reflection of anything in the commands to R)

We could change the seq function to specify a y-axis of 0 to 50 and a x-axis of 0 to 50 as follows:

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> x <- seq (0, 50, length = 50)

> plot(x)

We could change the seq function to specify a y-axis of 0 to 50 and a x-axis of 0 to 50 as follows:

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> x <- seq (0, 50, length = 50)

> plot(x)

The greater we specify the length,
the more solid the line will appear. For instance, if we changed length
to an upper limit of 1000, we'd have:

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> x <- seq (0, 50, length = 1000)

> plot(x)

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> x <- seq (0, 50, length = 1000)

> plot(x)

The line is thicker simply because
there are more points that had to be plotted, not because we've
instructed R to produce a thicker line.

Plotting Other Curves

Plotting other types of functions and curves is quite straightforward. We simply adjust the argument in the plot function. For instance, if we wanted to plot a parabola, we would enter:

> x <- seq (0, 50, length = 50)

> plot(x^2)

A couple things to note about the above plot. First, the x-axis is as expected, with length equal to 50. However, the y-axis no longer has an upper limit of 50, but rather has an upper limit of 50*50 = 2500. This is because we've plotted the square of x (x^2). Also, notice that the plot is not centered about the origin, and we're only getting the right "half" of the parabola. If we wanted to center the plot about its origin, we would need to specify equal left and right limits from the origin (0) in the seq function, as follows:

> x <- seq (-50, 50, length = 50)

> plot(x^2)

Plotting Other Curves

Plotting other types of functions and curves is quite straightforward. We simply adjust the argument in the plot function. For instance, if we wanted to plot a parabola, we would enter:

> x <- seq (0, 50, length = 50)

> plot(x^2)

A couple things to note about the above plot. First, the x-axis is as expected, with length equal to 50. However, the y-axis no longer has an upper limit of 50, but rather has an upper limit of 50*50 = 2500. This is because we've plotted the square of x (x^2). Also, notice that the plot is not centered about the origin, and we're only getting the right "half" of the parabola. If we wanted to center the plot about its origin, we would need to specify equal left and right limits from the origin (0) in the seq function, as follows:

> x <- seq (-50, 50, length = 50)

> plot(x^2)

Plotting a cubic function yields:

> plot(x^3)

> plot(x^3)

A
quartic
function can be plotted:

> plot(x^4)

We notice that the spacing just below the title is insufficient
(it looks a bit crowded too close to the actual graph). We can address
this by adding an additional line of white space below "Absolute
Function" by adjusting the par
function:

> par(mar = c(2.0, 2.0, 2.0, 2.0)+0.1)

> plot(abs(x))

> title("Absolute Function")

> plot(x^4)

A
quintic
function looks like:

> plot(x^5)

> plot(x^5)

The
absolute value function |x| is plotted as follows:

> plot(abs(x))

> plot(abs(x))

Adding a title to a plot is done by
adding the "title" command. In what follows, we add the title "Absolute
Function" to the previous plot:

> title("Absolute Function")

> title("Absolute Function")

> par(mar = c(2.0, 2.0, 2.0, 2.0)+0.1)

> plot(abs(x))

> title("Absolute Function")

Suppose
we
wanted to plot the rational function (x-3)/(x-2), we would enter:

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> plot((x-3)/(x-2))

> par(mar = c(2.0, 2.0, 1.0, 2.0)+0.1)

> plot((x-3)/(x-2))

Notice how R is showing the
asymptotes associated with the graph, by showing distant dots (the red
arrows have been drawn manually to emphasize the asymptotes).