Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing default scale gradient ggplot2

I am newbie, I am trying to desing a heat map. This is my code:

ggplot(gd, aes(Qcountry, Q6_1_Q6d), order = TRUE) +
  geom_tile(aes(fill = prob), colour = "white") +
  theme_minimal() +
  labs( y = "Main reason for mobility", x = "Country") +
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.3)) +
  scale_fill_gradient(name = "(%)")

Which produces a perfect chart, my problem is low levels are dark blue, and higher values are light blue, which is not intuitive. Most common way to do is use rev(). But in my case I dont know how to. So, is it possible to reverse this default scale? This is the legend

Other question, is there a way to create a scale gradient only with one colour. I mean, scale_fill_gradient/scale_fill_gradientn need to set a low color and high color (low = "", high = "") and I want to change the blue by red.

Thanks so much for your support.

like image 568
Tito Sanz Avatar asked Apr 20 '17 09:04

Tito Sanz


1 Answers

?scale_colour_gradient shows the default values of low = "#132B43" and high = "#56B1F7".

Simply switch those around:

ggplot(faithfuld, aes(waiting, eruptions)) +
    geom_raster(aes(fill = density)) +
    scale_fill_continuous(high = "#132B43", low = "#56B1F7")

enter image description here

Personally, I think this is less intuitive than the default.


Alternatively, you can use a reverse scale, but this will also flip the legend to start at the top:

ggplot(faithfuld, aes(waiting, eruptions)) +
    geom_raster(aes(fill = density)) +
    scale_fill_continuous(trans = 'reverse')

enter image description here

like image 191
Axeman Avatar answered Oct 14 '22 05:10

Axeman