What is the gray() function in R?

Overview

We obtain a vector of colors from a vector of gray levels using the gray() function in the R programming language.

Syntax

gray(level, alpha = NULL)

Parameter value

The gray() function takes the following parameter values:

  • level (required): This represents the vector which has the desired gray levels between 0 and 1. It’s worth noting that 0 indicates black while 1 indicates white.
  • alpha (optional): This is an optional value indicating the opacity, if specifed.

Return value

The gray() function returns a vector of the color having the same length as the input parameter level.

Example

# creating a level parameter
mylevel <- c( 0.1, 0.5, 1.0 )
# implementing the gray() function
gray(mylevel, alpha=TRUE)

Explanation

In the code above, we see the following:

  • Line 2: We create a vector object mylevel representing the level parameter.
  • Line 5: We apply the gray() function on the vector object and return the result to the console.

Free Resources