1

A previous post provided a solution to iteratively store plots in R: see ... iteratively in R... . I had a similar problem and after reading and implementing the solutions provided by the post I am still unable to solve my problem.

The previous post provided the following code:

# Create a list to hold the plot objects.

pltList <- list()

for( i in 2:15 ){

# Get data, perform analysis, ect.

# Create plot name.
pltName <- paste( 'a', i, sep = '' )

# Store a plot in the list using the name as an index.
pltList[[ pltName ]] <- plot()

}

The following is my code implementation:

a <- list.files("F:.../4hrs", pattern='.csv')
pltList <- list()
i=1

for (x in a) {
    myfiles <- read.csv(a, header=TRUE, as.is=TRUE, nrows=2500)

    h <- hist(data, plot=F)
    # perform analysis, ect.

    pltName <- paste('a', formatC(i, width=2, flag='0'), sep='')
    pltList[[ pltName ]] <- plot(h)
    i <- i+1

  }

pltName does produce a list of names but pltList is of length zero.

I am not sure why pltList is not being assigned the plots.
What I eventually want to do is create a pltList with multiple plots contained therein. Then plot those plots in par(mfrow=c(2,1)) style and export as a .pdf.

I should mention that the above works for

pltList[[ pltName ]] <- xyplot(h)

but then I am unable to plot multiple plots in the style of par(mfrow=c(2,1)).

Any suggestions are appreciated.

Community
  • 1
  • 1
wisfool
  • 99
  • 8
  • This strategy will only work for lattice and ggplot2 plots because base R plots usually don't return anything. – hadley Feb 20 '11 at 00:22

2 Answers2

1

In the original question you referenced and my answer to it, plot() was used as an abstract placeholder for a plotting function that returned an object and not a literal call to the R function plot. Most functions that return graphics objects are based on the 'grid' package such as xyplot from the lattice package or qplot from ggplot2.

Sorry for the confusion, I should have made this point clear in my answer, but I did not as the asker of the question was already aware of it.

Base graphics functions such as hist and plot render directly to output and do not return any objects that can be used to recreate the plot at a later time which is why you are ending up with a list of length zero.

In order to use this approach you will have to trade usage of the hist function for something that returns an object. Using ggplot2, you could do something like the following in your loop:

# Don't call your data variable 'data' or ggplot will confuse it with the
# `data` function and suffer an error.
h <- qplot(x = plot_data)

pltName <- paste('a', formatC(i, width=2, flag='0'), sep='')
pltList[[ pltName ]] <- h

I have edited my answer to the previous question to make it clear that the use of plot() in my example is not an actual call to the R function of the same name.

Sharpie
  • 17,323
  • 4
  • 44
  • 47
  • Thanks Sharpie... this really helped in clarifying my 'object-less' assignment. I am using `xyplot()` with the lattice package. `pltList[[ pltName ]] <- xyplot()` – wisfool Feb 20 '11 at 01:46
0

Your code uses files I don't have so I can't replicate it, I am also not entirely sure what you are trying to accomplish, but I do see some problems in the code that might help fix it:

a <- list.files("F:.../4hrs", pattern='.csv')

I am not fammiliar with list.files, Is this correctly assigning a? .csv seems an odd pattern.

pltList <- list()
i=1

for (x in a) {


 myfiles <- read.csv(a, header=TRUE, as.is=TRUE, nrows=2500)

Here I think a is a vector containing the filenanes right? You are looping x for every value of a, however I don't see x return anywhere in the code. Also you are reading a vector of filenames here. Shouldnt this be read.csv(x,..., or better yet, loop for (i in 1:length(a)) and index a[i].

    h <- hist(data, plot=F)

I don't see the object data anywhere. Is h correctly assigned?

# perform analysis, ect.

    pltName <- paste('a', formatC(i, width=2, flag='0'), sep='')
    pltList[[ pltName ]] <- plot(h)
    i <- i+1
  }

What I like to do is simply run such a loop by hand, and see what is going on. I think there is a problem in the assigning of myfiles or h

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • `a<-list.files()` just provides the names of the files that I would like to read. `myfiles <- read.csv(a, ...)` actually reads in the data. As Sharpie mentioned above Base Graphics do not create an object so `h` is not correctly assigned. Thanks for you suggestion! – wisfool Feb 20 '11 at 01:20