What is scale_colour_distiller() Function in R

The scale_colour_distiller() is a function from the ggplot2 package that applies a color scale to the color aesthetic of a plot. For example, you can use the scale_colour_distiller() function to create a color gradient for a scatter plot based on a third variable.

The scale_colour_distiller() function uses a color scale generated using a distiller color scheme, a type of color scheme that emphasizes perceptual uniformity and is commonly used for data visualization. It takes several arguments, including the name of the color scheme, the number of colors to use in the scale, and the range of values to map to the scale.

Syntax

scale_colour_distiller(
  ...,
  type = "seq",
  palette = 1,
  direction = -1,
  values = NULL,
  space = "Lab",
  na.value = "grey50",
  guide = "colourbar",
  aesthetics = "colour"
)

Parameters

type: It is one of “seq” (sequential), “div” (diverging), or “qual” (qualitative)

palette: If a string will use that named palette. If a number will index into the list of palettes of the appropriate type. The list of available palettes can be found in the Palettes section.

direction: It sets the order of colors in the scale. If 1, the default colors are as output by RColorBrewer::brewer.pal(). If -1, the order of colors is reversed.

aesthetics: The character string or vector of character strings listing the name(s) of the aesthetic(s) that this scale works with.

guide: Type of legend. Use “colourbar” for continuous color bar or “legend” for discrete color legend.

na.value: It is a color for missing values.

values: If colours are not evenly positioned along the gradient, this vector gives the position (between 0 and 1) for each colour in the colours vector.

space: It is a colour space in which to calculate the gradient. Must be “Lab” – other values are deprecated.

: Other arguments.

Example

library(ggplot2)

# Create a scatterplot of car data
ggplot(mtcars, aes(x = hp, y = mpg, color = cyl)) +
 geom_point(size = 3) +
 labs(
   title = "Fuel Efficiency",
   x = "Horse power", y = "Miles per gallon",
   color = "Vehicle Class"
 ) +
 scale_colour_distiller(
   palette = "Spectral", direction = 1,
   name = "Number of cylinders"
 )

Output

scale_colour_distiller() Function in R

In this example, the scale_colour_distiller() function applies a distiller color scheme to the color aesthetic of the plot.

The palette argument specifies the name of the color scheme to use (“Spectral”), the direction argument specifies the direction of the color gradient (1 for increasing values), and the name argument specifies the name of the legend for the color scale.

That’s it.

Leave a Comment