Change Continuous Color Range in ggplot2 Plot in R (Example)

 

In this R post you’ll learn how to adjust the continuous color range in a ggplot2 plot.

Table of contents:

Here’s how to do it!

 

Example Data, Add-On Packages & Basic Graph

Have a look at the following example data:

set.seed(568323)                                         # Generate continuous example data
data <- data.frame(x = rnorm(500),
                   y = rnorm(500))
head(data)                                               # Print head of example data

 

table 1 data frame change continuous color range ggplot2 r

 

As you can see based on Table 1, our exemplifying data is a data frame containing two columns called “x” and “y”.

We also have to install and load the ggplot2 package, in case we want to use the corresponding functions:

install.packages("ggplot2")                              # Install ggplot2 package
library("ggplot2")                                       # Load ggplot2

Now, we can draw a graphic of our data as follows:

ggp <- ggplot(data, aes(x, y, color = y)) +              # ggplot2 plot with default legend
  geom_point()
ggp                                                      # Draw ggplot2 scatterplot

 

r graph figure 1 change continuous color range ggplot2 r

 

Figure 1 shows the output of the previous code – A ggplot2 scatterplot with default color range.

 

Example: Set New Continuous Color Range in ggplot2 Plot Using colorRampPalette() & scale_colour_gradientn()

The following R programming syntax explains how to change the color range in a ggplot2 plot of continuous data.

As a first step, we have to define the first and last color (i.e. yellow and red) of our color range using the colorRampPalette function:

fun_color_range <- colorRampPalette(c("yellow", "red"))  # Create color generating function

The previous R code has defined a new function called fun_color_range, which can be used to generate a sequence of colors.

Let’s apply this new function to generate a sequence of 20 colors:

my_colors <- fun_color_range(20)                         # Generate color range
#  [1] "#FFFF00" "#FFF100" "#FFE400" "#FFD600" "#FFC900" "#FFBB00" "#FFAE00"
#  [8] "#FFA100" "#FF9300" "#FF8600" "#FF7800" "#FF6B00" "#FF5D00" "#FF5000"
# [15] "#FF4300" "#FF3500" "#FF2800" "#FF1A00" "#FF0D00" "#FF0000"
my_colors                                                # Print color range

As you can see, we have generated 20 hex color codes.

Next, we can use these colors and the scale_colour_gradientn function for our ggplot2 scatterplot. Have a look at the following R code:

ggp +                                                    # Change colors in ggplot2 plot
  scale_colour_gradientn(colors = my_colors)

 

r graph figure 2 change continuous color range ggplot2 r

 

The output of the previous syntax is shown in Figure 2: The values of our scatterplot with a different range of colors.

 

Video, Further Resources & Summary

I have recently published a video on my YouTube channel, which explains the contents of this article. Please find the video below.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

In addition, you may have a look at the other articles on https://www.statisticsglobe.com/. A selection of articles about similar topics such as colors, ggplot2, and graphics in R is shown below.

 

You have learned in this tutorial how to change the color range and set a fixed sequence in a ggplot2 plot in the R programming language. Let me know in the comments below, if you have additional comments and/or questions. Furthermore, please subscribe to my email newsletter to receive updates on new tutorials.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


2 Comments. Leave new

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top