Correlation plot in R
1
0
Entering edit mode
@lcarvalhbtkfi-5223
Last seen 10.2 years ago
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
• 1.3k views
ADD COMMENT
0
Entering edit mode
Marcus Davy ▴ 390
@marcus-davy-5153
Last seen 6.7 years ago
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]]
ADD COMMENT

Login before adding your answer.

Traffic: 647 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6