Quantitative Genomics and Genetics

Computer Lab 5

– 25 September 2014

– Author: Jin Hyun Ju (jj328@cornell.edu)

1. Scatter plots revisited

  • The function plot() is one of the built in basic plotting functions that R has. It does not generate the most beautiful plots, but it is very useful for initial visualization of the data.

  • plot(x,y) will plot the given x vector against the other y vector (they must have the same length). If you simply give it 1 vector like this: plot(x), it will assume that the x axis is a sequence of integers starting at 1 and ending at the length of x.

# Visualization of a single vector 
# We are going to use the mauna loa co2 data set from the built in R datasets package!
# If you want to learn more about it type ?co2

# If we just plot the dataset (which is in a time-series format):

plot of chunk unnamed-chunk-1

# Let's change it into a matrix to make it easier to deal with
mauna.loa <- matrix(co2, ncol = 12, byrow=T)
rownames(mauna.loa) <- c(1959:1997)

# Plotting only the values with a default index

plot of chunk unnamed-chunk-1

# By specifying options using the par() function you can control the way plots are generated
# If you want to have multiple plots in the same window
par(mfrow =c(1,3))
# the two numbers specify the number of rows and columns. So in this case it will plot 3 plots in 1 row.

# 1st Plot
# Plotting it with a specified x axis
plot(c(1959:1997),mauna.loa[,1], xlab="Year", ylab="CO2", main = "Mauna Loa CO2 - January",type ="l")
# xlab = x.axis label / y.lab = y.axis label / main = plot title / type = "l" for line

# 2nd Plot

# 3rd Plot

plot of chunk unnamed-chunk-2

par(mfrow = c(1,2))
# If you are interested in revealing a correlation structure between two vectors
plot(mauna.loa[1,],mauna.loa[2,], xlab="1959 - CO2", ylab="1960 - CO2", main = "1959 vs 1960",type = 'p' )

# Using different point styles with the options pch
plot(mauna.loa[1,],mauna.loa[2,], xlab="1959 - CO2", ylab="1960 - CO2", main = "1959 vs 1960",type = 'p' , pch = 17)

plot of chunk unnamed-chunk-3

  • Here is a reference card for different pch shapes.