Open In App

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

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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.

In this article, we are going to see how to set or view the Graphic Palette in R Programming Language.

R – 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. 
 

Set or view the current color palette

R




# R program to illustrate
# palette function
 
# Calling the palette() function
# to obtain the current palette
palette()


Output: 

[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
[8] "gray"

Set extra Graphics Palette

R




# R program to illustrate
# palette function
 
# Calling the palette() function
# to set 2 extra Graphics Palette
palette(c(palette(), "purple", "brown"))
 
# Viewing the final added Graphics Palette
palette()


Output: 

[1] "black"   "red"     "green3"  "blue"    "cyan"    "magenta" "yellow" 
[8] "gray" "purple" "brown"

Using Hexadecimal Color Codes

R




# Set a custom palette using hexadecimal color codes
hex_palette <- c("#FF5733", "#33FF57", "#3366FF", "#FF33DD")
 
# Set the palette
palette(hex_palette)
 
# Create a barplot with the custom 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

  1. Define Hexadecimal Color Palette:
    The variable hex_palette is assigned a vector of hexadecimal color codes. Each code represents a distinct color: orange (#FF5733), green (#33FF57), blue (#3366FF), and magenta (#FF33DD).
  2. Set the Palette:
    The palette() function is used to set the color palette for subsequent plots. In this case, it’s set to the custom hexadecimal color palette.
  3. Create a Barplot:
    The barplot() function generates a barplot with bars of equal height (all values in rep(1, 4) are 1). The col parameter specifies the colors for each bar, and the custom palette is applied (col = 1:4). The main parameter sets the main title of the plot to “Hex Color Palette,” and names.arg provides labels for the bars.

Defining your own palettes

Here we will make our own palettes, for this, we will make the vectors along with hex triplets and R color names and then plot the chart with these color names.

R




colors <- c("#A7A7A7",
 "forestgreen",
 "gold")
 
pie(discoveries, col = colors)


Output:

  1. Color Specification:
    The colors vector contains three color values specified as hexadecimal color codes. In this example, the colors are light gray (#A7A7A7), forest green (forestgreen), and gold (gold).
  2. Pie Chart Creation:
    The pie() function is then used to create a pie chart using the data in the discoveries vector. The col parameter is set to the colors vector, indicating that each segment of the pie chart should be filled with the corresponding color.


Last Updated : 24 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads