Skip to content
Related Articles

Related Articles

Generate Color Vectors of desired Length in R Programming – rainbow() Function
  • Last Updated : 30 Jun, 2020

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:




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

Output:



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

Example 2:




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

Output:
rainbow() function

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :