Generate Color Vectors of desired Length in R Programming – rainbow() Function

rainbow() function in R Language is a built in color palettes which can be used to quickly generate color vectors of desired length taken as the parameter and returns the hexadecimal code of the colours available. This hexadecimal code is of eight digits. This is because the last two digits specify the level of transparency (where FF is opaque and 00 is transparent).

Syntax: rainbow(n)

Parameters:
n: desired length of the returned color vector

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# rainbow function
  
# Calling rainbow() function
rainbow(2)
rainbow(4)
rainbow(6)

chevron_right


Output:

[1] "#FF0000FF" "#00FFFFFF"
[1] "#FF0000FF" "#80FF00FF" "#00FFFFFF" "#8000FFFF"
[1] "#FF0000FF" "#FFFF00FF" "#00FF00FF" "#00FFFFFF" "#0000FFFF" "#FF00FFFF"

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# R program to illustrate
# rainbow function
  
# Using rainbow() function to
# plot 6 different color
barplot(1:6, col = rainbow(6))

chevron_right


Output:
rainbow() function

My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.