Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rgb() with ggplot2 in R

Tags:

r

ggplot2

rgb

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

like image 304
RobertMyles Avatar asked Jan 23 '17 16:01

RobertMyles


1 Answers

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"
like image 138
G5W Avatar answered Sep 30 '22 16:09

G5W