Creating complex multiple axes

Now we will create a graph with two curves and three axes. First, let's read the following vectors of data:

x <- c(-25:25)

y <- 1.5*x + 2

z <- 0.3*(x**2) - 20

In the preceding code, we have a linear function and a quadratic function. As you will see, we will need some extra room for text on the right-hand margin. This is because we wish to add some explanatory text there. By default, graphs in R have margins that are as follows:

  • 5-lines wide on the bottom axis
  • 4-lines wide on the left-hand axis
  • 4-lines wide on the top axis
  • 2-lines wide on the right-hand axis

We want to create a right-hand margin 8.1-lines wide on the right axis using the mar argument, which controls margin widths:

par(mar=c(5, 4, 4, 8) + 0.1)

Note the syntax for changing the default margin width for any axis. You simply insert the desired line width value in the appropriate position within the mar argument. Now, we disable the default axes and plot as follows:

plot(x, y,type="o", pch=14, col="red", xaxt="n", yaxt="n", lty=3, xlab="", ylab="")
title("MY DESIGNER AXES", xlab=" HORIZONTAL AXIS", ylab="VERTICAL AXIS")

We now include a plot of z (the quadratic function). To do so, let's use line-type b, consisting of both points and lines. Let's also use dashes using the argument lty = 2. Use the following syntax:

lines(x, z, type="b", pch=16, col="blue", lty=2)

Now, we create a horizontal axis in the usual position using the following syntax:

axis(1, at=seq(-25,25,5),labels= seq(-25,25,5), col.axis="blue", las=2)

Now, we create a vertical axis as follows:

axis(2, at=seq(-25,25,10),labels= seq(-25,25,10), col.axis="red", las=2)

Next, we create an axis to the right using axis (4, . . .) with value labels at each point. Also, we will use smaller text and tick marks. If we wanted to create an axis at the top of the graph, we would use axis (3, . . . ) with value labels at each point.

axis(4, at=z, labels=round(z,digits=2), col.axis="blue", las=2, 
cex.axis=0.5, tck=-.02)

The tck argument controls the length of the tick marks, setting their length as a fraction of the plotting area. The default value for tck is 0.01. Setting tck = 0 gets rid of tick marks, while setting tck = 1 creates gridlines. Finally, by including the syntax cex.axis = 0.5, we have just set the axis labels to half their default size.

You can add text to your graphs using the text() and mtext() commands. The text() command places text within the graph, while mtext() places text in one of the four margins.

text(location, "text to include . . . ", pos, ...)mtext("text to place", side, line=n, ...) 

Finally, we include a title for the right-hand axis using mtext():

mtext("An axis for z", side=4, line=3, cex.lab=1.3,las=2, col="blue")

Now, we see that our graph indeed has three axes and that the right-hand axis tick marks match the data of the quadratic curve:

Creating complex multiple axes

You can use the same techniques to create complex axes for your own graphs.

Superposing graphs

To superpose graphs, the argument add=T can be very useful, but you can use it only when you have an analytic expression for each curve. Here, we plot three exponential functions together using curve():

curve(3 * exp(-x/2), from = 0, to = 10, ylim = c(0, 2), ylab = "", col = "red", lwd = 2)

curve(4 * exp(-x), add = T, lty = 4, col = "blue", lwd = 2)

curve(2.5 * exp(-x/3), add = T, lty = 3, col = "darkgreen", lwd = 2)

The syntax ylab = "" ensures that no y axis label is created. Now, add text at the right places using expression() and paste(), which we saw at the end of Chapter 1, Base Graphics in R – One Step at a Time. You must determine where to place the text by examining the graph carefully.

text(3.2, 1.9, expression(paste("My First Exponential: ", 3 * e^(-x/2))), col = "red")
text(2.8, 0, expression(paste("My Second Exponential: ", 4 * e^(-x))), col = "blue" )
text(7, 0.7, expression(paste("My Third Exponential: ", 2.5 * e^(-x/3))), col = "darkgreen" )

You should get the following graph:

Superposing graphs

Note that in each case the text is centered on the x value that you provided within the text() command.

Creating point labels

You can use the text() function to label the points on your graph. To do so, you create a set of x and y coordinates and include the text as a vector of labels. Let's work through the following example. The dataset for this example gives the heights of a group of children of different ages. Again, you can cut and paste this dataset from the code file this chapter or save the Children.csv file and read the data using read.csv(). We will produce a plot of height against age for each child, labeling each point according to the child's name.

cheight <- 
structure(list(Child = structure(c(4L, 3L, 2L, 1L), .Label = c("Anne", "John", "Mary", "Steven"), class = "factor"), Age = c(13L, 11L, 12L, 17L), Height = c(165L, 145L, 154L, 157L)), .Names = c("Child", "Age", "Height"), class = "data.frame", row.names = c(NA, -4L))

As done in previous examples, we attach the object to make the variables visible by name using the following command:

attach(cheight)

First, we create a basic graph as follows:

plot(Age, Height, main = "Heights of Four Children at Various Ages", pch = 16, ylab = "Height (cm)", xlab = "Age (yrs)" ,ylim = c(140, 180),xlim = c(10, 18))

Then, we add labels to each point. The argument Child (the third argument within the text() function) ensures that the children's names provide a label for each point.

text(Age, Height, Child, cex=1.2, pos=3, col="red") 

Let's examine our graph:

Creating point labels

Note that we used the argument pos = 3 to place the text above each point. You can experiment with the other options: pos = 1 (below the points), pos = 2 (to the left), and pos = 4 (to the right).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset