Multiple gradients on the same fill

1,085 views
Skip to first unread message

Doug Mitarotonda

unread,
Aug 8, 2013, 3:45:11 PM8/8/13
to ggplot2
Hi,
I have a continuous fill variable that goes across a wide range of values and I was thinking the output might look better if I had multiple color gradients across certain ranges of the variable. scale_fill_gradient provides you low and high, but that only allows you to use one set of colors. 

df <- data.frame(
  x = c(rep(1, 3), rep(2, 6), rep(2, 4), rep(3, 2)),
  y = c(1:3, 1:6, 1:4, 1:2),
  fll = sample(0:100, 15, TRUE))

## Works, but only goes from red to blue
p <- ggplot(df, aes(x = x, y = y, fill = fll)) + 
  geom_raster() +
  scale_fill_gradient(low = "red", high = "blue")
## Theoretically want something like (with some sort of way to specify ranges)
# + scale_fill_gradient(low = "blue", high = "green")

Any thoughts on how I could do this, or something similar? 

My ideas so far are:
  1. Figure out each gradient range manually and then use scale_fill_manual to stack them up and apply the values there.
  2. Create multiple plots, where I change the limits and low/high arguments to scale_fill_gradient to basically show each range on its own (with the others greyed out).
  3. Use the trans argument to transform the scale. I tried that with log but it still didn't look great.

Any suggestions are greatly appreciated.

Best wishes,
Doug

Brian Diggs

unread,
Aug 9, 2013, 2:48:26 PM8/9/13
to Doug Mitarotonda, ggplot2
On 8/8/2013 12:45 PM, Doug Mitarotonda wrote:
> Hi,
> I have a continuous fill variable that goes across a wide range of values and I was thinking the output might look better if I had multiple color gradients across certain ranges of the variable. scale_fill_gradient provides you low and high, but that only allows you to use one set of colors.
>
> df <- data.frame(
> x = c(rep(1, 3), rep(2, 6), rep(2, 4), rep(3, 2)),
> y = c(1:3, 1:6, 1:4, 1:2),
> fll = sample(0:100, 15, TRUE))
>
> ## Works, but only goes from red to blue
> p <- ggplot(df, aes(x = x, y = y, fill = fll)) +
> geom_raster() +
> scale_fill_gradient(low = "red", high = "blue")
> ## Theoretically want something like (with some sort of way to specify ranges)
> # + scale_fill_gradient(low = "blue", high = "green")
>
> Any thoughts on how I could do this, or something similar?

There is scale_fill_gradientn which allows you to specify multiple
points on the color gradient, not just the ends.

If you are wanting different gradients for different parts of the plot,
you can get an effect similar to that with my answer to this question:
http://stackoverflow.com/a/13016912/892313

> My ideas so far are:
> Figure out each gradient range manually and then use scale_fill_manual to stack them up and apply the values there.
> Create multiple plots, where I change the limits and low/high arguments to scale_fill_gradient to basically show each range on its own (with the others greyed out).
> Use the trans argument to transform the scale. I tried that with log but it still didn't look great.
>
> Any suggestions are greatly appreciated.
>
> Best wishes,
> Doug
>


--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University

Doug Mitarotonda

unread,
Aug 10, 2013, 1:26:51 PM8/10/13
to Brian Diggs, Doug Mitarotonda, ggplot2
Many thanks Brian --- somehow I completely missed gradientn!
Reply all
Reply to author
Forward
0 new messages