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()
.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With