I'm having problems building a raster with coefficients using ggplot2.
library(ggplot2) library(raster)
load raster data first
f <- system.file("external/test.grd", package="raster") r <- raster(f)
extract coordinates and values
val <- getValues(r) xy <- as.data.frame(xyFromCell(r,1:ncell(r))) xy <- cbind(xy,val)
build the grid using geom_raster (). Everything is working fine.
ggplot(xy, aes(x=x, y=y, fill=val)) + geom_raster() + coord_equal()
I do not have a continuous raster, but classified. Expand the raster:
r <- reclass(r, c(0,500,1, 500,2000,2)) val <- getValues(r) xy <- as.data.frame(xyFromCell(r,1:ncell(r))) xy <- cbind(xy,val)
draw a classified raster. Also OK, but the legend is continuous
ggplot(na.omit(xy), aes(x=x, y=y, fill=val)) + geom_raster() + coord_equal()
If I plot the values ββas a factor, the map will become wrong
ggplot(na.omit(xy), aes(x=x, y=y, fill=factor(val))) + geom_raster() + coord_equal()