0

I am having a problem with a couple R functions I wrote. I have 2 plotting functions and I want to write a function that creates plots for both of the functions. This function looks like this

 plotBoth = function(path = NULL){
     plotPopulationStats(path)
     plotInstructionFrequencies(path)
 }

However only whichever instruction I call second gets plotted, while the first instruction only plots a blank window. Below are simplified versions of these functions.

 plotInstructionFrequencies = function(path = NULL){
   quartz()
   table <- read.table(path, header=TRUE);
   frame <- as.data.frame(table);
   frame$time = seq(1, length(table$noop));
   frame$mean = NULL
   frame$sd = NULL
   frame$variance = NULL
   Molten <- melt(frame, id.vars = "time");
   ggplot(Molten, aes(x = time, y = value, colour = variable)) + geom_line()
}

and

plotPopulationStats = function(path = NULL){
    quartz()
    table <- read.table(path, header=TRUE);
    frame <- as.data.frame(table);
    frame$time = seq(1, length(table$noop));
    frame$noop = NULL
    frame$plus = NULL
        ...
    frame$store = NULL
        Molten <- melt(frame, id.vars = "time");
    ggplot(Molten, aes(x = time, y = value, colour = variable)) + geom_line();
}

All the functions are in separate files, but I would like to have them all in one file.

Both functions plot fine when called individually.

pb2q
  • 58,613
  • 19
  • 146
  • 147
wfbarksdale
  • 7,498
  • 15
  • 65
  • 88
  • possible duplicate of [Generate multiple graphics from within an R function](http://stackoverflow.com/questions/2547306/generate-multiple-graphics-from-within-an-r-function) – Ari B. Friedman Sep 22 '12 at 21:10

1 Answers1

3

Read Faq 7.22: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f

You need to print() your ggplot or lattice plots.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • That did the trick, I enclosed changed the `ggplot(...)...` to `print(ggplot(...)...)` and it worked. – wfbarksdale Nov 18 '11 at 20:32
  • 1
    Everyone working with lattice or ggplot looses at least half a day to the old "why doesn't my plot plot" problem :). – Paul Hiemstra Nov 19 '11 at 07:43
  • I remember that Superguru Douglas Bates called Deepayan in the middle of the night when the latter was in India on the same subject. – Dieter Menne Nov 19 '11 at 09:11