Hi Thiago,
As with other geoms, you can use factor() in your geom_raster to get R to treat your z variable as categories. So it would be:
geom_raster(data=df, aes(x, y, fill = factor(z)))
Some things to note.
First, your random function is going to return continuous values, so they'll all be different with virtual certainty. I assume in your real implementation, you have data that more "naturally" inclines itself toward categories. If your data really are continuous, the gradient color probably is the best way to display them.
Second, when you're picking the manual colors you mentioned, bear in mind the levels in a factor are sorted alphabetically. This is how ggplot2 maps the vectors values and labels to your factor. So when assigning labels and colors (through scale_color_manual), it'll look like this:
df <- data.frame(x=1:20, y=1:20, z=c(rep('x',5), rep('z',5), rep('w',5), rep('a',5)))
ggplot() +
geom_point(data=df, aes(x=x, y=y, color=z), shape=15,size=20) +
scale_color_manual(values=c("red","brown","ivory","yellow"),
labels=c("apples","walnuts","xylophones","zits"))
Hope this helps!
- Peter