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