Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assign colors to a range of values

Tags:

r

A quick question,

I have a vector with numbers, e.g:

values <- c(0.104654225, 0.001781299, 0.343747296, 0.139326617, 0.375521201, 0.101218053)

and I want to assign a gradient scale to this vector, I know that I can do it with

gray(values/(max(values))

but I would like to use a blue colour scale, for example using colorRampPalette or something similar,

Thanks

I've noticed that there are several questions with similar topics but they map intervals and I would like to map colours to numeric values

like image 852
user2380782 Avatar asked Mar 07 '14 16:03

user2380782


People also ask

How do I create a color scale for varying ranges?

Create a color scale that separates data into five varying ranges. This example uses Superstore sample data to compare varying ranges of Sales. 1. Select Analysis > Create Calculated Field 2. Name the field. In this example, it is named Five Colors.

Is it possible to assign color to elevation change values?

I can do this using a colorramp that grades from green to red. However, it's not possible to assign the colors of a colorramp to a specific range of elevation change values, which is what I need to do.

How to color numbers based on value or condition in Excel?

In Microsoft Excel, we can use Conditional Formatting to color numbers based on the value or condition. Conditional formatting is an important feature & is commonly used by the Excel users. Conditional formatting quickly highlights the important information.

How do I change the color of a column in Excel?

You can also select the Color Scale option in the Formatting tab from the Quick Access Toolbar Icon to have a different range of colors for the column. You will have a wide range of color cells based on the percentiles- red for the lowest, to white, to green for the highest.


1 Answers

Edit: I now think using colorRampPalette() makes this a bit easier than does colorRamp().

## Use n equally spaced breaks to assign each value to n-1 equal sized bins 
ii <- cut(values, breaks = seq(min(values), max(values), len = 100), 
          include.lowest = TRUE)
## Use bin indices, ii, to select color from vector of n-1 equally spaced colors
colors <- colorRampPalette(c("lightblue", "blue"))(99)[ii]

## This call then also produces the plot below
image(seq_along(values), 1, as.matrix(seq_along(values)), col = colors,
      axes = F)

Among base R functions, colorRamp() is what you are looking for. As dg99 notes, it returns a function that maps values in the range [0,1] to a range of colors.

Unfortunately, it returns a matrix of RGB values, which is not directly usable by most of R's plotting functions. Most of those functions take a col= argument which "wants" sRGB values expressed as hex values in strings that look like "#E4E4FF" or "#9F9FFF". To get those you'll need to apply the rgb() function to the matrix returned by colorRamp(). Here's an example:

f <- colorRamp(c("white", "blue"))
(colors <- rgb(f(values)/255))
# [1] "#E4E4FF" "#FFFFFF" "#A7A7FF" "#DBDBFF" "#9F9FFF" "#E5E5FF"

In practice, you'll probably want to play around with both the color scale and the scaling of your values.

## Scale your values to range between 0 and 1
rr <- range(values)
svals <- (values-rr[1])/diff(rr)
# [1] 0.2752527 0.0000000 0.9149839 0.3680242 1.0000000 0.2660587

## Play around with ends of the color range
f <- colorRamp(c("lightblue", "blue"))
colors <- rgb(f(svals)/255)

## Check that it works
image(seq_along(svals), 1, as.matrix(seq_along(svals)), col=colors,
      axes=FALSE, xlab="", ylab="")

enter image description here

like image 174
Josh O'Brien Avatar answered Sep 29 '22 11:09

Josh O'Brien