The Wayback Machine - https://web.archive.org/web/20240915134431/https://www.geeksforgeeks.org/set-or-view-the-graphics-palette-in-r-programming-palette-function/
Open In App

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

Last Updated : 24 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.


Similar Reads

Create Distinct Color Palette in R
In this article, we will discuss how to create a distinct color palette in R Programming Language. There are a number of ways to create color palettes in R programming, all such methods are discussed below. Method 1: Using grDevices package We'll be using a package named grDevices, stands for graphics Devices, for making distinct color palettes in
7 min read
Check if a Function is a Primitive Function in R Programming - is.primitive() Function
is.primitive() function in R Language is used to check if a function is a primitive function, i.e. its either a built-in function or a special function. Syntax: is.primitive(func) Parameters: func: Function to be checked Example 1: # R program to illustrate # the use of is.primitive function # Calling is.primitive() function is.primitive(1) is.prim
1 min read
Generate a set of Sample data from a Data set in R Programming - sample() Function
sample() function in R Language creates random sample based on the parameters provided in the function call. It takes either a vector or a positive integer as the object in the function parameter. Syntax: sample(x, size, replace) Parameters: x: indicates either vector or a positive integer or data frame size: indicates size of sample to be taken re
2 min read
View Function in R
The `View()` function in R Programming Language looks like a window into the data. It's a simple yet powerful tool, allowing users to peek inside datasets in a way that's easy to understand. It functions like opening up a spreadsheet where data is neatly organized in rows and columns. With `View()`, individuals don't need to be coding experts to ex
4 min read
How to View the Source Code for a Function in R?
If you're diving into R programming, there will come a time when you want to look under the hood and see how a function works. Maybe you're curious about the mechanics, or you want to understand it better to use it more effectively. Here's a guide to help you view the source code for a function in R. Viewing the Source Code of a FunctionUnderstandi
4 min read
How to make graphics with transparent background in R using ggplot2?
In this article, we will discuss how to create graphs with transparent background in R programming language. The required task will be achieved by using theme() function with appropriate parameters. theme() function used to modify theme settings. To actually visualize a transparent background the image has to be stored as a PNG image, this can be d
2 min read
What is the Relation Between R Programming and Hadoop Programming?
The relation between R programming and Hadoop revolves around integrating R with the Hadoop ecosystem to analyze large datasets that are stored in a Hadoop environment. R, primarily used for statistical analysis and data visualization, is not inherently built for handling big data. However, when combined with Hadoop, it can leverage Hadoop's distri
3 min read
Compute Density of the Distribution Function in R Programming - dunif() Function
dunif() function in R Language is used to provide the density of the distribution function. Syntax: dunif(x, min = 0, max = 1, log = FALSE) Parameters: x: represents vector min, max: represents lower and upper limits of the distribution log: represents logical value for probabilities Example 1: # Create vector of random deviation u &lt;- runif(20)
1 min read
Apply a Function over a List of elements in R Programming - lapply() Function
lapply() function in R Programming Language is used to apply a function over a list of elements. lapply() function is used with a list and performs the following operations: lapply(List, length): Returns the length of objects present in the list, List.lapply(List, sum): Returns the sum of elements held by objects in the list, List.lapply(List, mean
2 min read
Search the Interval for Minimum and Maximum of the Function in R Programming - optimize() Function
optimize() or optimise() function in R Language is used to search the interval from lower to upper for a minimum or maximum of the function f with respect to its first argument. Syntax: optimize(f, interval, maximum)Parameters: f: the function to be optimized. The function is either minimized or maximized over its first argument depending on the va
2 min read
Compute the Natural Logarithm of the Absolute Value of Gamma Function in R Programming - lgamma() Function
lgamma() function in R Language is used to compute the natural logarithm of the absolute gamma value of a numeric vector computed using the gamma function. lgamma function is basically, lgamma(x) = ln(factorial(x - 1)) Syntax: lgamma(x) Parameters: x: Non-negative Numeric Vector Example 1: # R program to calculate the # natural logarithm of gamma v
1 min read
Compute the Logarithmic Derivative of the gamma Function in R Programming - digamma() Function
digamma() function in R Language is used to calculate the logarithmic derivative of the gamma value calculated using the gamma function. digamma Function is basically, digamma(x) = d(ln(factorial(n-1)))/dx Syntax: digamma(x) Parameters: x: Numeric vector Example 1: # R program to find logarithmic derivative # of the gamma value # Calling the digamm
1 min read
Compute the Second Derivative of the Logarithmic value of the gamma Function in R Programming - trigamma() Function
trigamma() function in R Language is used to calculate the second derivative of the natural logarithm of the gamma value calculated using the gamma function. trigamma Function is basically, trigamma(x) = d2(ln(factorial(n-1)))/dx2 Syntax: trigamma(x) Parameters: x: Numeric vector Example 1: # R program to find second derivative # of the lgamma valu
1 min read
Get the List of Arguments of a Function in R Programming - args() Function
args() function in R Language is used to get the required arguments by a function. It takes function name as arguments and returns the arguments that are required by that function. Syntax: args(name) Parameters: name: Function name Returns: For a closure: Formal Argument list but with NULL body For a Primitive Function: A closure with usage and a N
1 min read
Recursively apply a Function to a List in R Programming - rapply() function
rapply() function in R Language is used to recursively apply a function to a list. Syntax: rapply(object, f, classes = "ANY", deflt = NULL, how = c("unlist", "replace", "list")) Parameters: object: represents list or an expression f: represents function to be applied recursively classes: represents class name of the vector or "ANY" to match any of
3 min read
Apply a Function over a Ragged Array in R Programming - tapply() Function
tapply() function in R Language is used to apply a function over a subset of vectors given by a combination of factors Syntax: tapply(vector, factor, fun) Parameters: vector: Created Vector factor: Created Factor fun: Function to be applied Example 1: # R Program to apply a function # over a data object # Creating Factor fac &lt;- c(1, 1, 1, 1, 2,
1 min read
Applying a Function over an Object in R Programming - sapply() Function
sapply() function in R Language takes list, vector or data frame as input and gives output in vector or matrix. It is useful for operations on list objects and returns a list object of same length of original set. Syntax: sapply(X, FUN) Parameters: X: A vector or an object FUN: Function applied to each element of x Example 1: # R program to illustr
1 min read
Compute the value of Cauchy Quantile Function in R Programming - qcauchy() Function
qcauchy() function in R Language is used to calculate the value of cauchy quantile function. It also creates a density plot of cauchy quantile function. Syntax: qcauchy(vec, scale) Parameters: vec: x-values for cauchy function scale: Scale for plotting Example 1: # R Program to compute value of # cauchy quantile function # Creating vector for x-val
1 min read
Compute value of Logistic Quantile Function in R Programming - qlogis() Function
qlogis() function in R Language is used to compute the value of logistic quantile function. It also creates a plot of the quantile function of logistic density distribution. Syntax: qlogis(vec) Parameters: vec: x-values for normal density Example 1: # R program to compute value of # logistic quantile function # Creating x-values for density x &lt;-
1 min read
Compute value of Log Normal Quantile Function in R Programming - qlnorm() Function
qlnorm() function in R Language is used to compute the value of log normal quantile function. It also creates a plot of the quantile function of log normal distribution. Syntax: qlnorm(vec) Parameters: vec: x-values for normal density Example 1: # R program to compute value of # log normal quantile function # Creating x-values for density x &lt;- s
1 min read
Compute the Value of Poisson Quantile Function in R Programming - qpois() Function
qpois() function in R Language is used to compute the value of Poisson Quantile Function. It creates a quantile density plot for poisson distribution. Syntax: qpois(vec, lambda) Parameters: vec: Sequence of integer values lambda: Average number of events per interval Example 1: # R program to compute # Poisson Quantile Density # Sequence of x-value
2 min read
Compute the Value of Negative Binomial Quantile Function in R Programming - qnbinom() Function
qnbinom() function in R Language is used to compute the value of negative binomial quantile function. It also creates a density plot of the negative binomial quantile function. Syntax: qnbinom(vec, size, prob) Parameters: vec: x-values for binomial density size: Number of trials prob: Probability Example 1: # R program to compute value of # Negativ
1 min read
Compute the Value of Empirical Cumulative Distribution Function in R Programming - ecdf() Function
ecdf() function in R Language is used to compute and plot the value of Empirical Cumulative Distribution Function of a numeric vector. Syntax: ecdf(x) Parameters: x: Numeric Vector Example 1: # R Program to compute the value of # Empirical Cumulative Distribution Function # Creating a Numeric Vector x &lt;- seq(1, 50, by = 2) # Calling ecdf() Funct
1 min read
Compute the value of F Cumulative Distribution Function in R Programming - pf() Function
pf() function in R Language is used to compute the density of F Cumulative Distribution Function over a sequence of numeric values. It also plots a density graph for F Cumulative Distribution. Syntax: pf(x, df1, df2) Parameters: x: Numeric Vector df: Degree of Freedom Example 1: # R Program to compute # Cumulative F Density # Creating a sequence of
1 min read
Compute the value of Quantile Function over F Distribution in R Programming - qf() Function
qf() function in R Language is used to compute the value of quantile function over F distribution for a sequence of numeric values. It also creates a density plot of quantile function over F Distribution. Syntax: qf(x, df1, df2) Parameters: x: Numeric Vector df: Degree of Freedom Example 1: # R Program to compute value of # Quantile Function over F
1 min read
Compute the Value of Quantile Function over Weibull Distribution in R Programming - qweibull() Function
qweibull() function in R Language is used to compute the value of Quantile Function for Weibull Distribution. Syntax: qweibull(x, shape) Parameters: x: Numeric Vector shape: Shape Parameter Example 1: # R Program to compute the value of # Quantile Weibull Function # Creating a sequence of x-values x &lt;- seq(0, 1, by = 0.2) # Calling qweibull() Fu
1 min read
Compute the value of Quantile Function over Studentized Distribution in R Programming - qtukey() Function
qtukey() function in R Language is used to compute the value of Studentized Range Quantile Function over a sequence of Numeric Values. Syntax: qtukey(x, nmeans, df) Parameters: x: Numeric Vector nmeans: Number of means df: Degree of Freedom Example 1: # R Program to compute the value of # Quantile Function Studentized Range # Creating a sequence of
1 min read
Compute the value of Quantile Function over Wilcoxon Signedrank Distribution in R Programming - qsignrank() Function
qsignrank() function in R Language is used to compute the value of Quantile Function(QF) over Wilcoxon Signedrank Statistic Distribution for a sequence of Numeric values. Syntax: qsignrank(x, n) Parameters: x: Numeric Vector n: Sample Size Example 1: # R Program to compute the value of # QF over Wilcoxon Signedrank Distribution # Creating a sequenc
1 min read
Compute the value of Quantile Function over Wilcoxon Rank Sum Distribution in R Programming – qwilcox() Function
qwilcox() function in R Language is used to compute the value of Quantile Function(QF) for Wilcoxonrank Sum Statistic Distribution over a sequence of Numeric Values. Syntax: qwilcox(x, m, n) Parameters: x: Numeric Vector m: Larger sample size n: Smaller sample size Example 1: # R Program to compute the value of # QF over Wilcoxonrank Sum Distributi
1 min read
Compute Single Order Integral value of a Function in R Programming - integrate() Function
integrate() function in R Language is used to compute single order integral of the function provided. Syntax: integrate(f, lower, upper) Parameters: f: represents a function lower: represents lower limit of integration upper: represents upper limit of integration To know about more option parameters, use below command: help("integrate") Example 1:
1 min read