How I Use scale_fill_gradient in R: A Guide with Examples

Learn how to use scale_fill_gradient in R to create stunning color gradients for your plots. Step-by-step tutorial with practical examples and tips.

RStudioDataLab
3 min readNov 27, 2023

Read the complete article and download the code:

Key Points

  • Scale_fill_gradient function allows us to create a continuous color gradient based on a numeric variable for our graphs.
  • We can use the scale_fill_gradient function with the ggplot2 package, a powerful and popular R data visualization tool.
  • We can customize the color gradient by changing the low and high colors, limits, breaks, and scale labels.
  • We can apply the color gradient to different types of plots, such as bars, points, lines, and polygons, to enhance the visual appeal and the information of the data.
  • We can also use other functions, such as scale_fill_gradient2 and scale_fill_gradientn, to create diverging or multiple color gradients for your plots.

ggplot2 Customization:

Create a bar chart with mpg as fill.

Before We Start, Make sure you have Read these Articles:

Add scale_fill_gradient with low = “red” and high = “blue”

Change the color gradient from red to blue to green to Brown

Change the color space from Lab to rgb

Create a new dataset with some missing values

Change the type of legend from colourbar to legend

Add titles and labels with labs

Adjust the size and shape with theme

Create three plots with scale_fill_gradient

--

--

RStudioDataLab
RStudioDataLab

Written by RStudioDataLab

I am a doctoral scholar, certified data analyst, freelancer, and blogger, offering complimentary tutorials to enrich our scientific community's knowledge.

No responses yet