Creating a Basic Scatter Plot

head(iris)  
##   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))

Optional Parameters

par(mfrow=c(1,1),mar=c(5,4,2,4))
with(iris,plot(Petal.Length ~ Petal.Width, 
     col= "purple",                    
     pch= 5,                          
     main= "Petal Lengths and Widths", 
     ylab= "Petal length (cm)",        
     xlab= "Petal width (cm)",        
     ylim= c(0,8),                     
     xlim= c(0,3),                     
     las= 1))                          

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,   
     pch= 16,         
     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))                           


summary(iris$Species) 
##     setosa versicolor  virginica 
##         50         50         50
#>     setosa versicolor  virginica 
#>         50         50         50

legend("bottomright",                         
       title= "Species",                      
       legend= c("setosa","versicolor","virginica"), 
       col= 1:3,                             
       pch= 16,                             
       bty= "n")                              

Advanced Scatter Plot Features

par(mar=c(5,4,0,4))

with(iris,plot(Petal.Length ~ Petal.Width, 
             
     col= c("darkorchid1","darkorchid","darkorchid4")[as.numeric(Species)],
     pch= 19))  

legend("topleft",                            
       title= "Species",           
       legend= c("setosa","versicolor","virginica"),     
       col= c("darkorchid1","darkorchid","darkorchid4"), 
       pch= 19,                                          
       bty="n")                                          

par(mar=c(5,4,2,4))

with(iris,plot(Petal.Length ~ Petal.Width,
    
     col= c("grey10","grey50","grey80")[as.numeric(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))

legend("bottomright",               
       title= "Species",           
       legend= c("setosa","versicolor","virginica"),     
       col= c("grey10","grey50","grey80"),               
       pch= c(6,19,21),                                  
       bty="n")