Open In App

How To Get the Default Color Codes of ggplot2 in R?

Last Updated : 29 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to get the default color codes of ggplot2 in R Programming Language.

Get Hex Color Codes

Format of Hex Color Codes: Each Hex color code contains the symbol “#” followed by 6 alphabets or numbers. Numbers are in the hexadecimal numeric system. There are 1,67,77,216 different color possibilities. 00 value range of the code represents the lowest intensity of color on the other hand FF value range of code represents the highest intensity.

Meaning of a Hex code:

  • The 1st and 2nd variable in Hex color code represents the intensity of red color.
  • The 3rd and 4th variable represents the intensity of green.
  • The 5th and 6th variable represents the intensity of blue.

By combining the intensities of red, green, and blue almost any color can be made:

Syntax: hue_pal()(n)

Parameters:

  • hue_pal(): it is a function present in “scales” package
  • n: this indicates number of levels

Note: We can adjust the value of “n” to get the color code of different levels

R




library(scales)
hue_pal()(6)


 

 

Output:

 

[1] "#F8766D" "#B79F00" "#00BA38" "#00BFC4" "#619CFF" "#F564E3"

Visualising Hex Color Codes

 

Visualizing Hex code using show_col, we will be using the show_col() function to visualize the color codes in grid format.

 

Syntax: show_col(hue_pal()(n))

Parameters:

  • hue_pal(): it is a function present in “scales” package
  • n: this indicates number of levels

 

The only extra step we have to do is to pass whatever we did in the previous step into show_col() function as an argument.

 

R




library(scales)
show_col(hue_pal()(16))


 

 

Output:

 

 

We can also visualize in the reverse direction by using the direction parameter

 

Syntax: show_col(hue_pal(direction=-1)(n))

Parameters:

  • hue_pal(): it is a function present in “scales” package
  • n: this indicates number of levels
  • direction: this param specifies the direction in which order we want our colors

 

Code:

 

R




library(scales)
show_col(hue_pal(direction=-1)(16))


 

 

Output:

 

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads