Introduction

This document demonstrates different ways of generating scatter plots with the base, CAR, lattice and ggplot2 plotting packages. Plots make use of the iris dataset.

library(knitr)
knitr::opts_chunk$set(tidy=T, 
               fig.width=5,
               fig.height=5,
               fig.align='left',
               warning=FALSE,
               message=FALSE,
               echo=TRUE)
options(width = 120)
library(ggplot2)
attach(iris)

Base Plotting Package

plot(Sepal.Length, Petal.Length,
     main="Flower Characteristics in Iris",
     xlab="Sepal Length",
     ylab="Petal Length",
     pch=16, cex=1,
     col=Species)
legend(x=4.5, y=7,
       legend=levels(Species),
       col=c(1:3),
       pch=16)
abline(lm(Petal.Length ~ Sepal.Length), col="purple")
lines(lowess(Sepal.Length, Petal.Length), col="blue")

CAR Package

# Enhanced Scatterplot of MPG vs. Weight 
# by Number of Car Cylinders 
library(car) 
scatterplot(Sepal.Length ~ Petal.Length | Species, data=iris,
            main="Flower Characteristics in Iris",
            xlab="Sepal Length",
            ylab="Petal Length",
            labels=row.names(iris),
            legend.coords = "topleft")

scatterplotMatrix(~ Petal.Length + Petal.Width + Sepal.Length + Sepal.Width | Species,
                   data=iris,
                   main="Flower Characteristics in Iris",
                   legend.pos="bottomright")

Correlation Matrix

kable(cor(iris[1:4]), 
      format='markdown', 
      caption="Correlation Matrix",
      digits=4)
Sepal.Length Sepal.Width Petal.Length Petal.Width
Sepal.Length 1.0000 -0.1176 0.8718 0.8179
Sepal.Width -0.1176 1.0000 -0.4284 -0.3661
Petal.Length 0.8718 -0.4284 1.0000 0.9629
Petal.Width 0.8179 -0.3661 0.9629 1.0000

Lattice Plotting Package

# Scatterplot Matrices from the lattice Package 
library(lattice)
super.sym <- trellis.par.get("superpose.symbol")
splom(~iris[1:4], groups = Species, data = iris,
      panel = panel.superpose,
      key = list(title = "Flower Characteristics in Iris",
                 columns = 3, 
                 points = list(pch = super.sym$pch[1:3],
                 col = super.sym$col[1:3]),
                 text = list(c("setosa", "versicolor", "virginica"))))

ggplot2 Plotting Package

ggplot(iris,aes(x=Petal.Width,y=Petal.Length,shape=Species,colour=Species)) +
  geom_point() +
  xlab("Petal Width (cm)") +
  ylab("Petal Length (cm)") +
  theme_bw() +
  ggtitle("Flower Characteristics in Iris\n")

ggplot(iris,aes(x=Petal.Width,y=Petal.Length)) +
  geom_point(colour="purple") +
  xlab("Petal Width (cm)") +
  ylab("Petal Length (cm)") +
  theme_bw() +
  ggtitle("Flower Characteristics in Iris\n") +
  stat_smooth()

Programming Environment

sessionInfo()
## R version 3.3.3 (2017-03-06)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: macOS Sierra 10.12.3
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] lattice_0.20-35 car_2.1-4       ggplot2_2.2.1   knitr_1.15.1   
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.10       magrittr_1.5       splines_3.3.3      MASS_7.3-45        munsell_0.4.3      colorspace_1.3-2  
##  [7] minqa_1.2.4        highr_0.6          stringr_1.2.0      plyr_1.8.4         tools_3.3.3        parallel_3.3.3    
## [13] nnet_7.3-12        pbkrtest_0.4-7     grid_3.3.3         nlme_3.1-131       gtable_0.2.0       mgcv_1.8-17       
## [19] quantreg_5.29      MatrixModels_0.4-1 htmltools_0.3.5    yaml_2.1.14        lme4_1.1-12        lazyeval_0.2.0    
## [25] rprojroot_1.2      digest_0.6.12      tibble_1.3.0       Matrix_1.2-8       formatR_1.4        nloptr_1.0.4      
## [31] evaluate_0.10      rmarkdown_1.4      labeling_0.3       stringi_1.1.3      scales_0.4.1       backports_1.0.5   
## [37] SparseM_1.76