Based on the basic graphics that were created in the previous paragraph of this manual, we will elaborate some to create more advanced graphics. What we are going to do is to add two other sets of data, one represented by an additional line, one as four large green varying symbols. Then, in order to keep oversight over the graph, a basic legend is added to the plot. Finally, we let R draw a curved line based on a quadratic function.

## Adding elements to plots

Just as in the previous paragraph, first some data will be created. The x and y variables are copied from the previous paragraph, so we will be able to recreate the graph we saw there. Then, an additional set of values is created and assigned to the variable z. This is done in the first three rows of the syntax below.

x <- c(1, 3, 4, 7, 9)

y <- c(3, 4, 5, 6, 5)

z <- c(5, 6, 4.5, 5.5, 5)plot (x,y, type=”b”, main=”Example plot”, xlab=”Predictor Value”,

ylab=”Outcome Value”, col=”blue”, lty=2)lines(x, z, type=”b”, col=”red”)

points(x=c(2,4,6,8), y=c(5.5, 5.5, 5.5, 5.5), col=”darkgreen”, pch=1:4, cex=2)

Then, using the plot() function, a plot is created for the first line we want, closely representing the figure from the previous paragraph. Two things are different now: first of all, we ask for a blue line, by specifying col=”blue”. Secondly a dotted line is created because the line-type is set to ‘2’ (lty=2).

Now we want to add an additional line. As you might have noticed by now, the plot() function generally clears the graphics-device and creates a new graphic from scratch ((This is not always the case, though. You can have the plot() function add elements to existing graphs by specifying ‘add=TRUE’, which results in similar functionality as the lines() and points() functions shown here)). Remember that there are two types of plotting functions: those that create a full plot (including the preparation of the graphics device), and those that only add elements to an already prepared graphics device. We will use two functions of that second type to add elements to our existing plot.

First, we want another line-graph, representing the relationship between the x and the z variables. For this, we use the lines() function. By specifying col=”red” we get a red line, added to our existing graph. It is possible to set the line-type (which we didn’t here), but you can’t set elements as labels for the axes or the main graph-title. This can only be done by functions that setup the graphics device as well, such as plot().

Next, using points(), we add four green points to our already existing graphic. Instead of storing the four coordinates of these points in variables and specifying these variables in a plot-function, we describe the coordinates inside the points() function. This can be done with all (plotting) functions in are that require data, just as all these functions can have data specified by using variables in which the data is stored. By specifying four different values for the ‘pch’-parameter, four different symbols are used to indicate the data-points. The ‘cex=2′ parameter tells R to expand the standard character in size by a factor 2 (cex stands for Character Expansionfactor).

## Legend

legend(x=7.5, y=3.55, legend=c(“Line 1″, “Line 2″, “Dots”), col=c(“blue”,”red”, “darkgreen”), lty=c(2,1,0), pch=c(1,1,19))

A legend is not automatically added to graphics made by R-Project. Fortunately, we can add these manually, by using the legend() function. The syntax above is used to create the legend as shown below.

Several parameters are needed to have the right legend added to your graph. In the order as specified above these are:

**x=7.5, y=3.55**: These parameters specify the coordinates of the legend. Normally, these coordinates refer to the upper-left corner of the legend, but this can be specified differently.**legend=c(“Line 1″, “Line 2″, “Dots”)**: the ‘legend=’ parameter needs to receive values that will be used as labels in the legend. Here, I chose to use the character strings ‘Line 1′, ‘Line 2′, and ‘Dots’ and concatenated them using c(). It is important to note that the order these labels will appear on the legend is determined by the order that they are specified to the legend-parameter,*not*by the order in which they are added to the plot.**col=c(“blue”,”red”, “darkgreen”), lty=c(2,1,0), pch=c(1,1,19)**The last three parameters, col, lty, and pch, are treated the same way as in other graphical function, as described above. The difference is that not one value is given, but three.

## Plotting the curve of a formula

Sometimes you don’t want to graphically represent data, but the model that is based on it. Or, more generally, you want to graphically represent a formula. When the relationship is bivariate, this can easily be done with the curve()-function. For instance, let’s say we want to add the formula “y = 2 + x ^ 5″ (the result is 2 plus the square root of the value of x). Using the syntax below, this formula is graphically represented and added to the existing graphic. It is drawn for the values on the x-axis from 1 to 7. Here it can be seen, that it is not necessary to draw the function over the full scale of the x-scale.

By specifying lwd=2 (lwd= Line Width) a thick line is drawn.

curve(1 + x ^.5, from=1, to=7, add=TRUE, lwd=2)

– – — — —– ——–

**Discuss this article and pose additional questions in the R-Sessions Forum****Find the original article embedded in the manual.**

– – — — —– ——–

R-Sessions is a collection of manual chapters for R-Project, which are maintained on Curving Normality. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.

——– —– — — – –

Index of the R-Sessions | Curving Normality[...] Intermediate Graphics [...]