Scatter plot
The plot()
function is used for creating scatter plots and line graphs. In the two examples below we first create a scatter graph then a line graph.
A scatter plot requires two vectors of data one for the x coordinates and one for the y coordinates of the graph. We can create a third vector which represents a split in the data e.g. male/female, ward 1/ward 2. All three of these vectors must be of equal length as R will match up the elements from each vector in order. The base-R plot functions take a large number of different arguments which can be explored with the help of the function documentation available online. In the example below we enter the x and y coordinates as the first two arguments, each of the following arguments is then preceded by an identifier. xlab
and ylab
refer to the x and y axis labels. Main
refers to the title of the graph. Col
is the colour of the points in the scatter plot, we use the vector z as the input for this argument and the points are coloured black and red.
Input:
set.seed(1)
x <- rnorm(1000, 50, 2)
y <- rnorm(1000, 25, 2)
z <- as.factor(sample(1:2,1000,replace=TRUE))
plot(x,y,xlab="Outcome measure 1",ylab="Outcome measure 2",main="Comparison of outcome measures",col=z)
Output:
Line plot
When creating a line graph you will need to have the different lines as separate e.g. patients,samples, as separate vectors. We can enter multiple x and y coordinate vectors into the plot function using the concatenation command c()
. Note that there must be an equal number of x coordinate vectors and y coordinate vectors so the x variable is repeated twice in the plot function c(x,x)
to match up with c(y,z)
. The point type can be changed using the pch
argument, 1 creates circles and 2 creates triangles (more options are available).
To add the lines to the graph we use the line()
function which takes the xy coordinates as the first two arguments, then the line colour can be specified along with the line type. There are again additional arguments that can be used to change the line weight, offsets etc.
The final part of this example is the addition of a legend using the legend()
function. The first argument is the position of the legend. This can be specified using a key word as shown below or specific coordinates. The labels are the next argument to be entered and these are concatenated into a vector.
The colour and line type need to then be specified using the col
and lty
calls. If creating a legend for a scatter plot you would use pch
instead of lty
to specify the point type.
Input:
x <- c(0,5,10,15,20,25,30)
y <- c(81,95,102,120,110,108,92)
z <- c(78,90,100,118,125,112,96)
plot(c(x,x),c(y,z),xlab="Time",ylab="Heart rate",main="Patient heart rate during exercise",pch=c(1,2))
lines(x,y,col="red",lty=1)
lines(x,z,col="blue",lty=2)
legend("topleft",c("Patient 1","Patient 2"),col=c("red","blue"),lty=c(1,1))
Output: