How do I generate a mapping from numbers to colors in R?

后端 未结 4 693
刺人心
刺人心 2021-02-15 13:45

In R, I can easily generate a color ramp, for example colorRampPalette. The following produces a sequence of five colors, from blue to red:

> mypal <- colo         


        
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-15 14:22

    The simplest thing I can think of is discretizing your variable x with cut and then using the resultant factor to index a color Palettes like heat.colors or cm.colors

    f <- function(x,n=10){
        heat.colors(n)[cut(x,n)]
    }
    

    see ?heat.colors for more details of Palettes.

    This function will give you are color vector corresponding to x of varying granularity proportional to n.

提交回复
热议问题