How to create a heatmap in R

[This article was first published on Data Science Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The post How to create a heatmap in R appeared first on Data Science Tutorials

What do you have to lose?. Check out Data Science tutorials here Data Science Tutorials.

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

The post How to create a heatmap in R appeared first on Data Science Tutorials

Learn how to expert in the Data Science field with Data Science Tutorials.

To leave a comment for the author, please follow the link and comment on their blog: Data Science Tutorials.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)