R colour scale for logarithmic data?

前端 未结 1 1054
花落未央
花落未央 2021-01-22 23:22

I\'m looking for a log (base 10) colour scale to colour a SOM U-matrix plot in R. Specifically, looking for a colorRampPallette that will have more bins at the low end of the d

相关标签:
1条回答
  • 2021-01-23 00:01

    in ggplot one can use scale_color_gradientn. Here is an example with cars data.

    ggplot(cars)+
      geom_point(aes(x = speed, y =dist, color = dist))+
      scale_color_gradientn(colors = colorRampPalette(colors = c("blue", "white"))(nrow(cars)), 
                            values = scales::rescale(log(sort(cars$dist))))
    

    To summarize, one can define a linear gradient of any number of colors with colorRampPalette function, and in scale_color_gradientn you can map any of the colors to a certain value - the spread of the colors can be linear, log or arbitrary. Since values argument accepts 0 - 1 range, scales::rescale was used on the log transformed values.

    to compare, here is without transformation

    ggplot(cars)+
      geom_point(aes(x = speed, y =dist, color = dist))+
      scale_color_gradientn(colors = colorRampPalette(colors = c("blue", "white"))(nrow(cars)), values  = scales::rescale(sort(cars$dist)))
    

    0 讨论(0)
提交回复
热议问题