Set or View the Graphics Palette in R Programming – palette() Function
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:
- Define Hexadecimal Color Palette:
The variablehex_palette
is assigned a vector of hexadecimal color codes. Each code represents a distinct color: orange (#FF5733
), green (#33FF57
), blue (#3366FF
), and magenta (#FF33DD
). - Set the Palette:
Thepalette()
function is used to set the color palette for subsequent plots. In this case, it’s set to the custom hexadecimal color palette. - Create a Barplot:
Thebarplot()
function generates a barplot with bars of equal height (all values inrep(1, 4)
are 1). Thecol
parameter specifies the colors for each bar, and the custom palette is applied (col = 1:4
). Themain
parameter sets the main title of the plot to “Hex Color Palette,” andnames.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:
- Color Specification:
Thecolors
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
). - Pie Chart Creation:
Thepie()
function is then used to create a pie chart using the data in thediscoveries
vector. Thecol
parameter is set to thecolors
vector, indicating that each segment of the pie chart should be filled with the corresponding color.
Contact Us