The following code is used to produce a correlation matrix heatmap in ggplot2
set.seed(1)
n <- 10
p <- 100
X <- matrix(rnorm(n*p), ncol=p)
R <- cor(X)
library("reshape2")
x <- melt(R)
colnames(x) <- c('row', 'col', 'cor')
library("ggplot2")
HM <- ggplot(x, aes(x=row, y=col, fill = cor)) + geom_tile()
HM <- HM + scale_fill_gradient2(low='red', mid ='white', high ='blue', midpoint=0, guide = "colourbar")
HM <- HM + scale_x_discrete(expand = c(0, 0))
HM <- HM + scale_y_discrete(expand = c(0, 0))
HM <- HM + coord_equal()
HM <- HM + labs(title = "Correlation Matrix") [![enter image description here][1]][1]
HM <- HM + theme(plot.title = element_text(hjust = 0.5),
axis.text=element_blank(),
axis.ticks=element_blank(),
axis.title=element_blank(),
plot.margin = unit(c(0,0,0,0), "cm"))
print(HM)
The code produce the following picture
It is nice, but I have two questions
when I print the pdf the white side margins are a waste of space especially when I put figure in a row. I tried to use
mar
andoma
paramter inpar()
but this do not produce any difference.the legend on the right does not cover the range [-1,1] this is because the min correlation in the matrix is larger than -1. But still I'd like to have the legend to code the whole range [-1,1]
if possible I'd like to have the legend box justified on top and not at the center of the vertical axes, I mean I'd like it starts from north-east corner down.
Any ideas?
Cheers Pierre