我正在尝试绘制线条,并根据该连接的概率为线条上色。给定一个概率向量,我使用:
colfunc <- colorRamp(c("white", "red"))
colors <- colfunc(probs)然后,颜色是RGB值的nx3矩阵。但是,colfunc经常返回0值,所以当我尝试使用这些颜色绘图时,R会报错
Error in col2rgb(colors) : numerical color values must be positive我定义颜色函数的方式有错误吗?
发布于 2014-12-06 07:38:04
我认为您的函数工作得很好,但是它不会返回可以用于plot的颜色,因为plot需要的是一个颜色,而不是矩阵中的RGB值。
可能有一种更好的方法,但你可以简单地隐藏矩阵:
probs <- runif(10)
colors <- colfunc(probs)
my_col = apply(colors, MARGIN = 1, function(x) rgb(x[1]/255, x[2]/255, x[3]/255))
plot(1:10, 1:10, col = my_col) # should work fine或者,您可以只包装您的函数
better_colfunc <- function(x, ramp = colorRamp(c("white", "red"))) {
colors <- ramp(x)
colors = apply(colors, MARGIN = 1, function(x) rgb(x[1]/255, x[2]/255, x[3]/255))
return(colors)
}
plot(1:10, 1:10, col = better_colfunc(probs, ramp = colfunc))至于"colfunc经常返回0值“和其他问题,你需要同时分享一些数据(你的probs是什么样子的?)可能还有实际的绘图代码。See here获取关于制作可重现问题的提示。
发布于 2014-12-06 05:56:50
我有点困惑你试图do...the col2rgb函数返回的RGB值,所以如果你已经有了这些值,那么你想要什么?
或者如果你想要rgb,为什么不使用:
col2rgb(c("white", "red"))https://stackoverflow.com/questions/27325162
复制相似问题