Basic tutorial on R plotting

Setting up

# install new packages (once)
# install.packages('planar',repos='http://cran.r-project.org') planar is
# similar to Splac
library(ggplot2)  # load a package
# ggplot2 is for plotting

getwd()  # where is the current working dir
## [1] "/Users/auguieba/Documents/r/github/tutorial"
setwd("/Users/auguieba/Documents/r/github/tutorial/")  # set the location

Loading data

# list all files with a pattern in the current wd
list.files(pattern = "txt")
## [1] "1_radius_0-01_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [2] "2_radius_0-02_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [3] "3_radius_0-03_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [4] "4_radius_0-04_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [5] "5_radius_0-05_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [6] "6_radius_0-06_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [7] "7_radius_0-07_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [8] "8_radius_0-08_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
## [9] "9_radius_0-09_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
fname <- "9_radius_0-09_medium_1_coating_1-52_thickness_0_gold_Mie_2012-04-21.txt"
d <- read.table(fname, header = TRUE)

Basic functions to check the data

str(d)  # structure
## 'data.frame':    801 obs. of  4 variables:
##  $ wavelength: num  0.4 0.401 0.402 0.403 0.404 0.405 0.406 0.407 0.408 0.409 ...
##  $ extinction: num  0.0831 0.0831 0.0831 0.0831 0.0831 ...
##  $ scattering: num  0.0434 0.0434 0.0435 0.0435 0.0435 ...
##  $ absorption: num  0.0396 0.0396 0.0396 0.0396 0.0396 ...
names(d)  # names
## [1] "wavelength" "extinction" "scattering" "absorption"
summary(d)
##    wavelength    extinction        scattering        absorption     
##  Min.   :0.4   Min.   :0.00545   Min.   :0.00476   Min.   :0.00069  
##  1st Qu.:0.6   1st Qu.:0.01225   1st Qu.:0.01132   1st Qu.:0.00093  
##  Median :0.8   Median :0.03645   Median :0.03476   Median :0.00169  
##  Mean   :0.8   Mean   :0.05126   Mean   :0.04097   Mean   :0.01029  
##  3rd Qu.:1.0   3rd Qu.:0.08639   3rd Qu.:0.06083   3rd Qu.:0.01243  
##  Max.   :1.2   Max.   :0.12667   Max.   :0.11066   Max.   :0.04324  
head(d)
##   wavelength extinction scattering absorption
## 1      0.400    0.08306    0.04342    0.03964
## 2      0.401    0.08306    0.04344    0.03962
## 3      0.402    0.08307    0.04346    0.03961
## 4      0.403    0.08308    0.04347    0.03960
## 5      0.404    0.08308    0.04349    0.03959
## 6      0.405    0.08309    0.04350    0.03958
plot(d[, 1], d[, 2])  # basic plot

plot of chunk basic

Plotting a data.frame

plot(d[, 1], d[, 2], type = "l", col = "red", lty = "dashed", lwd = 3, 
    xlab = expression(Wavelength/mu * m), ylab = "Cross-section", main = "Mie theory for a gold sphere")

plot of chunk basicplot


# ?plot to see help on plot function and all options, also ?par

# adding lines to it
lines(d[, 1], d[, 3], col = "blue")

plot of chunk basicplot

# ... or points
d2 <- d[seq(1, nrow(d), by = 10), ]
points(d2[, 1], d2[, 4], col = "green")

plot of chunk basicplot

Plotting multiple columns at once

matplot(d[, 1] * 1000, d[, 2:4], type = "l", col = 1:3, lty = "solid", 
    xlab = "Wavelength / nm", ylab = "Cross-section")
legend("topright", c("Extinction", "Scattering", "Absorption"), lty = 1, 
    col = 1:3)

plot of chunk multiplot

ggplot2 plotting

ggplot(data = d, mapping = aes(x = wavelength, y = extinction)) + 
    geom_path()

plot of chunk ggplot

Often, you need to reshape the data from wide to long format,

library(reshape2)
m <- melt(d, id.var = "wavelength")
str(d)
## 'data.frame':    801 obs. of  4 variables:
##  $ wavelength: num  0.4 0.401 0.402 0.403 0.404 0.405 0.406 0.407 0.408 0.409 ...
##  $ extinction: num  0.0831 0.0831 0.0831 0.0831 0.0831 ...
##  $ scattering: num  0.0434 0.0434 0.0435 0.0435 0.0435 ...
##  $ absorption: num  0.0396 0.0396 0.0396 0.0396 0.0396 ...
str(m)
## 'data.frame':    2403 obs. of  3 variables:
##  $ wavelength: num  0.4 0.401 0.402 0.403 0.404 0.405 0.406 0.407 0.408 0.409 ...
##  $ variable  : Factor w/ 3 levels "extinction","scattering",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ value     : num  0.0831 0.0831 0.0831 0.0831 0.0831 ...
p <- ggplot(data = m, mapping = aes(x = wavelength * 1000, y = value)) + 
    geom_path(mapping = aes(colour = variable))
p

plot of chunk mggplot

p + facet_grid(variable ~ .) + labs(x = "Wavelength / nm", y = "Cross-section", 
    colour = "Type") + theme_bw()

plot of chunk mggplot

Read in and combine multiple files

l <- list.files(patt = "txt")  # list files
ld <- llply(l, read.table, head = TRUE)  # read each file and store in a list
names(ld) <- paste("file", seq_along(ld))  # give unique names
m <- melt(ld, id.var = "wavelength")
str(m)
## 'data.frame':    21627 obs. of  4 variables:
##  $ wavelength: num  0.4 0.401 0.402 0.403 0.404 0.405 0.406 0.407 0.408 0.409 ...
##  $ variable  : Factor w/ 3 levels "extinction","scattering",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ value     : num  0.000107 0.000107 0.000106 0.000106 0.000106 ...
##  $ L1        : chr  "file 1" "file 1" "file 1" "file 1" ...

These data can be plotted,

ggplot(data = m, mapping = aes(x = wavelength * 1000, y = value)) + 
    facet_wrap(~L1, ncol = 3, scales = "free") + geom_path(mapping = aes(colour = variable)) + 
    labs(x = "Wavelength / nm", y = "Cross-section", colour = "Type") + theme_bw() + 
    opts(strip.background = theme_blank())

plot of chunk mfggplot