Chapter 6 - Graphical Parameters

6.1 Defining Colours

# specify colour by number
#  col= 1,          # black
#  col= c(1,2,3),   # vector of colours 1,2,3
# col= c(1:3),     # range of colours 1 to 3 - same as above
# specify colour by name
#  col= "grey",     
#  col= c("grey10","grey30","grey80"),
par(mfrow=c(1,1),mar=c(3,0,2,0))
x <- rep(1:8)
y <- rep(1, times = 8)
plot(x,y, col= 1:8, pch= 15, cex= 3, ylim= c(0,2), yaxt= "n",
     main= "Colour Numbers in R: 'col = 1 to 8'")

Figure 6.1: Plot showing standard colour numbers (1 to 8) in R.

6.2 Defining Symbols (Points)

# default point types by number
#  pch= 2,              
#  pch= c(1,2,3),      
#  pch= c(1:3),         
# setting points as characters
#  pch= "A",            
#  pch= c("N","S","E","W"),
# point size (cex)
#  pch= 2, cex= 3,         
#  pch= c(1,2,3), cex= 1.2,   
# add colour (col,bg)
#  pch= 2, col= 5,           
#  pch= 22, col= 4, bg= 2,   
# combine parameters
# points 1,2,5 all 20% larger, and using colours 2 to 4
#  pch= c(1,2,5), cex= 1.2, col= c(2:4), 
# the most common situation for changing the points is for a scatter plot
# for example:
with(mtcars,plot(mpg ~ wt, cex= c(1,2,3), col= c(3,4,5), bg= (8)))

Figure 6.2: Plot showing available symbols by number in R.

6.3 Defining Line Types

Figure 6.3: Plot showing available line types in R (1 to 6).

# line type
#  lty= 1,              # solid line
#  lty= c(1,5,3),       # list of lines 1,5,3
#  lty= c(1:6),         # range of points 1 to 3 - same as above
# line width
#  lty= 1, lwd= 0.5,         # solid lines, 50% default size
#  lty= c(1,2,3), lwd= 1.2,  # lines 1,2,3, all 20% larger
# add colour
#  lty= 1, col= 4,           # blue solid line
#  lty= c(1,5), col= c(3,4), # green solid and blue dashed lines
# combine all parameters
# lines 1,2,3, all red, and using line widths 2 to 4
#  lty= c(1,2,3), col= 2, lwd= c(2:4), 
#  the most common situation for controlling the line type is adding 
#  the trend line to a scatter plot.  For example:
plot(mtcars$vs)
abline(v= c(5,10,15,20,25,30), col = c(1,2,3), lty= c(1,2,3,4,5,6), lwd= c(4:6))

Figure 6.3: Plot showing available line types in R (1 to 6).

6.4 Saving Plots

#1. Create an empty file in your working directory with your 
#   specified parameters
png("Figure1.png", width = 100, height = 100, units = 'mm', res = 300)
#2. Create your plot
with(PlantGrowth,hist(weight,
      xlim= c(3,7),                       # set the x-axis range
      ylim= c(0,10),                      # set the y-axis range
      col= "lightgray",                   # fill the columns
      border= "black",                    # column border
      main= "Histogram of Plant Weights", # figure title
      xlab= "Dried weight (g)",           # x-axis label
      ylab= "Frequency"))                 # y-axis label
#3. Save your plot by turning device off
dev.off()
## png 
##   2
## png 
##   2