Open In App

Set or View the Graphics Palette in R Programming - palette() Function

Last Updated : 30 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In R Programming language a graphics palette refers to a set of colors that can be used to enhance the visual appearance of plots and charts. The default graphics palette in R typically consists of a range of colors, but users can customize and define their own palettes based on their preferences or specific needs.

palette() Function

The palette() function in base R allows users to set or view the current color palette. It can be used to select or define a custom set of colors.

Syntax: palette(value)

Parameters: 

  • value: an optional character vector. 

1. Set or view the current color palette

When called without arguments, palette() returns the current color palette as a vector of colors. This will return the current set of colors being used in R's graphical functions.

R
palette()

Output: 

palette
Graphics Palette in R Programmin

2. Set extra Graphics Palette

R
palette(c(palette(), "purple", "brown"))

palette()

Output: 

Palette2
Updated Colour Palette

3. Using Hexadecimal Color Codes

Hexadecimal codes are a way to represent colors in a base-16 format, where each pair of characters (from 00 to FF) represents the intensity of red, green, and blue in the RGB color model.

We are creating a custom color palette using specific hexadecimal color codes: "#FF5733" (a shade of red), "#33FF57" (a shade of green), "#3366FF" (a shade of blue), and "#FF33DD" (a shade of pink). Then, we set this custom palette using palette(hex_palette). Finally, we are creating a bar plot with four bars, each colored according to the updated palette, and labeling the bars with numbers 1 to 4

R
hex_palette <- c("#FF5733", "#33FF57", "#3366FF", "#FF33DD")

palette(hex_palette)

barplot(rep(1, 4), col = 1:4, main = "Hex Color Palette", names.arg = 1:4)

Output:

gh
Graphics Palette in R Programming - palette() Function

4. Defining your own palettes

We are defining a custom color palette using a mix of a hexadecimal color code ("#A7A7A7" for gray), a named color ("forestgreen"), and another named color ("gold"). Then, we are creating a pie chart with the pie() function, where the slices are colored according to the custom colors palette.

R
colors <- c("#A7A7A7",
 "forestgreen",
 "gold")

pie(discoveries, col = colors)

Output:

In this article, we explored how to use the paleete() function and apply custom colors to visualizations in R, enhancing the appearance of charts by using both hexadecimal and named color codes.


Explore