Given a numerical value Im trying to find the corresponding color in a ggplot legend; essentially the legend in reverse. I will try a simplified motivating example:
df <- data.frame(x = rnorm(11, mean = .2, sd = .1), y = rnorm(11, mean = -.2, sd = .2))
now plot the data
d <- ggplot(df, aes(x, y)) +
geom_point() +
geom_raster(aes(fill = seq(from = -1, to = 1, by = .2)))
d
I now want to know what hex color code corresponds to the value at .2 from the legend (or do this for any other value in the range -1,1). I realize that for this example this seems a bit silly; in reality Im making a shared legend for a scientific image and I need to extract the hex colors backwards from the legend.
Ive tried extracting the legend with g_legened(); however, the data structure is complex and I am stuck.
library(lemon)
legend <- g_legend(d)
the envisioned function would be hexColor <- reverseLegend(legend,.2)
and assistance would be appreciated.