How to Generate a Palette of Distinct Colors in R (Example Code)

In this R tutorial you’ll learn how to create a range of different hex color codes.

Example: Creating Palette of Different Colors Using rainbow() Function

number_of_col <- 30                          # Defining number of colors
set.seed(964294)                             # Setting random seed
my_cols <- sample(rainbow(number_of_col))    # Applying rainbow() function
my_cols                                      # Showing hex color codes in RStudio console
#  [1] "#00FF33" "#FF0099" "#FF6600" "#6600FF" "#FFFF00" "#99FF00" "#0099FF" "#FF3300" ...
pie(rep(1, number_of_col), col = my_cols)    # Drawing pie chart with colors

r graph figure 1 generate palette distinct colors

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top