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
Output:
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow"
[8] "gray"
Set extra Graphics Palette
R
palette ( c ( palette (), "purple", "brown"))
palette ()
|
Output:
[1] "black" "red" "green3" "blue" "cyan" "magenta" "yellow"
[8] "gray" "purple" "brown"
Using Hexadecimal Color Codes
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:
Graphics Palette in R Programming – palette() Function
- 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
).
- 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.
- 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:
- 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
).
- 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
Share your thoughts in the comments
Please Login to comment...