0

I am having some troubles applying a gradient fill to my area plot.

The data is as below:

> df
   year  annual
1  1960  0.0100
2  1961 -0.2700
3  1962 -0.3450
4  1963 -0.6508
5  1964 -0.9458
6  1965 -0.2458
7  1966  0.9492
8  1967  0.5383
9  1968  0.6275
10 1969  0.0000

I've set up a colorRampPalette for the gradient, and I know this works.

spi.cols <- colorRampPalette(c("darkred","red","yellow","white","green","blue","darkblue"),space="rgb")

With the plot, my aim is to have the fill colours follow the values in the annual column. So as to make it easy to tell that values are within certain boundaries. Right now, the plot seems to think every value it is "filling" is equal to zero, and is thus filling it all in one colour only.

ggplot(df, aes(x = year)) +
   geom_polygon(aes(y = annual, fill = annual)) +
   theme_classic() +
   scale_fill_gradientn(colours = spi.cols(12), limits = c(-2.5, 2.5), guide = "legend")

I have also specified the breaks I'd like in my gradient, but I'm not sure how to utilise this. I attempted to use this in values of the scale_fill_gradientn but this was unsuccessful.

enter image description here

spi.breaks <- c(-2.5,-2,-1.6,-1.3,-0.8,-0.5,0.5,0.8,1.3,1.6,2,2.5)

Any help would be much appreciated

Quinn
  • 419
  • 1
  • 5
  • 21
  • This could help: http://stackoverflow.com/questions/33965018/ggplot-how-to-produce-a-gradient-fill-within-a-geom-polygon – Philipp Merkle Feb 25 '17 at 16:21
  • Related: [How to make gradient color filled timeseries plot in R](http://stackoverflow.com/questions/27250542/how-to-make-gradient-color-filled-timeseries-plot-in-r) – Henrik Feb 25 '17 at 16:41

0 Answers0