Home > database >  ggplot geom_rect color gradient (without reference to data)?
ggplot geom_rect color gradient (without reference to data)?

Time:09-24

I was wondering if it is possible to have a geom_rect with a color gradient without a data reference, i.e. outside of aes(). I would like the two rectangles in the bottom of the following plot to show a color gradient from red to white (left to right) and the top one to show a color gradient from yellow to white. Is this possible in a simple way or do I have to create data to refer to?

ggplot()  
  geom_rect(aes(xmin = c(1, 3), xmax = c(2.5, 4), ymin = c(1, 1), ymax = c(2, 2)), color = "black", fill = "red")  
  geom_rect(aes(xmin = 1, xmax = 3.5, ymin = 3, ymax = 4), color = "black", fill = "yellow")  
  theme_bw()  
  theme(panel.grid = element_blank())

enter image description here

I tried to use scale_fill_gradient with geom_tile but this doesn't really do what I want: 1. the two supposed-to-be-red rectangles share a gradient and don't start with pure red each and 2. I can't manage to use two different scale_fill_gradient's in one plot.

foo <- tibble(x = seq(from = 1, to = 2.5, by = 0.001),
              y = rep(1, 1501))
bar <- tibble(x = seq(from = 3, to = 4, by = 0.001),
              y = rep(1, 1001))
foobar <- tibble(x = seq(from = 1, to = 3.5, by = 0.001),
              y = rep(3, 2501))

ggplot()  
  geom_tile(data = foo, aes(x = x, y = y, fill = x))  
  geom_tile(data = bar, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'red', high = 'white')  
  geom_tile(data = foobar, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'yellow', high = 'white')  
  theme_bw()  
  theme(panel.grid = element_blank())

enter image description here

CodePudding user response:

You could use the function new_scale_fill from ggnewscale between your two different scale_fill_gradient functions in your plot process. This will reset your aesthetics to make it possible to use another gradient like this:

library(tibble)
foo <- tibble(x = seq(from = 1, to = 2.5, by = 0.001),
              y = rep(1, 1501))
bar <- tibble(x = seq(from = 3, to = 4, by = 0.001),
              y = rep(1, 1001))
foobar <- tibble(x = seq(from = 1, to = 3.5, by = 0.001),
                 y = rep(3, 2501))

library(ggplot2)
library(ggnewscale)
ggplot()  
  geom_tile(data = foo, aes(x = x, y = y, fill = x))  
  geom_tile(data = bar, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'red', high = 'white')  
  new_scale_fill()  
  geom_tile(data = foobar, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'yellow', high = 'white')  
  theme_bw()  
  theme(panel.grid = element_blank())

Created on 2022-09-23 with reprex v2.0.2


To add for each geom_tile a gradient color, you could use for each tile new_scale_fill like this:

library(ggplot2)
library(ggnewscale)
ggplot()  
  geom_tile(data = foo, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'red', high = 'white', guide = 'none')  
  new_scale_fill()  
  geom_tile(data = bar, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'red', high = 'white')  
  new_scale_fill()  
  geom_tile(data = foobar, aes(x = x, y = y, fill = x))  
  scale_fill_gradient(low = 'yellow', high = 'white')  
  theme_bw()  
  theme(panel.grid = element_blank())

Created on 2022-09-23 with reprex v2.0.2

  • Related