719016
719016

Reputation: 10431

R simplify heatmap to pdf

I want to plot a simplified heatmap that is not so difficult to edit with the scalar vector graphics program I am using (inkscape). The original heatmap as produced below contains lots of rectangles, and I wonder if they could be merged together in the different sectors to simplify the output pdf file:

nentries=100000
ci=rainbow(nentries)
set.seed=1
mean=10
## Generate some data (4 factors)
i = data.frame(
  a=round(abs(rnorm(nentries,mean-2))),
  b=round(abs(rnorm(nentries,mean-1))),
  c=round(abs(rnorm(nentries,mean+1))),
  d=round(abs(rnorm(nentries,mean+2)))
  )
minvalue = 10
# Discretise values to 1 or 0
m0 = matrix(as.numeric(i>minvalue),nrow=nrow(i))
# Remove rows with all zeros
m = m0[rowSums(m0)>0,]
# Reorder with 1,1,1,1 on top
ms =m[order(as.vector(m %*% matrix(2^((ncol(m)-1):0),ncol=1)), decreasing=TRUE),]
rowci = rainbow(nrow(ms))
colci = rainbow(ncol(ms))

colnames(ms)=LETTERS[1:4]
limits=c(which(!duplicated(ms)),nrow(ms))
l=length(limits)
toname=round((limits[-l]+ limits[-1])/2)
freq=(limits[-1]-limits[-l])/nrow(ms)

rn=rep("", nrow(ms))
for(i in toname) rn[i]=paste(colnames(ms)[which(ms[i,]==1)],collapse="")
rn[toname]=paste(rn[toname], ": ", sprintf( "%.5f", freq ), "%")

heatmap(ms,
        Rowv=NA,
        labRow=rn,
        keep.dendro = FALSE,
        col=c("black","red"),
        RowSideColors=rowci,
        ColSideColors=colci,
        )

dev.copy2pdf(file="/tmp/file.pdf")

Upvotes: 7

Views: 4142

Answers (3)

boulder_ruby
boulder_ruby

Reputation: 39695

I don't think we (the internet) are being clear enough on this one.

Let me just start off with a successful export example

png("heatmap.png") #Ruby dev's think of this as kind of like opening a `File.open("asdfsd") do |f|` block
heatmap(sample_matrix, Rowv=NA, Colv=NA, col=terrain.colors(256), scale="column", margins=c(5,10))
dev.off()

The dev.off() bit, in my mind, reminds me of an end call to a ruby block or method, in that, the last line of the "nested" or enclosed (between png() and dev.off()) code's output is what gets dumped into the png file.

For example, if you ran this code:

png("heatmap4.png")
heatmap(sample_matrix, Rowv=NA, Colv=NA, col=terrain.colors(32), scale="column", margins=c(5,15))
heatmap(sample_matrix, Rowv=NA, Colv=NA, col=greenred(32), scale="column", margins=c(5,15))
dev.off()

it would output the 2nd (greenred color scheme, I just tested it) heatmap to the heatmap4.png file, just like how a ruby method returns its last line by default

Upvotes: 0

Marc in the box
Marc in the box

Reputation: 11995

I use the Cairo package for producing svg. It's incredibly easy. Here is a much simpler plot than the one you have in your example:

require(Cairo)
CairoSVG(file = "tmp.svg", width = 6, height = 6)
plot(1:10)
dev.off()

Upon opening in Inkscape, you can ungroup the elements and edit as you like.

Example (point moved, swirl added):

enter image description here

Upvotes: 1

annndrey
annndrey

Reputation: 1788

Why don't you try RSvgDevice? Using it you could save your image as svg file, which is much convenient to Inkscape than pdf

Upvotes: 1

Related Questions