Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")
like image 430
719016 Avatar asked Feb 10 '13 12:02

719016


People also ask

How do you read a heatmap?

How do I read a heatmap? You can read any website heatmap in two ways: by looking at the visualization and by reviewing the raw data points. You can spot click trends and issues at a glance thanks to the color-coded nature of heatmaps (red means the most interaction, blue the least).


1 Answers

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

like image 69
annndrey Avatar answered Oct 15 '22 21:10

annndrey