Open In App

Change Color of Range in ggplot2 Heatmap in R

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:




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 R

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:




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 R

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:




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 R

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:




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 R

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.


Article Tags :