Question

I am attempting to create a block of color in R based on density values. So for example given 10 values with a density distribution:

values=c(0,1,2,3,4,5,6,7,8,9)

densities=c(0.7, 0.1, 0.05,0,0,0,0,0.05,0.05,0.05)

I want to create what is essentially a colored bar where the greatest density is e.g. black and the least white, with the intermittent values somewhere in between but proportionally so i.e. 0.05 if half as dark as 0.1 AND similar values are the same color.

As I think of it I can just create a bar chart, with all bars the same height, no borders etc and the densities used to create the colors. However, no matter how hard I try I can't figure out how to get the color scheme correct.

I have created a gradient but this isn't linked to density. Also I have linked color to density with densCols but I haven't managed to make the colors sequential.

Can someone point me in the right direction? I have seen similar questions but none that have gotten me to the point I need to be at. I would prefer to do this with the base graphics package if possible.

Thanks in advance.

Was it helpful?

Solution

if you can normalize value in [0,1] range you can use functions like grey (grey scale).

## not with density but with probability example                                
val<- rnorm(100) < 0.5
mean(val)

grey(0) # black                                                                 
grey(1) # white                                                                 

## color intensity proportional to probability                                  
grey(1 - prop.table(table(val)))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top