Text size to tile size ratio in geom_tile

71 views
Skip to first unread message

Alexander Predeus

unread,
Apr 4, 2015, 1:16:05 PM4/4/15
to ggp...@googlegroups.com
Hello all, 

I'm trying to make a heatmap using geom_tile, and so far it's working really well. However, I can't find a way to control the ratio of text size and tile size. 

Below, I use some tricks to make sure that in the resulting SVG file, tiles look approximately like squares. What I'm looking for is if there's a way to fix the ratio of height/width of tile in the final SVG, and ratio of these measurements to font size.

Here's an example of input and scripts that I am using to make a heatmap. 

Input file "table.txt":

Spermatids_wildtype_rep2_GSM722457 Spermatids_wildtype_rep1_GSM722458 Spermatids_wildtype_rep3_GSM722459 Spermatids_wildtype_rep4_GSM722460 Spermatids_sh367_rep1_GSM722461 Spermatids_sh367_rep2_GSM722462 Spermatids_shSLX1_rep1_GSM722463 Spermatids_shSLX1_rep2_GSM722464
0 -0.2080 -0.8163 0.6512 -0.1983 -1.0000 1.0000 -0.0912 -0.2344
1 -0.5354 -0.5369 0.6374 0.5665 -0.9029 -1.0000 1.0000 0.5760
2 1.0000 0.8603 -0.4087 -0.3665 0.9428 0.9411 -1.0000 -0.6362
3 -0.3785 -0.2970 -0.7447 -0.6239 1.0000 0.8149 -0.5175 -1.0000
4 -0.1792 -0.2843 1.0000 0.8936 -0.2926 -0.5569 -1.0000 0.4640
5 0.1164 0.0676 -1.0000 -0.8588 -0.6495 -0.1821 1.0000 -0.2743
6 -0.8594 -0.5773 -0.9122 -1.0000 -0.6111 -0.9415 0.4893 1.0000
7 0.8527 0.6401 0.2275 0.1333 -1.0000 -0.7931 1.0000 0.3063
8 0.7446 0.4455 -1.0000 -0.8670 0.4982 1.0000 0.7450 -0.5098
9 1.0000 0.2473 0.4379 0.2746 -0.0163 -0.1307 -1.0000 -0.0349
10 1.0000 0.9730 -0.8686 -1.0000 0.8020 0.9126 -0.8830 0.9766
11 -0.1946 -0.0268 0.5958 1.0000 0.6004 0.4368 -1.0000 -0.4747
12 -0.4535 -0.2511 -0.9505 -1.0000 0.5858 0.6110 0.6338 1.0000
13 0.2489 0.1224 0.6202 1.0000 -0.8839 -1.0000 -0.0264 -0.6591
14 0.4920 0.1954 0.2130 0.0464 -0.3245 -0.5113 -1.0000 1.0000
15 1.0000 0.3454 -0.1172 -0.3290 -0.6369 -0.2290 0.0891 -1.0000
16 -1.0000 -0.6441 -0.0095 0.2357 1.0000 -0.0229 0.7442 -0.3155
17 -0.2106 -0.2043 -0.0491 -0.1816 0.9811 1.0000 -1.0000 0.6551
18 0.2254 0.1823 0.3827 -0.0613 0.2672 0.1040 1.0000 -1.0000
19 -0.0661 -0.0694 -1.0000 -0.8220 0.0947 1.0000 -0.2007 -0.5139
20 0.5394 1.0000 -0.8235 -0.8280 -1.0000 -0.4263 -0.8121 0.4181
21 1.0000 0.5141 0.6701 -0.4018 -0.4417 -1.0000 -0.8490 -0.0604

Commands:

library(ggplot2)
library(reshape)
fname<-"table.txt"
tt<-read.table(fname,header=T,row.names=NULL)
nsmp <- ncol(tt)-1
nmod <- nrow(tt)-1
colnames(tt)[1]<-"Module"
tt.m<-melt(tt)
names(tt.m)<-c("Module","Sample","value")
tt.m$Module<-as.factor(tt.m$Module)
levels(tt.m$Sample)<-sort(levels(tt.m$Sample))
levels(tt.m$Module) <- paste(sort(as.integer(levels(tt.m$Module))))
title_length <- max(nchar(names(tt)))
svg_height <- round(10*nsmp/(0.15*title_length+nmod+1),digits=3)

p <- ggplot(tt.m, aes(Module,Sample)) + geom_tile(aes(fill = value), colour = "black",size=0.5) + scale_fill_gradient2(low = "blue", mid="white",high = "red")
base_size <- 9
p2 <- p + theme_grey(base_size=base_size)+labs(x="",y="")+scale_x_discrete(expand=c(0,0))+scale_y_discrete(expand =c(0,0))+theme(legend.position ="none",axis.text.y=element_text(size=base_size*1.2,colour="black"),axis.text.x=element_text(size=base_size*1.2,colour="black"))
xmin <- 4.5
xmax <- 5.5
ymin <- 0.5
ymax <- nsmp+0.5
rect <- data.frame(xmin=4.5, xmax=5.5, ymin=0.5, ymax=8.5)
p3 <- p2 + geom_rect(data=rect, aes(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax),color="black",fill="grey",alpha=0, size=3, inherit.aes = F)
ggsave(file="test.svg", plot=p3, width=10, height=svg_height) 

Doug Mitarotonda

unread,
Apr 4, 2015, 4:45:56 PM4/4/15
to Alexander Predeus, ggp...@googlegroups.com
Take a look at the `rel` function and see if that works for you.

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages