7.3 Modifying a graph
7.3.1 Graphical parameters
To change the layout of a plot or to change a certain aspect of a plot such as the line type or symbol type, you will need to change certain graphical parameters. We have seen some in the previous section. The graphical functions in R accept graphical parameters as extra arguments. These graphical parameters are usually three or four letter abbreviations (like col, cex or mai). The following use of the plot function,
plot(x,y, xlim=c(-3.3))
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
will set the minimum and maximum values of the x-axis. It is also possible to use the function par to set graphical parameters. Some graphical parameters can only be set with this function. A call to the function par has the following form:
par(gp1 = value1, gp2 = value2)
In the above code the graphical parameter gp1 is set to value1, graphical parameter gp2 is set to value2 and so on. Note that some graphical parameters are read only and cannot be changed. Run the function par with no arguments to get a complete listing of the graphical parameters and their current values.
par()
$xlog [1] FALSE
$ylog [1] FALSE
$adj [1] 0.5
$ann [1] TRUE
$ask [1] FALSE
$bg
[1] "transparent"
...
etc.
We will discuss some useful graphical parameters. See the help file of par for a more detailed description and a list of all the graphical parameters. Once you set a graphical parameter with the par function, that graphical parameter will keep its value until you:
• Set the graphical parameter to another value with the par function.
• Close the graph. R will use the default settings when you create a new plot.
When you specify a graphical parameter as an extra parameter to a graphical function, the current value of the graphical parameter will not be changed. Some example code:
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
## define some data x <- rnorm(10) y <- rnorm(10) z <- rnorm(10)
## set plotting color to red par(col="red")
plot(x,y)
## draw extra blue points points(x,z, col="blue")
## draw red points again points(y,z)
The Plot and figure regions, the margins
A graph consists of three regions. A ‘plot region’ surrounded by a ‘figure regions’ that is in turn surrounded by four outer margins. The top, left, bottom and right margins.
See figure 7.7. Usually the high level plot functions create points and lines in the plot region.
Outer margin 3
Outer margin 1
Outer margin 2 Outer margin 4
Plot region
Figure region
Figure 7.7: The different regions of a plot
The outer margins can be set with the oma parameter, the four default values are set to zero. The margins surrounding the plot region can be set with the mar parameter.
Experiment with the mar and oma parameters to see the effects.
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
## Default values par(c("mar", "oma"))
$mar
[1] 5.1 4.1 4.1 2.1
$oma
[1] 0 0 0 0
## set to different values par(oma=c(1, 1, 1, 1))
par(mar=c(2.5, 2.1, 2.1, 1)) plot(rnorm(100))
Multiple plots on one page
Use the parameter mfrow or mfcol to create multiple graphs on one layout. Both parameters are set as follows:
par(mfrow=c(r,k)) par(mfcol=c(r,k))
where r is the number of rows and k the number of columns. The graphical parameter mfrow fills the layout by row and mfcol fills the layout by column. When the mfrow parameter is set, an empty graph window will appear and with each high-level plot command a part of the graph layout is filled. We have seen an example in the previous section, see figure 7.6.
A more flexible alternative to set the layout of a plotting window is to use the function layout. An example, three plots are created on one page, the first plot covers the upper half of the window. The second and third plot share the lower half of the window.
## first argument is a matrix with integers
## specifying the next n graphs nf = layout(
rbind(
c(1,1), c(2,3) )
)
## If you are not sure how layout has divided the window
## use layout.show to display the window splits
## layout.show(nf)
plot(rnorm(100),type="l") hist(rnorm(100))
qqnorm(runif(100))
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
0 20 40 60 80 100
−2−1012
Index
rnorm(100)
Histogram of rnorm(100)
rnorm(100)
Frequency
−2 −1 0 1 2
051020
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●●
●●
●
●
●
● ●
●●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●●
●
●
●
●
−2 −1 0 1 2
0.00.40.8
Normal Q−Q Plot
Theoretical Quantiles
Sample Quantiles
Figure 7.8: The plotting area of this graph is divided with the layout function.
The matrix argument in the layout function can contain 0’s (zero’s), leaving a certain sub plot empty. For example:
nf = layout(
rbind(
c(1,1), c(0,2) )
)
Other settings
The following list shows some more parameters, these are usually set as an argument of the plotting routine. For example, plot(x,y, col=2).
• lwd, the line width of lines in a plot, a positive number default lwd=1.
• lty, the line type of lines in a plot, this can be a number or a character. For example lty="dashed".
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
• col, the color of the plot, this can be a number or a character. For example col
= "red".
• font, an integer specifying which font to use for text on plots.
• pch, an integer or character that specifies the plotting symbols, in scatterplots for example.
• xlab, ylab, character strings that specify the labels of the x and y axis. Usually given direct with the high-level plotting functions like plot or hist.
• cex, character expansion. A numerical value that gives the amount to scale the plotting symbols and texts. The default is 1.
Some of the graphical parameters may be set as vectors so that each point, text or symbol could have its own graphical parameter. This is another way to display an additional dimension. Lets look at a plot with different symbols, for the cars data set we can plot the ‘Price’ and ‘Mileage’ variables in a scatterplot and have different symbols for the different ‘Types’ of cars.
Ncars = dim(cars)[1]
plot(
cars$Price, cars$Mileage, pch = as.integer(cars$Type) )
legend(20000,37,
legend = levels(cars$Type), cex=1.25, pch=1:6
)
The color palette
The graphical parameter col can be a vector. This can be used to create a scatterplot of ‘Price’ and ‘Mileage’ where each point has a different color depending on the ‘Weight’
value of the car. To do this, we first need to change the color palette in R. The color palette specifies which colors corresponds with the numbers 1,2,3,... in the specification col = number. The current palette can be printed with the function palette.
palette()
[1] "black" "red" "green3" "blue" "cyan"
[6] "magenta" "yellow" "gray"
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
This means plot(rnorm(100), col=2) will create a scatterplot with red points. The function can also be used to change the palette. Together with a few auxiliary functions (heat.colors, terrain.colors, gray), it is easy to create a palette of colors, say from dark to light red.
palette(heat.colors(Ncars)) palette()
[1] "red" "#FF2400" "#FF4900" "#FF6D00" "#FF9200" "#FFB600"
[7] "#FFDB00" "yellow" "#FFFF40" "#FFFFBF" ...
So in the color palette, col=1 represents red, col=2 a slightly lighter red and so on.
Then in the plot function we specify col=order(cars$Weight), the largest value has order number Ncars. The following code uses several (plot) functions to create a colored scatterplot and a color legend.
## split the screen in two, the larger left part will contain
## the scatter plot the right side contains a color legend layout(matrix(c(1, 2), nc = 2), widths = c(4, 1))
## create the scatterplot with different colors plot(
cars$Price, cars$Mileage, pch = 16, cex = 1.5, col = order(cars$Weight) )
## do some calculations for the color legend, determine
## minimum and maximum weight values.
zlim = range(cars$Weight, finite = TRUE)
## lets use 20 color values in the color legend levels = pretty(zlim, 20)
## start the second plot that is the color legend plot.new()
plot.window(
xlim = c(0, 1),
ylim = range(levels), xaxs = "i", yaxs = "i"
)
## use the function rect to draw multiple colored rectangles rect(
0, levels[-length(levels)], 1, levels[-1],
col = terrain.colors(length(levels) - 1) )
## draw an axis on the right-hand side of the legend axis(4)
CHAPTER 7. GRAPHICS 7.3. MODIFYING A GRAPH
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
10000 15000 20000 25000
20253035
cars$Price
cars$Mileage
● Compact Large Medium Small Sporty Van
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●
●● ● ●
●
●
●
●
● ●
●
●
●
●
● ●
●
●
●
●
●
10000 15000 20000 25000
20253035
cars$Price
cars$Mileage 2000250030003500
Figure 7.9: Examples of different symbols and colors in plots
To set the color palette back to default use palette("default").