Search code examples
graphicssvginkscaper

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")

Solution

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