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
First, fixing the issue to do with the legends.
library(ggplot2)
library(directlabels)
df <- expand.grid(x=1:100, y=1:100)
df$z <- df$x * df$y
p <- ggplot(aes(x=x, y=y, z=z), data = df) +
geom_raster(data=df, aes(fill=z), show.legend = TRUE) +
scale_fill_gradient(limits=range(df$z), high = 'white', low = 'red') +
geom_contour(aes(colour = ..level..)) +
scale_colour_gradient(guide = 'none')
p1 = direct.label(p, list("bottom.pieces", colour='black'))
p1
There aren't too many options for positioning the labels. One possibility is angled.boxes
, but the fill
colour might not be too nice.
p2 = direct.label(p, list("angled.boxes"))
p2
To change the fill
colour to transparent (using code from here.
p3 = direct.label(p, list("far.from.others.borders", "calc.boxes", "enlarge.box",
box.color = NA, fill = "transparent", "draw.rects"))
p3
And to move the labels off the contour lines:
p4 = direct.label(p, list("far.from.others.borders", "calc.boxes", "enlarge.box",
hjust = 1, vjust = 1, box.color = NA, fill = "transparent", "draw.rects"))
p4