1

I assume the ggplot scales creates some sort of function which reads the appropriate aes value and returns the colour, size, etc. Can this be used as a standalone function?

For example, to this function I will pass the necessary arguments (range, limits, high, low, etc.) and a value which I want to get the mapping for, and the output of the function will be the colour / size / etc.

# example of usage
HypotheticalScaleFunction(
   range = c(0,10),
   high = '#000000',
   low = '#222222',
   ValueToLookup = 5
)
# this should return -
"#111111"
TheComeOnMan
  • 12,535
  • 8
  • 39
  • 54
  • 1
    Possible duplicate of http://stackoverflow.com/questions/11774262/how-to-extract-the-fill-colours-from-a-ggplot-object/11774500#11774500 – Henrik Sep 18 '15 at 11:18
  • Not really. If I have applied a continuous fill and I want to query for a value that wasn't there in the original dataset, then this won't work. – TheComeOnMan Sep 18 '15 at 11:28
  • 1
    OK, sorry, I misunderstood your question. You might wish to elaborate and provide a minimal example. – Henrik Sep 18 '15 at 11:29
  • 1
    No problems, my textual description probably wasn't clear. I've added an example now. – TheComeOnMan Sep 18 '15 at 11:38
  • What's the reason you want to do this manually? – Heroka Sep 18 '15 at 11:58
  • I like ggplot's colours but I need to send the data and plot it somewhere else where I don't have ggplot available to me. If I can send the colour values as data then I can recreate a very similar look at the other end. – TheComeOnMan Sep 18 '15 at 12:03

1 Answers1

2

You can find this by reading through the source, by typing in scale functions. For example, if you read through the source for ggplot2::scale_color_continuous, you will find that it uses seq_gradient_pal from the scales package.

So, for color on a continious scale, we can define the following function (with the defaults that ggplot uses):

ColorScaleFunction <- function(Range, high = "#56B1F7", low = "#132B43", ValueToLookup) {
  seq_gradient_pal(low, high)((ValueToLookup - Range[1]) / diff(Range))
}

This results in the typical dark blue colors that you get by default, in heatmaps for example.

It produces #161616 on your example though.

Axeman
  • 32,068
  • 8
  • 81
  • 94