How to create a heatmap in R

How to create a heatmap in R, you must convert a numerical matrix into a data frame that ggplot2 can interpret.

Use the melt function from the reshape package for that.

How to make a rounded corner bar plot in R? – Data Science Tutorials

library(reshape)
set.seed(123)
m <- matrix(round(rnorm(200), 2), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)
# Transform the matrix in long format
df <- melt(m)
colnames(df) <- c("x", "y", "value")
head(df)
     x     y value
1 Row 1 Col 1 -0.56
2 Row 2 Col 1 -0.23
3 Row 3 Col 1  1.56
4 Row 4 Col 1  0.07
5 Row 5 Col 1  0.13
6 Row 6 Col 1  1.72

Heat map with geom_tile

When using geom_tile in ggplot2, a heap map can be produced by giving categorical variables to the x and y inputs and a continuous variable to the fill argument of the aes function.

Data Science Challenges in R Programming Language (datasciencetut.com)

# install.packages("ggplot2")
library(ggplot2)
ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile()

Square tiles

Be aware that the tiles may not be squared depending on the size of the plotting window. Use cood_fixed if you wish to keep them square.

library(ggplot2)
ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile() +
  coord_fixed()

Border customization

With colour, lwd, and linetype, respectively, you can change the border colour, line width, and line style of the tiles.

How to Use Mutate function in R – Data Science Tutorials

library(ggplot2)
ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "white",
            lwd = 1.5,
            linetype = 1) +
  coord_fixed()

Adding the values

Additionally, you may use the geom_text function to add values over tiles while giving a number variable as the label argument for the aes function.

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  geom_text(aes(label = value), color = "white", size = 4) +
  coord_fixed()

Algorithm Classifications in Machine Learning – Data Science Tutorials

Color palette

There are three options for altering the heat map’s default colour scheme.

scale_fill_gradient

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradient(low = "white", high = "red") +
  coord_fixed()

Random Forest Machine Learning Introduction – Data Science Tutorials

scale_fill_gradient2

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradient2(low = "#075AFF",
                       mid = "#FFFFCC",
                       high = "#FF0000") +
  coord_fixed()

How to Group and Summarize Data in R – Data Science Tutorials

scale_fill_gradientn

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradientn(colors = hcl.colors(20, "RdYlGn")) +
  coord_fixed()

How to create Anatogram plot in R – Data Science Tutorials

You may also like...

Leave a Reply

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

3 × 4 =