Tingolfin
Tingolfin

Reputation: 935

ggplot2 scale_fill_gradient2 not showing mid colour

I'm trying to build a plot with a colour gradient. All works fine with 2 colours (low, high) but when using scale_fill_gradient2() for a 3rd colour (mid), nothing happens. Well something happens: mid replaces low and I still end up with 2 colours. My R version and all my packages are up-to-date.

My data:

> head(Data)
       Datum  X  Y Total
1 31/05/2012 21  5    36
2 31/05/2012 21 19     3
3 31/05/2012 11 25   100
4 31/05/2012 21 11    16
5 31/05/2012 11 17     0
6 31/05/2012 21 23    14

My code:

ggplot(Data, aes(X, Y)) +
  xlim(0,40) +
  ylim(0,50) +
  coord_equal() +
  geom_raster(aes(fill=Total)) +
  scale_fill_gradient2(low="darkgreen", mid="black", high="red")

enter image description here

Any ideas?

PS: If a problem-solver also knows the answer to this question, it's welcome: Notice my bars to be very long, the one on x = 11 reaches all the way to x = 21. Is there a way to make them shorter?

Upvotes: 3

Views: 3126

Answers (1)

fkm
fkm

Reputation: 56

You probably want to set midpoint = 0 and state limits = c(-max(Data$Total), max(Data$Total)) to extend the range of the colorbar.

Upvotes: 4

Related Questions