Scatterplot

Problem

You want to make a scatterplot.

Solution

Suppose this is your data:

  1. set.seed(955)
  2. # Make some noisily increasing data
  3. dat <- data.frame(xvar = 1:20 + rnorm(20,sd=3),
  4. yvar = 1:20 + rnorm(20,sd=3),
  5. zvar = 1:20 + rnorm(20,sd=3))
  6. head(dat)
  7. #> xvar yvar zvar
  8. #> 1 -4.252354 3.473157275 -2.97806724
  9. #> 2 1.702318 0.005939612 -1.16183118
  10. #> 3 4.323054 -0.094252427 4.85516658
  11. #> 4 1.780628 2.072808278 4.65078709
  12. #> 5 11.537348 1.215440358 -0.06613962
  13. #> 6 6.672130 3.608111411 6.24349897

Basic scatterplots

  1. # Plot the points using the vectors xvar and yvar
  2. plot(dat$xvar, dat$yvar)
  3. # Same as previous, but with formula interface
  4. plot(yvar ~ xvar, dat)
  5. # Add a regression line
  6. fitline <- lm(dat$yvar ~ dat$xvar)
  7. abline(fitline)

plot of chunk unnamed-chunk-3plot of chunk unnamed-chunk-3

Scatterplot matrices

It is also possible to make a matrix of scatterplots if you would like to compare several variables.

See this for a way to make a scatterplot matrix with r values.

  1. # A scatterplot matrix
  2. plot(dat[,1:3])
  3. # Another way of making a scatterplot matrix, with regression lines
  4. # and histogram/boxplot/density/qqplot/none along the diagonal
  5. library(car)
  6. scatterplotMatrix(dat[,1:3],
  7. diagonal="histogram",
  8. smooth=FALSE)

plot of chunk unnamed-chunk-4plot of chunk unnamed-chunk-4

To calculate the corresponding correlation matrix, see ../../Statistical analysis/Regression and correlation.

To visualize the correlation matrix, see ../Correlation matrix.