I have a raster map that I want to plot using ggplot2 using a continuous scale and labeled isolines on top of that.
For that I'm using the directlabels package and am close to getting what I want but I can't get both the legend and the labeled isolines on the same map
The following code reproduces my problem:
install.packages(c('ggplot2', 'directlabels'))
library('ggplot2')
library('directlabels')
df <- expand.grid(x=1:100, y=1:100)
df$z <- df$x * df$y
# Plot 1: this plot is fine but without contours
p <- ggplot(aes(x=x, y=y, z=z), data = df) +
geom_raster(data=df, aes(fill=z)) +
scale_fill_gradient(limits=range(df$z), high = 'white', low = 'red')
p
# Plot 2: This plot adds the isolines but no labels and it also adds a second legend for level which I don't want
p <- p + geom_contour(aes(colour = ..level..), color='gray30', na.rm=T, show.legend=T)
p
# Plot 3: This plot has the labeled isolines but it removes the z legend that I want to show
direct.label(p, list("bottom.pieces", colour='black'))
Plot 1
Plot 2
Plot 3
I would like to have the coloured raster in the background, with it's color legend on the side and the labeled isolines on top. Is there a way to do this?
Also is there a way to get the labels placed in the middle of the isolines instead of the bottom or top?
Thanks in advance
Pablo