RobertMyles
RobertMyles

Reputation: 2832

rgb() with ggplot2 in R

In ggplot2, we have the option of setting colours by name or hex code. Is there any way to use rgb values in the same way? I searched the docs but the quick answer seems to be 'no'. (The reason I would like to use rgb is that I have some colours that I am going to use for some plots, and I have them all in rgb format. I can get the hex from places like here, but it would be great if I could just enter the values straight into ggplot().

Upvotes: 14

Views: 26836

Answers (1)

G5W
G5W

Reputation: 37661

You can use the function rgb(r, g, b) to convert fractional RGB values to hex:

rgb(0.1,0.2,0.3)
[1] "#1A334D"

If your values are based on 8-bit color (or any other limit), you can use the maxColorValue option to specify the maximum number:

rgb(207, 31, 46, maxColorValue = 255)
[1] "#CF1F2E"

Upvotes: 26

Related Questions