Open In App

Change Color of Range in ggplot2 Heatmap in R

Last Updated : 13 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A heatmap depicts the relationship between two attributes of a dataframe as a color-coded tile. A heatmap produces a grid with multiple attributes of the dataframe, representing the relationship between the two attributes taken at a time.

Dataset in use: bestsellers

Let us first create a regular heatmap with colors provided by default. We will use the geom_tile() function of the ggplot2 library. It is essentially used to create heat maps.

Syntax: geom_tile(x,y,fill)

Parameter:

  • x: position on x-axis
  • y: position on y-axis
  • fill: numeric values that will be translated to colors

To this function, Var1 and Var2 of the melted dataframe are passed to x and y respectively. These represent the relationship between attributes taken two at a time. To fill parameters provide since that will be used to color-code the tiles based on some numeric value.

Example:

R




library(ggplot2)
library(reshape2)
 
df<-read.csv("bestsellers.csv")
 
data<-cor(df[sapply(df,is.numeric)])
data1<-melt(data)
 
ggplot(data1,
       aes(x=Var1,
           y=Var2,
           fill=value))+geom_tile()


Output:

Heatmap in RGeeksforgeeks

Heatmap in R

  • ggplot2 and reshape2, which offer functions for data visualization and data reshaping, respectively, are the necessary packages to load.
     
  • opens a dataframe called df and reads the information from the “bestsellers.csv” file. If the CSV file is in the current working directory, it will be read using the read.csv() function.
     
  • Using the cor() function, this line determines the correlation matrix of the numerical columns in the df dataframe. Using the sapply() and is.numeric() functions, it only picks the numeric columns from the df.
     
  • The melt() function from the reshape2 package is used in this line to convert the correlation matrix (data) into a long format. Three columns, Var1, Var2, and value, make up the resultant dataframe, called data1. Value holds the correlation values, whereas Var1 and Var2 stand for the variables that are being correlated.
     
  • Based on the data1 dataframe, a heatmap plot is produced by this code using ggplot2. The x and y aesthetics are designated as Var1 and Var2, respectively. Value, which represents the correlation values, is the aesthetic setting for the fill. Rectangular tiles that represent the correlation values in the plot are made using the geom_tile() method.

Method 1: Using scale_fill_gradient()

In this method, the starting and the ending value of the colors to define a range is given as an argument.

Syntax: scale_fill_gradient(low, high, guide)

Parameter:

  • low: starting value
  • high: ending value
  • guide: type of legend

Example:

R




library(ggplot2)
library(reshape2)
 
df<-read.csv("bestsellers.csv")
data<-cor(df[sapply(df,is.numeric)])
data1<-melt(data)
 
ggplot(data1,aes(x=Var1,
                 y=Var2,
                 fill=value))+geom_tile()+
scale_fill_gradient(low = "#86ebc9",
                    high = "#09855c",
                    guide = "colorbar")


Output:

Heatmap in RGeeksforgeeks

Heatmap in R

  • Data1: ggplot(aes(x = Var1, y = Var2, fill = value)) Using the ggplot() function, this line establishes the plot’s fundamental framework. It provides the data source as the data1 dataframe and establishes the plot’s aesthetics. The fill aesthetic is linked to the value variable, while the x and y aesthetics are mapped to the Var1 and Var2 variables, respectively.
     
  • Geom_tile() adds the tiles to the plot, resulting in the visualization of a heatmap. The fill color of each tile indicates the correlation value kept in the value variable, and each tile is a combination of the Var1 and Var2 variables.
     
  • scale_fill_gradient Utilising the scale_fill_gradient() method, this line establishes the heatmap’s color gradient.  The “colorbar” parameter adds a legend in color. 
     
     

Method 2: Using scale_fill_manual()

Up until now, we were adding colors to the continuous values, in this method, the values are first converted into discrete ranges using the cut() function.

Syntax: cut(data, breaks)

Where breaks take a vector with values to divide the data by. Now again plot a heatmap but with the new data created after making it discrete. To add colors to such heatmap in ranges, use scale_fill_manual() with a vector of the colors for each range.

Syntax: scale_fill_manual(interval, values=vector of colors)

Example:

R




library(ggplot2)
library(reshape2)
 
df<-read.csv("bestsellers.csv")
data<-cor(df[sapply(df,is.numeric)])
data1<-melt(data)
 
data2<-data1
data2$group<-cut(data2$value,
                 breaks = c(-1,-0.5,0,0.5, 1))
 
 
ggplot(data2,aes(x=Var1,
                 y=Var2,
                 fill=group))+geom_tile()+
  scale_fill_manual(breaks = levels(data2$group),
                    values = c("#86ebc9", "#869ceb",
                               "#b986eb","#a1eb86"))


Output:

Heatmap in RGeeksforgeeks

Heatmap in R

  • With the help of the method scale_fill_manual(), you can manually set the breaks and appropriate colors for the plot’s fill scale. With customizable breaks and colors, it replaces the built-in fill scale.
     
  • levels(data2$group) is the value set for the breaks parameter, which establishes the fill scale’s breaks or categories. It is assumed that the data2 dataframe has categorical values in the “group” column.
     
  • values = “#86ebc9”, “#869ceb”, “#b986eb”, “#a1eb86” A vector of colours, c(“#86ebc9”, “#869ceb”, “#b986eb”, “#a1eb86”), is specified as the values argument. Each color is associated with a certain division or category in the fill scale. In this illustration, the first break corresponds to the color #86ebc9, the second break to the color #869ceb, and so on.
     

Method 3: Using Scale_fill_viridis

These examples show how to use the additional techniques to modify a ggplot2 heatmap’s color range. If you need to visualize something differently, feel free to tweak the arguments and experiment with various color schemes.

Example:

R




library(ggplot2)
library(reshape2)
 
df<-read.csv("bestsellers.csv")
data<-cor(df[sapply(df,is.numeric)])
data1<-melt(data)
 
# Create a heatmap with the viridis color palette
ggplot(data1, aes(x=Var1, y=Var2, fill = value)) +
  geom_tile() +
  scale_fill_viridis()


output:

Heatmap in RGeeksforgeeks

Heatmap in R

  • scale_fill_viridis(): This line uses the scale_fill_viridis() function to set the fill color scale for the heatmap. The fill scale is colored using the viridis color scheme. The fill scale’s colors are automatically assigned by the scale_fill_viridis() function based on the correlation values’ range. It makes sure that the colors vary in intensity perceptibly in a regular manner as they move from low to high values.
     

Overall, this code employs ggplot2 to produce a heatmap plot. It uses the value variable to determine the fill color of the tiles and maps the Var1 and Var2 variables to the x and y aesthetics, respectively. The viridis color palette is used by the scale_fill_viridis() function to produce a visually pleasing and illuminating color gradient that depicts the correlation values in the heatmap.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads