Create Distinct Color Palette in R
In this article, we will discuss how to create a distinct color palette in R Programming Language. There are a number of ways to create color palettes in R programming, all such methods are discussed below.
Method 1: Using grDevices package
We’ll be using a package named grDevices, stands for graphics Devices, for making distinct color palettes in the first method. Here we’ll be using the colors function from the grDevices to make our palette.
- Using colors() function:
In this example we will make a pie chart using this very package. So, first we’ll be mentioning the number of colors, which we want in the palette. Then we’ll use the colors() function of this package, which contains a wide range of HCL(hue-chroma-luminance) based colors.
Example:
R
# number of colors in the palette no_of_colors <- 10 # applying the colors function from # grDevices package color_palette <- grDevices:: colors () # optional, for printing the hex # color codes color_palette # pie function for drawing a pie chart pie ( rep (1, no_of_colors), radius = 0.8, col = color_palette, main = "Color Palette using colors function" ) |
Output:
Now, let’s say we wish to create a coloraturas’ palette of only Green color and its shades. We shall be following the above steps as usual for making this palette too, but the only change is that, we’ll be using the grep command here. grep is used to search or matches to argument pattern within each element of a character vector: they differ in the format of and amount of detail in the results.
Syntax:
grep(pattern, x, ignore.case = FALSE, perl = FALSE, value = FALSE, fixed = FALSE, useBytes = FALSE, invert = FALSE)
Example:
R
# number of colors in the palette no_of_colors <- 20 # applying the colors function color_palette <- grDevices:: colors () # prints all hex color codes color_palette # grep command for matching the # pattern(character string --> green), palette <- color_palette[ grep ( "green" , grDevices:: colors ())] # sample colours green_palette <- sample (palette, no_of_colors) # the list of the colours, which will # show up in the palette green_palette # shows the created palette pie ( rep (1, no_of_colors), col = green_palette, main = "Green and its shades" ) |
Output:
- Using rainbow()
Here we’ll be making the color palette using the rainbow() function from grDevices package. rainbow() function is an in-built color palette which can be used in order to instantly generate some color vectors of desired length as per the parameter, and it returns the hexadecimal code of the colo available.
Syntax:
rainbow(variable name which stores the number of colors)
The entire process is just the same as before, only that we are using rainbow function over here.
Example:
R
# no. of colours in the palette no_of_colors <- 15 # applying the rainbow function colorful_palette <- rainbow (no_of_colors) # prints the hex color codes colorful_palette # creates a pie chart of rainbow colours pie ( rep (1, no_of_colors), col = colorful_palette, main = "Rainbow Palette" ) |
Output
Method 2: Using viridis package.
Viridis package is a default color maps. It’s a series of color maps that are specially designed to improve graph readability for someone with common forms of color vision deficiency or color blindness.
This particular package consists of 8 color scales: “viridis”, being the topic name, and other such options with similar properties, which are listed below.
-> “magma”, –> option A
->“inferno”, –> option B
->“plasma”, –> option C
-> “viridis” –> default option D
->“cividis”, –> option E
-> “rocket”, –> option F
-> “mako”, –> option G
-> “turbo” –> option H
This viridis scale is for better understanding the color types, mentioned with their options.
Syntax:
viridis_pal(option = “option”)(variable storing the number of colors)
Example:
R
# installation of the viridis package install.packages ( "viridis" ) # loading of the library library ( "viridis" ) # it may happen that the above 2 # lines of code pops out an error # saying viridisLite has to loaded # so to avoid that do load that # package too library ( "viridisLite" ) # number of colors in the palette no_of_colors <- 10 # options represent the color types, # there are altogether 8 options. palette <- viridis_pal (option = "D" )(no_of_colors) # hex color codes palette # represents colors in a pie chart manner pie ( rep (1, no_of_colors), col = palette, main = "viridis palette" ) |
Output:
Also, if we wish to change the color pattern type of this palette, then we need to change the option(anything within A to H). Every part of the above code will remain the same except the option.
Example:
R
# loading of the library library ( "viridis" ) # it may happen that the above # 2 lines of code pops out an # error saying viridisLite has # to loaded so to avoid that do # load that package too library ( "viridisLite" ) # number of colors in the palette no_of_colors <- 10 # option A --> magma palette <- viridis_pal (option = "A" )(no_of_colors) # hex color codes palette # represents colors in a pie chart manner pie ( rep (1, no_of_colors), col = palette, main = "magma palette" ) |
Output:
Method 3: Using randomcoloR package
randomcoloR is an R language package for generating attractive and distinctive colors. The function distictColorPalette() generates optimally recognizable colors.
Syntax:
distictColorPalette(variable having the number of colors stored)
Example:
R
# installation install.packages ( "randomcoloR" ) library ( "randomcoloR" ) # no. of colours in the palette no_of_colors <- 15 # sample colors palette <- distinctColorPalette (no_of_colors) # hex color codes palette # colors in the pie chart pie ( rep (1, no_of_colors), col = palette, main = "palette using randomcoloR package" ) |
Output:
Method 4: Using RColorBrewer package
This package creates nice looking color palettes, especially for thematic maps.
- brewer.pal() makes the color palettes from ColorBrewer available as R palettes.
Syntax:
brewer.pal(n, name)
Parameter:
- n: Number of different colors in the palette, minimum 3, maximum depending on palette.
- name: A palette name from the lists below.
- display.brewer.pal() displays the selected palette in a graphics window.
Syntax:
display.brewer.pal(n, name)
Parameter:
- n: Number of different colors in the palette, minimum 3, maximum depending on palette.
- name: A palette name from the lists below.
- display.brewer.all() displays a few palettes simultaneously in a graphics window.
Syntax:
display.brewer.all(n=NULL, type=”all”, select=NULL, exact.n=TRUE, colorblindFriendly=FALSE)
Parameter:
- n: Number of different colors in the palette, minimum 3, maximum depending on palette.
- name: A palette name from the lists below.
- type: One of the string “div”, “qual”, “seq”, or “all”.
- select: A list of names of existing palettes
- exact.n: If TRUE, only display palettes with a color number given by n.
- colorblindFriendly: if TRUE, display only colorblind friendly palettes.
- brewer.pal.info returns information about the available palettes as a dataframe. brewer.pal.info is not a function, it is a variable. This might change in the future.
- rownames Retrieve or set the row or column names of a matrix-like object.
Syntax:
rownames(x) <- value
There are 3 types of palettes namely: sequential, diverging, and qualitative.
- Sequential palettes are suited to ordered data that progress from low to high. Lightness steps dominate the look of these schemes, with light colors for low data values to dark colors for high data values.
R
display.brewer.all (type= "seq" ) |
Output:
- Diverging palettes put equal emphasis on mid-range critical values and extremes at both ends of the data range. The critical class or break in the middle of the legend is emphasized with light colors and low and high extremes are emphasized with dark colors that have contrasting hues.
R
display.brewer.all (type= "div" ) |
Output:
- Qualitative palettes do not imply magnitude differences between legend classes, and hues are used to create the primary visual differences between classes. Qualitative schemes are best suited to representing nominal or categorical data.
R
display.brewer.all (type= "qual" ) |
Output:
Example:
R
# installation of the package RColorBrewer install.packages ( "RColorBrewer" ) # loading of the package library ( "RColorBrewer" ) # extraction of color info # qual --> Qualitative color palette palette5 <- brewer.pal.info[brewer.pal.info$category == "qual" ,] # create vector with all colors palette <- unlist ( mapply (brewer.pal, palette5$maxcolors, rownames (palette5))) palette # sample colors palette_palette <- sample (palette, no_of_colors) # colors with their hex codes palette_palette # prints the pie chart pie ( rep (1, no_of_colors), col = palette_palette, main = "RColorBrewer Palette" ) |
Output:
.
Contact Us