I'd like to plot a geom_raster with colors based on a specific classification:
I tried the following code (variable = SPI) but it didn't work:
scale_fill_gradient(colours = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue"),
breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c("-2","-1.5","-1","1","1.5","2")))
Example:
year <- seq(1977,2021,1)
jan = runif(45, min=-4, max=4)
feb = runif(45, min=-4, max=4)
mar = runif(45, min=-4, max=4)
apr = runif(45, min=-4, max=4)
may = runif(45, min=-4, max=4)
jun = runif(45, min=-4, max=4)
jul = runif(45, min=-4, max=4)
aug = runif(45, min=-4, max=4)
sep = runif(45, min=-4, max=4)
oct = runif(45, min=-4, max=4)
nov = runif(45, min=-4, max=4)
dec = runif(45, min=-4, max=4)
df = data.frame(year,jan,feb,mar,apr,may,jun,jul,aug,sep,oct,nov,dec)
df <- reshape2::melt(df, id.vars = "year")
df$year <- factor(df$year, levels = (unique(df$year)))
df$variable <- factor(df$variable, levels = (unique(df$variable)))
library(ggplot2)
e1 <- ggplot(df, aes(x = variable, y = year, fill = value)) +
geom_raster()+
guides(fill=guide_legend(title="Bohicon"))+
scale_fill_gradient(colours = c( "brown","burlywood","bisque","aliceblue","cadetblue2","blue"),
breaks=c(-2,-1.5,-1,1,1.5,2), labels = format(c("-2","-1.5","-1","1","1.5","2")))+
theme(legend.position="bottom")
e1
Many thanks and kind regards!