It is because you need to use print() inside the function for the lattice
plot, since it is not the last line being returned.

Try this function version;

saveCorrelationPlot <- function(data, fName, PNG=TRUE) {
  require(lattice)

  if(PNG) png(file=fName, width=600, height=600)

  x <- levelplot(cor(data), col.regions = rainbow(100,start=.1,
end=.35),scales =
list(x = list(rot = 45)), ylab="", xlab="", main="" )
  print(x)

  if(PNG) dev.off()
  return(invisible())
}


Marcus


On Thu, Apr 19, 2012 at 6:47 PM, <lcarvalh@btk.fi> wrote:

>
>
> I have a problem when  creating a correlation plot when called within a
> function, if called directly there is no problem. Can anyone find out why?
> The
> code is below.
>
> Code:
>
> load("norm.matrix.RData")
>
> require(lattice)
>
>
> #This works and the figure is produced:
>
> png(file="correlation.png", width=600, height=600)
> levelplot(cor(data), col.regions = rainbow(100,start=.1, end=.35),scales =
> list(x = list(rot = 45)), ylab="", xlab="", main="" )
> dev.off()
>
>
> #The following does not produce a figure, why?
>
> saveCorrelationPlot <- function(data, fName) {
>        #quartz()
>        x11()
>        png(file=fName, width=600, height=600)
>        levelplot(cor(data), col.regions = rainbow(100,start=.1,
> end=.35),scales =
> list(x = list(rot = 45)), ylab="", xlab="", main="" )
>        dev.off()
> }
>
> saveCorrelationPlot(data, "correlation2.png")
>
>
> Thanks,
>
> Leonor
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor@r-project.org
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives:
> http://news.gmane.org/gmane.science.biology.informatics.conductor
>

	[[alternative HTML version deleted]]

