从ggplot中检索颜色 - 值映射 [英] Retrieve Colour - Value Mapping From ggplot
问题描述
我假设ggplot尺度创建了一些函数,它读取适当的 aes
值并返回颜色
, size
等。这可以用作独立功能吗?例如,对于这个函数,我将传递必要的参数( range
, limits)。
,
> 高
,低
等)和我想要的值映射为,并且函数的输出将是颜色
/ 大小
/ etc。
#使用示例
HypotheticalScaleFunction(
range = c(0,10),
high ='#000000 ',
low ='#222222',
ValueToLookup = 5
)
#应该返回 -
#111111
您可以通过在源文件中输入缩放函数来查找。例如,如果您阅读 ggplot2 :: scale_color_continuous
的源代码,您会发现它使用 seq_gradient_pal
scale
包。
因此,对于连续的颜色,我们可以定义以下函数(使用默认值 ggplot
使用):
ColorScaleFunction< - function(Range, (低值,高值)((ValueToLookup - 范围[1])/差值(范围))
$ b
这会导致默认情况下获得的典型深蓝色,例如热量图。
它会在您的示例中产生#161616
。
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"
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.
这篇关于从ggplot中检索颜色 - 值映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!