1

I can't figure out how to define fixed cutoffs in a gradient fill. This is an example on the iris data:

ggplot(iris, aes(x=Sepal.Length,
                 y=Sepal.Width,
                 fill=Petal.Length))+
  geom_point(shape=21)+
  scale_fill_gradientn(colors=c("white","black","darkred","red"))

In my actual data, I have a heatmap with many values 0, a lot between 1-10, a few outliers in the hundreds and thousands.

I would like the gradient to be:

  • 0 = white
  • 1-10 = from gray to black
  • 11-100 = from black to darkred
  • 101-1000 = from darkred to red
  • over 1000 = purple

I don't want to transform it in a factor with bins, because I do want to maintain the continuous gradient especially at the low parts of the scale.

Thank you for you help

Carl
  • 4,232
  • 2
  • 12
  • 24
  • Does this answer your question? [Create discrete color bar with varying interval widths and no spacing between legend levels](https://stackoverflow.com/questions/50506832/create-discrete-color-bar-with-varying-interval-widths-and-no-spacing-between-le) – tjebo Jul 02 '20 at 20:23

1 Answers1

1

You will have to generate you own color pal with colorspace for the white to red, but if you're okay with the default blues or viridis, you can have that out of the box with scale_fill_binned():

ggplot(iris, aes(x=Sepal.Length,
                 y=Sepal.Width,
                 fill=Petal.Length))+
  geom_point(shape = 21, size = 4) +
  scale_fill_binned(breaks = c(2, 5), type = "viridis")

enter image description here

https://ggplot2.tidyverse.org/reference/guide_coloursteps.html

Nate
  • 10,361
  • 3
  • 33
  • 40