I have the following data frame:
df
zone X Y Value
1 604000 2673000 522
1 612000 2643000 524
. . . .
. . . .
615 604000 2673000 698
In fact I have 615 zones, X and Y are the Lambert coordinates and Value is the rain. My zones represents France and I am trying to plot them:
mi <- 300
ma <- 1900
df$val <- cut(df$Sim, breaks=seq(mi,ma,100),
labels = paste( "(", format(seq(mi,ma,100)[1:(length(seq(mi,ma,100))-1)]),
", ", format(seq(mi,ma,100)[-1]), "]", sep = ""))
breaks <- unique(bincol(df$Sim,"green","yellow","red"))
breaks <- breaks[order(unique(df$val))]
p <- ggplot(data =df, aes(x=X, y=Y))
p <- (p
+ theme_bw()
+ coord_equal()
+ geom_tile(aes(fill=val))
+ scale_colour_manual("mm", values = breaks)
+ geom_path(data = polfrance, colour = 'black',
aes(x = long, y = lat, group = group))
+ geom_path(data = sympzones, colour = 'grey40',
aes(x = long, y = lat, group = group))
)
p <- (p + scale_y_continuous(limits=c(1580000,2730000),
breaks=seq(1600000,2700000, 200000),
labels= seq(1600,2700,200), expand=c(0,0))
+ scale_x_continuous(limits=c(0,1250000),
breaks= seq(0,1250000, 400000),
labels= seq(0,1250, 400), expand=c(0,0))
+ xlab("X Lambert [km]")
+ ylab("Y Lambert [km]")
)
I use sympzones and polfrance to draw the contour of my zones.
AND
bincol <- function(x,low,medium,high) {
breaks <- function(x) pretty(range(x), n = nclass.Sturges(x), min.n = 1)
colfunc <- colorRampPalette(c(low, medium, high))
binned <- cut(x,breaks(x))
res <- colfunc(length(unique(binned)))[as.integer(binned)]
names(res) <- as.character(binned)
res
}
Values are from 300 to 1900 and this is what I obtain:
I have a problem:
I can't change the legend, this is the legend from geom_file and it doesn't take into account the scale_colour_manual. So this is not an ascending order, not the good title (I want "mm") and not the good colors.
I don't know if it is really clear... Can someone help me?
Edit: I took inspiration from that: easiest way to discretize continuous scales for ggplot2 color scales?