0

I'd like to generate separate plots according to combinations of two factors in a dataframe.

I've got most of the way there using the advice below, where it's suggested that employing plyr is the best approach.
How subset a data frame by a factor and repeat a plot for each subset?
Unlike the example given above however, I need to generate plot using base R (specifically matplot).

I'd ideally like to add appropriate titles to each plot (e.g. the specific 'sample, phase') so that they can be identified. I can't for the life of me figure out how best I might do that though. Any help please?

A simplified example is given below

# simplified dataframe
dat <- data.frame(sample = c("A", "A", "A", "B", "B", "B", "B", "C", "C"),
              phase = c("W", "X", "X", "W", "X", "Z", "Z", "X", "X"),
              La = c(0.01, 0.015, 0.2, 0.35, 0.6, 0.02, 0.5, 0.1, 0.15),
              Lu = c(0.85, 0.95, 0.7, 0.55, 0.9, 0.75, 0.55, 0.9, 0.65))

# define the function which generates the plots
ree_plot <- function(z) {
  plot(z)
}

# use plyr to split dataframe and generate plots according to sample and name combinations
plyr::dlply(.data = dat[,3:4], .variables = .(dat$sample, dat$phase), .fun = ree_plot)

Many thanks

Community
  • 1
  • 1
pyg
  • 716
  • 6
  • 18
  • You're welcome. I submitted it as an answer so that you can mark the question as solved. – lukeA Oct 25 '15 at 11:59

1 Answers1

0

You could try

ree_plot <- function(z) 
  plot(z[, 3:4], main = paste0(unlist(z[1, 1:2]), collapse=",")) 

library(plyr)
dlply(.data = dat, .variables = .(dat$sample, dat$phase), .fun = ree_plot)
lukeA
  • 53,097
  • 5
  • 97
  • 100