0

I have a data set on sea ambient temperatures that looks like this:

Date timestamp SW
11/1/2017 02:00:00 AM 17.8804
11/1/2017 04:00:00 AM 17.8106
11/1/2017 06:00:00 AM 17.752
11/1/2017 08:00:00 AM 17.7315
11/1/2017 10:00:00 AM 17.9066
11/1/2017 12:00:00 PM 18.1229
11/1/2017 02:00:00 PM 18.5551
11/1/2017 04:00:00 PM 19.2719
11/1/2017 06:00:00 PM 18.6102
11/1/2017 08:00:00 PM 18.0809
11/1/2017 10:00:00 PM 17.9975
11/2/2017 02:00:00 AM 17.6566
11/2/2017 04:00:00 AM 17.4234
11/2/2017 06:00:00 AM 17.6084
11/2/2017 08:00:00 AM 17.5954 ...

I would like to the temperatures as a color gradient in the background of another ggplot2 plot showing growth rates over the same time period to look for correlations between temperature and growth rate the two variables. A similar issue was raised here: Plot background colour in gradient, but I haven't figured out how to use continuous variables to code for the gradient.

Thank you very much

cfnnvt
  • 1
  • Hi and welcome to stack overflow. Can you provide your example data using `dput()` as then it will be easier for people to use that data to help answer your question. Can you also show what you have tried so far and how the problem is different to the question you said is similar? – Sarah Mar 24 '20 at 02:51

1 Answers1

0

What you're looking for was actually further down in the answers of the post you've linked. It's the grad_by_val() function from jkd's answer.

library(ggplot2)

# Load your data
dat <- "Date, SW
11/1/2017 02:00:00 AM, 17.8804
11/1/2017 04:00:00 AM, 17.8106
11/1/2017 06:00:00 AM, 17.752
11/1/2017 08:00:00 AM, 17.7315
11/1/2017 10:00:00 AM, 17.9066
11/1/2017 12:00:00 PM, 18.1229
11/1/2017 02:00:00 PM, 18.5551
11/1/2017 04:00:00 PM, 19.2719
11/1/2017 06:00:00 PM, 18.6102
11/1/2017 08:00:00 PM, 18.0809
11/1/2017 10:00:00 PM, 17.9975
11/2/2017 02:00:00 AM, 17.6566
11/2/2017 04:00:00 AM, 17.4234
11/2/2017 06:00:00 AM, 17.6084
11/2/2017 08:00:00 AM, 17.5954"
dat <- read.table(text = dat, header = T, sep = ",", as.is = T)
# convert to date format
dat$Date <- as.POSIXct(dat$Date, format = "%m/%d/%Y %I:%M:%S %p")

# function from jkd's post
grad_by_val <- function(x, y, cols = blues9) {
  require(grid)
  y <- y[order(x)]
  ys <- (y - min(y)) / diff(range(y))
  cols <- colorRamp(cols)(ys) / 256
  colnames(cols) <- c("red", "green", "blue")
  cols <- apply(cols, 1, function(z) do.call(rgb, as.list(z)))
  mat <- matrix(cols, ncol = length(x))
  rasterGrob(
    image = mat,
    width = unit(1, "npc"),
    height = unit(1, "npc"),
    interpolate = TRUE
  )
}

On your data, this gives :

ggplot(dat, aes(x = Date, y = SW)) +      
  annotation_custom(
    grob = grad_by_val(dat$Date, dat$SW, cols = heat.colors(10)),
    xmin = -Inf,
    xmax = Inf,
    ymin = -Inf,
    ymax = Inf) +
  geom_point() # make sure you plot stuff AFTER the gradient (or it'll be below it)

Output :

enter image description here

RoB
  • 1,833
  • 11
  • 23
  • Thank you RoB ! Would you know how to make the colder temperatures appear blue and the hotter ones in the red? – cfnnvt Mar 25 '20 at 02:01
  • Yes, just define a color palette instead of `heat.colors()`, such as `c("blue", "red")` for example. The function will make the intermediate colors on its own – RoB Mar 25 '20 at 08:15