Chapter 5 - Scatter Plots

5.1 Creating a Basic Scatter Plot

head(iris)  # prints the first few rows of the iris data set
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
## 3          4.7         3.2          1.3         0.2  setosa
## 4          4.6         3.1          1.5         0.2  setosa
## 5          5.0         3.6          1.4         0.2  setosa
## 6          5.4         3.9          1.7         0.4  setosa
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1          5.1         3.5          1.4         0.2  setosa
#> 2          4.9         3.0          1.4         0.2  setosa
#> 3          4.7         3.2          1.3         0.2  setosa
#> 4          4.6         3.1          1.5         0.2  setosa
#> 5          5.0         3.6          1.4         0.2  setosa
#> 6          5.4         3.9          1.7         0.4  setosa
with(iris, plot(Petal.Length ~ Petal.Width))

### 5.2 Optional Parameters

par(mfrow=c(1,1),mar=c(5,4,2,4))
with(iris,plot(Petal.Length ~ Petal.Width, 
     col= "purple",                    # points colour
     pch= 5,                           # symbol 5 for rhombus/diamond
     main= "Petal Lengths and Widths", # figure title
     ylab= "Petal length (cm)",        # y-axis label
     xlab= "Petal width (cm)",         # x-axis label
     ylim= c(0,8),                     # y-axis range
     xlim= c(0,3),                     # x-axis range
     las= 1))                          # horizontal axis labels

### 5.3 Scatter Plots with a Grouping Variable

par(mfrow=c(1,1),mar=c(5,4,2,4))
with(iris,plot(Petal.Length ~ Petal.Width, 
     col= Species,    # colour points by variable 'Species'
     pch= 16,         # 16 = filled circle (easier to see differences) 
     main= "Petal Lengths and Widths by Species", 
     ylab= "Petal length (cm)",        
     xlab= "Petal width (cm)",                 
     ylim= c(0,8),                    
     xlim= c(0,3),                   
     las= 1))                           

# check order of species to make sure we get the legend details correct
summary(iris$Species) # gives summary for only Species in iris data
##     setosa versicolor  virginica 
##         50         50         50
#>     setosa versicolor  virginica 
#>         50         50         50
# create legend
legend("bottomright",                         # put in bottom right corner
       title= "Species",                      # add a title
       legend= c("setosa","versicolor","virginica"), # specify species order
       col= 1:3,                              # add range of colours
       pch= 16,                               # use same symbol as in plot
       bty= "n")                              # remove legend box outline

### 5.4 Advanced Scatter Plot Features

par(mar=c(5,4,0,4))
#1. scatter plot with specified colours for grouping variable
with(iris,plot(Petal.Length ~ Petal.Width, 
     # specify colours and tell R to apply them to the variable 'Species'           
     col= c("darkorchid1","darkorchid","darkorchid4")[as.numeric(Species)],
     pch= 19))                                           # define symbol
# add legend
legend("topleft",                            
       title= "Species",           
       legend= c("setosa","versicolor","virginica"),     # list species' order
       col= c("darkorchid1","darkorchid","darkorchid4"), # list colours
       pch= 19,                                          # define symbol
       bty="n")                                          # remove outline

par(mar=c(5,4,2,4))
#2. scatter plot with specified colours and symbols for grouping variable
with(iris,plot(Petal.Length ~ Petal.Width,
     # specify colours and tell R to apply them to the variable 'Species' 
     col= c("grey10","grey50","grey80")[as.numeric(Species)],
     # specify symbols and tell R to apply them to the variable 'Species'
     pch= c(6,19,21)[as.numeric(Species)],
     main= "Petal Lengths and Widths",
     ylab= "Petal length (cm)",
     xlab= "Petal width (cm)",
     ylim= c(0,8),
     xlim= c(0,3),
     las= 1))
# add legend
legend("bottomright",               
       title= "Species",           
       legend= c("setosa","versicolor","virginica"),     # specify species' order
       col= c("grey10","grey50","grey80"),               # specify colours
       pch= c(6,19,21),                                  # specify symbols
       bty="n")                                          # remove outline