0:00

So the grDevices package has a few different

Â functions to help, you deal with colors and the

Â ones that I'm going to talk about in this

Â lecture are the colorRamp function and the colorRampPalette function.

Â basically, these two functions do take

Â palettes of colors and interpolate between them.

Â So the, the, the model that you could think of is basically, you know,

Â if a painter has, his palette, and there are of four, two or three

Â blotches of specific colors on that palette.

Â And then what you might do is take your brush,

Â and kind of mix between the various colors of the various

Â per, blotches on that palette, to create new colors that are

Â blends of the original colors that you had on the palette.

Â 0:41

So I've never done any actually painting, so

Â I just the only thing I know about painting

Â is you know from TV and movies but, that's kind of what I imagine a painter would do

Â is take these primary colors and then blend them together with the brush.

Â And so, that's what the colorRamp and the colorRampPalette functions

Â do, they take a set of colors that make up

Â a palette and they interpolate between them numerically of course

Â to make new colors that are blends of the original colors.

Â 1:09

Another function that may be of interest to you is the

Â colors function which just, you just executed with no arguments and it

Â gives you a character vector of all the names of

Â the colors that you can use in a plotting function.

Â So these are colors that you can reference by name rather than having to

Â reference their kind of red, green, and blue values that we'll talk about later.

Â 1:39

And this, this function that's returned by colorRamp will take values

Â between 1 and 1 indicating the extremes of the color palette.

Â And so, a similar function that already exists in R is the gray function.

Â And the gray function interpolates between black and white.

Â And so it gives you kind of all

Â the shades of gray there between black and white.

Â The colorRamp function is a generalization because it will take any

Â set of colors in your palette and it will give you

Â a function back that takes numbers between 0 and 1 and

Â it kind of interpolate between the extremes of the color palette.

Â The colorRampPalette is a very similar type of function it takes a palette

Â of colors and what it does it returns another function that rather than taking

Â a value between 0 and 1.

Â It would takes an integer argument and it

Â will return a vector of colors interpolating that palette.

Â So, this is similar to the heat.colors function or the topo.colors function.

Â So, So let's just.

Â A very quick example here.

Â In the top here, I've created I'm going to use

Â a palette that consists of two colors, red and blue.

Â So you can imagine that on your

Â little painter's palette here, you've got a blotch

Â of red and a blotch of blue, and you're going to

Â mix them together in varying degrees to create new colors.

Â So I, when I pass this through colorRamp it returns function back and

Â that function I've called pal, and, and now pal can take numbers between 0 and 1.

Â So when I say pal (0).

Â What do I get back?

Â I get a little matrix here with one row and three columns.

Â And the three columns

Â indicate, are, are, represent the colors red, blue and green.

Â 3:17

And so in the first column, I've got 255 which is the

Â maximum value that I can have in this, for the, for these colors.

Â The numbers that you specify go between 0 and 255, so

Â there's 256 total numbers that you can specify for each color.

Â And so when pal is when I, when pal

Â (0), it gives the argument 0, basically what it gives

Â me back is red, because that's kind of one end

Â of the color spectrum that I've specified in my palette.

Â The other end being blue, of course.

Â And so I've got the maximum value for red, I've

Â got 0 for green, and I've got 0 for blue.

Â So that color is red.

Â When I say pal (0) down here you see, I've got 0 for

Â red, 0 for green, and then I've got the maximum 255 for blue.

Â So this just gives me the color blue.

Â So that's the other end of my color palette.

Â So what if I do something in between, like pal (0.5)?

Â So this gives me something that, this should give me a

Â color that's kind of in the middle of red and blue.

Â So it's going to give me 127.5 for red, and then 0 for green, then 127.5 for blue.

Â So it's kind of half red, half blue, whatever that color is happens to be.

Â Notice that there's no green in any of these

Â calls because when in interpolate between red and blue,

Â you don't encounter green along the way.

Â 4:36

So if I were to give a sequence of numbers between 0 and

Â 1s pal would give me a sequence of colors between red and blue.

Â So here I'm giving a sequence between 0 and 1 that's that's, that's a length 10.

Â So it starts at 0 and it ends at 1 and and there's a eight numbers in between.

Â And so when I pass the sequence to pal, you'll see at the very top here, I've

Â got, I start with red, so it's maximum red, and then, 0 for green, 0 for blue.

Â And

Â I slowly reduce the red amount and increase the blue amount, till I get

Â to the bottom here, where I've got all blue and no green and no red.

Â So those are the various colors that you get

Â in between red and blue as you interpolate the palette.

Â 5:17

So, that's the colorRamp function.

Â The colorRamp palette function is very similar but it just

Â it, the type of function that it returns is slightly different.

Â So here I'm passing it a different palette.

Â So this palette has two blotches, it has a red and a yellow

Â blotch on it and we're going to try and interpolate between those two colors.

Â And so I've got a function here that's returned by colorRampPalette

Â called pal and now pal is going to take integer arguments not

Â numbers between 0 and 1.

Â So, suppose I give it 2, so pal

Â (2) will return two colors that interpolate the palette.

Â Now, because the palette itself only has two colors

Â they will, when I just give a 2 it'll

Â just give me the two colors on the palette

Â so the first being red and the second being yellow.

Â And so, that the way that the format here is slightly

Â different instead of giving a matrix with red, green, and blue values.

Â It gives me a character vector

Â 6:09

which has numbers for, or, or values for

Â red, green, and blue that are represented in hexadecimal.

Â And so if you ignore the pound symbol there, you'll see there are two digits.

Â There are six digits in, in each character string.

Â The first two represent red, the second two

Â represent green, and the third two represent blue.

Â 6:29

So you can see for the first number here I've got FF0000.

Â So in hexadecimal

Â F is the largest number you can have.

Â So FF on the red, the red pair means a

Â maximum for red is 00 for green 00 for blue.

Â So that's red.

Â In the second string here, I've got FF for red, FF for green and then 00 for blue.

Â So I've got the maximum on red and maximum on green and I got nothing for blue.

Â So what does that gives me?

Â Well, that gives me yellow and by combining red and green I get yellow.

Â So that's the other end of the spectrum on the palette.

Â So, when now, if I say pal (10), you'll see the first element here

Â is going to be red, because it's FF on red and 0 on everything else.

Â The last element here is going to be yellow, because it's

Â FF on red, FF on green, 0 on the blue.

Â But then in between, I've got my interpolation of colors, so you'll see

Â that there's, there'll be there'll be, there'll be some red, and then there'll

Â be some mix ins, a little bit of green as you go along here.

Â For more, for all the colors there's no blue, because there's,

Â you don't encounter blue when you interpolate between red and yellow.

Â So you can see now the representations of the colors

Â in hexadecimal here changing as you go from red to yellow.

Â [BLANK_AUDIO]

Â