Book Image

R Graph Essentials

Book Image

R Graph Essentials

Overview of this book

This book is targeted at R programmers who want to learn the graphing capabilities of R. This book will presume that you have working knowledge of R.
Table of Contents (6 chapters)
5
Index

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=&quot...