6

I've run into a bit of an issue in my R self-teaching. I'm hoping someone here can help, as I've found no solutions on Google.

I'm trying to apply some basic color parameters to the plot command. My basic code looks either as:

plot(dose,drugA, type = "b", lty = 2, pch = 18, cex = 1.5, lwd = 1.25, col = rgb(43,228,178), col.axis = rgb(11,114,86), col.lab = rgb(95,195,168), fg = rgb(222,92,53), bg = rgb(222,216,53))

Or:

par(lty = 2, pch = 18, cex = 1.5, lwd = 1.25, col = rgb(43,228,178), col.axis = rgb(11,114,86), col.lab = rgb(95,195,168), fg = rgb(222,92,53), bg = rgb(222,216,53))

Each time I use this code, I receive the following error message:

Error in rgb(43, 228, 178) : color intensity 43, not in [0,1]

Any help would be greatly appreciated.

Yehuda
  • 1,787
  • 2
  • 15
  • 49

2 Answers2

5

By default, rgb takes R,G, B values between zero and one. You can get the effect that you are looking for with rgb(43/255, 228/255, 178/255)

G5W
  • 36,531
  • 10
  • 47
  • 80
  • 6
    You're right, but `rgb` has a `maxColorValue=` argument too - so you can do `rgb(43, 228, 178, maxColorValue = 255)` – thelatemail Dec 12 '16 at 22:47
4

I thing the best answer comes from @thelatemail. It's preferable to specify RGB colors in 0-255 range, then use:

rgb(43, 228, 178, maxColorValue = 255)

If you use something like rgb(43/255, 228/255, 178/255) you may not get the exactly color you really want.

Alex8752
  • 597
  • 5
  • 12
  • "f you use something like rgb(43/255, 228/255, 178/255) you may not get the exactly color you really want." I'm assuming because of rounding error? – Yehuda May 15 '19 at 17:10