2

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 distribution than the high when colour coding in original units.

I found a possible lead here https://stat.ethz.ch/pipermail/r-help/2006-July/110187.html but this solution seemed to be overly complicated.

I'm not sure where to start with this but suspect somebody has already solved this log-scale palette problem?

Markm0705
  • 1,340
  • 1
  • 13
  • 31

1 Answers1

1

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))))

enter image description here

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)))

enter image description here

missuse
  • 19,056
  • 3
  • 25
  • 47