R scale_fill_distiller() Function

The scale_fill_distiller() function in R is used to create a fill color scale for continuous data based on ColorBrewer palettes.

Syntax

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

Parameters

  1. type: It is one of “seq” (sequential), “div” (diverging), or “qual” (qualitative)
  2. palette: If a string will use that named palette. If a number is indexed into the list of palettes of the appropriate type. The list of available palettes can be found in the Palettes section.
  3. 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.
  4. aesthetics: The character string or vector of character strings listing the name(s) of the aesthetic(s) that this scale works with.
  5. guide: Type of legend. Use “colourbar” for continuous color bar or “legend” for discrete color legend.
  6. na.value: It is a color for missing values.
  7. values: If colors are not evenly positioned along the gradient, this vector gives the position (between 0 and 1) for each color in the colors vector.
  8. space: It is a color space in which to calculate the gradient. Must be “Lab” – other values are deprecated.

Example

library(ggplot2)

# Create a scatterplot of car data
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point(size = 3) +
  labs(
    title = "Fuel Efficiency by Engine Displacement and Vehicle Class",
    x = "Engine Displacement (L)", y = "Highway Fuel Economy (mpg)",
    color = "Vehicle Class"
  ) +
  scale_fill_distiller(palette = "Spectral", direction = 1,
                       name = "Vehicle Class")

Output

Output of scale_fill_distiller() Function in R

In this example, the function applies a distiller color scheme to the fill aesthetic of the plot based on the class variable.

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.

The choice of palette should be based on the data and the type of visualization. Some palettes are more suitable for specific data types and can accurately convey information.

Leave a Comment