[R] how to save a heatmap.2 in png /jpeg /tiff

2,039 views
Skip to first unread message

STADLER Frederic

unread,
Sep 9, 2012, 10:04:44 AM9/9/12
to r-h...@r-project.org
Hey, I am still working on my heat map (for those who are read my previous post about row.names)…
Now, I would like to save my heat map.2 in .png or .tiff in order being able to work on the picture in photoshop, but it doesn't work.
I'am using (as I have found on some forum)
> png("heatmap.2.png) # and it just doesn't work. when I try doing it with::
>jpeg("heatmap.2.jpeg) # it works once every 10 times, but it's a 22kb file. completely use less !!!

I really need to have high quality image, as I will have to work on photoshop and also I will have to cut and zoom in just some lines of my heatmap.

#here is the code I use for my heatmap.2 :
>heatmap.2(a_matrix, Rowv=NA, Colv =NA, col=greenred(60), scale="column", margins=c(7,10), trace="none", density.info=c("none"))

Does someone know what I have to do in order to get my heatmap.2.png ??? Do I need some other package (I only use gplots, to allow the heatpmap.2)

THANKS for your help
Fred

[[alternative HTML version deleted]]

Sarah Goslee

unread,
Sep 9, 2012, 11:26:03 AM9/9/12
to STADLER Frederic, r-h...@r-project.org
Normally the workflow is:

png("heatmap.png") # don't forget the second quote, as you did below
my.plot.code # whatever you need to draw the heatmap
dev.off() # people often forget this step - did you?

You'll probably want to adjust the size and resolution settings for
png() to get the desired high-resolution output; see ?png for details.

Sarah
--
Sarah Goslee
http://www.functionaldiversity.org

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Jeff Newmiller

unread,
Sep 9, 2012, 11:41:34 AM9/9/12
to STADLER Frederic, r-h...@r-project.org
"It just doesn't work" could mean anything... and for those of us for whom it does work, that leaves a lot of possible differences between your case and ours. This is your cue to read the Posting Guide.

Some issues I have encountered:

If you are using Windows, and you have opened a graphics file in an editor, and you try to write a new version out with R, the editor will prevent this change in most cases. You have to remember to close the graphics file first.

Also, you need to remember to close the file in R using dev.off() when you are done writing to it for similar reasons.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdne...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

STADLER Frederic <frederic...@unifr.ch> wrote:

>------------------------------------------------------------------------

Fred

unread,
Sep 9, 2012, 1:36:31 PM9/9/12
to r-h...@r-project.org
hey Sarah,
thanks for your help !!

Of Course I put the second quote also (I forgot to put it on the last post).
Sorry, I don't get the my.plot.code... # I'm new in R and use it only to
draw heatmaps right now.
Well, I did forget the dev.off(). # but I got
null device (1) # when quartz is turned off and when it's on I get : "
quartz 2".

But I don't have any files called "heatmap.2.png " on my computer.
I really don't understand why I don't get anything !

and when I do:
> jpeg ("heatmap.jpg") # it works but I get only a 20kb picture which is
> useless in my case (edit and work on it in photoshop)

Fred



--
View this message in context: http://r.789695.n4.nabble.com/how-to-save-a-heatmap-2-in-png-jpeg-tiff-tp4642607p4642615.html
Sent from the R help mailing list archive at Nabble.com.

Fred

unread,
Sep 9, 2012, 1:44:31 PM9/9/12
to r-h...@r-project.org
Hey Jeff, sorry for the "it just doesn't work", but it's really what it
does...
I can't find any file called heatmap.2.png on my computer after creating my
heatmap.2 (that I can see on Quartz) and typing:
> png("heatmap.2.png")
Maybe I should add, that I am working on mac with MAC OS X (v.10.8.1).!!

It is not a problem of having a picture editor open when I try to create
this file.

Any other idea ??
Thanks

Fred



--
View this message in context: http://r.789695.n4.nabble.com/how-to-save-a-heatmap-2-in-png-jpeg-tiff-tp4642607p4642617.html
Sent from the R help mailing list archive at Nabble.com.

Hasan Diwan

unread,
Sep 9, 2012, 2:54:13 PM9/9/12
to Fred, r-h...@r-project.org
Mr Stadler,

On 9 September 2012 10:36, Fred <frederic...@unifr.ch> wrote:

> But I don't have any files called "heatmap.2.png " on my computer.
> I really don't understand why I don't get anything !
>

What does getwd() print out as a path? Check there for the your file. -- H
--
Sent from my mobile device
Envoyait de mon portable

[[alternative HTML version deleted]]

Rui Barradas

unread,
Sep 9, 2012, 3:23:35 PM9/9/12
to Fred, r-h...@r-project.org
Hello,


Em 09-09-2012 18:36, Fred escreveu:
> hey Sarah,
> thanks for your help !!
>
> Of Course I put the second quote also (I forgot to put it on the last post).
> Sorry, I don't get the my.plot.code...

What Sarah meant is that you must put your.plot.code between the
instructions that open and close the graphics device. This is example 1
from the 'gplots::heatmap.2' help page, adapted.

# From ?gplots::heatmap.2
library(gplots)
data(mtcars)
x <- as.matrix(mtcars)


# Plot nothing, but like Jeff said (suggested) it does something
# it opens the device and closes it
png(file = "myplot.png", bg = "transparent")
dev.off() # 318 bytes file in current directory

# Plot an heatmap.2, example 1 in ?gplots::heatmap.2
png(file = "heatmap2.png")
heatmap.2(x) ## default - dendrogram plotted and reordering done.
dev.off() ## 10Kb file in current dir

# The same but to a jpeg graphics device
jpeg(file = "heatmap2.jpeg")
heatmap.2(x) ## same as above
dev.off() ## 46Kb file


Hope this helps,

Rui Barradas

David Winsemius

unread,
Sep 9, 2012, 9:04:22 PM9/9/12
to STADLER Frederic, r-h...@r-project.org

On Sep 9, 2012, at 7:04 AM, STADLER Frederic wrote:

> Hey, I am still working on my heat map (for those who are read my previous post about row.names)∑
> Now, I would like to save my heat map.2 in .png or .tiff in order being able to work on the picture in photoshop, but it doesn't work.
> I'am using (as I have found on some forum)
>> png("heatmap.2.png) # and it just doesn't work. when I try doing it with::
>> jpeg("heatmap.2.jpeg) # it works once every 10 times, but it's a 22kb file. completely use less !!!

Neither of those should have _ever_ "worked", since they both are missing closing quotes.

Furthermore, just emitting the command jpeg("filename.jpg") even with proper closing quotes will be completely useless, as you say, unless you follow the plot() command with dev.off().

?Devices
?jpeg # and please DO the examples

>
> I really need to have high quality image, as I will have to work on photoshop and also I will have to cut and zoom in just some lines of my heatmap.
>
> #here is the code I use for my heatmap.2 :
>> heatmap.2(a_matrix, Rowv=NA, Colv =NA, col=greenred(60), scale="column", margins=c(7,10), trace="none", density.info=c("none"))
>
> Does someone know what I have to do in order to get my heatmap.2.png ??? Do I need some other package (I only use gplots, to allow the heatpmap.2)
>

Pleaese include complete code. What you have provided so far should, as you say, be "completely useless!!!".

David Winsemius, MD
Alameda, CA, USA

Fred

unread,
Sep 10, 2012, 3:48:44 AM9/10/12
to r-h...@r-project.org
Thanks everyone for your comments and help.
Actually, I found my mistake (probably stupid for y'all). I was drawing
first the heatmap and then putting the
>png("heatmap.2.png").

What you have to do is :
>a<- read.csv (".... .csv)
>a_matrix <- data.matrix (a)
>row.names(a_matrix) <-a$Name # In my case I wanted to have the name from
the column Name...
>a_matrix <-a_matrix [,2:5] # To delete the first column containing the
Name.
>library(gplots)
>png("heatmap.2.png", width= 10000, height = 10000, units = "px",
bg="white")
>heatmap.2(a_matrix, Rowv=NA, Colv = NA, col= topo.colors(250),
scale=column, margins=c(7,10), trace="none", density.info=c("none"))


The only problem I have now, is taht I get a nice png file of 2Mb, but I
can't read the name of the genes anymore.!!
2 questions :
1) Do I need to play with the "pointsize = "
2) What is the higher quality picture I can make ? When I try with more
pixels "R " tends to crash...

THANKS

Fred



--
View this message in context: http://r.789695.n4.nabble.com/how-to-save-a-heatmap-2-in-png-jpeg-tiff-tp4642607p4642641.html
Sent from the R help mailing list archive at Nabble.com.

PIKAL Petr

unread,
Sep 10, 2012, 4:39:43 AM9/10/12
to Fred, r-h...@r-project.org
Hi

> -----Original Message-----
> From: r-help-...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Fred
> Sent: Sunday, September 09, 2012 7:45 PM
> To: r-h...@r-project.org
> Subject: Re: [R] how to save a heatmap.2 in png /jpeg /tiff
>
> Hey Jeff, sorry for the "it just doesn't work", but it's really what it
> does...
> I can't find any file called heatmap.2.png on my computer after
> creating my
> heatmap.2 (that I can see on Quartz) and typing:
> > png("heatmap.2.png")
> Maybe I should add, that I am working on mac with MAC OS X
> (v.10.8.1).!!
>
> It is not a problem of having a picture editor open when I try to
> create this file.
>
> Any other idea ??

I was not following this thread from beginning but does

png("heatmap.2.png")
plot(1:10)
dev.off()

produce the file named "heatmap.2.png" in your working directory?

Petr
Reply all
Reply to author
Forward
0 new messages