Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • OLS Regression in R
    OLS Regression in R R
  • Artificial Intelligence Examples
    Artificial Intelligence Examples-Quick View Course
  • How to Add Superscripts and Subscripts to Plots in R?, The basic syntax for adding superscripts or subscripts to charts in R is as follows:
    How to Add Superscripts and Subscripts to Plots in R? R
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Filter Rows In R
    How to Filter Rows In R? R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • How to use image function in R
    How to use the image function in R R
  • Box Cox transformation in R
    Box Cox transformation in R R
How to create a heatmap in R

How to create a heatmap in R

Posted on October 24October 25 By Jim No Comments on How to create a heatmap in R
Tweet
Share
Share
Pin

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()

Check your inbox or spam folder to confirm your subscription.

How to create Anatogram plot in R – Data Science Tutorials

Tweet
Share
Share
Pin
R

Post navigation

Previous Post: Box Cox transformation in R
Next Post: The pheatmap function in R

Related Posts

  • How to create contingency tables in R
    How to create contingency tables in R? R
  • Triangular Distribution in R
    Triangular Distribution in R R
  • Top 10 online data science programmes
    Top 10 online data science programs Course
  • How to Replace String in Column in R
    How to Replace String in Column using R R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R
  • Linear Interpolation in R
    Linear Interpolation in R-approx R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Defensive Programming Strategies in R
  • Plot categorical data in R
  • Top Data Modeling Tools for 2023
  • Ogive Graph in R
  • Is R or Python Better for Data Science in Bangalore

Check your inbox or spam folder to confirm your subscription.

  • Data Scientist Career Path Map in Finance
  • Is Python the ideal language for machine learning
  • Convert character string to name class object
  • How to play sound at end of R Script
  • Pattern Searching in R
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • How to Recode Values in R
    How to Recode Values in R R
  • Top Data Science Examples You Should Know 2023
    Top Data Science Applications You Should Know 2023 Machine Learning
  • Separate a data frame column into multiple columns
    Separate a data frame column into multiple columns-tidyr Part3 R
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • How to handle Imbalanced Data
    How to handle Imbalanced Data? R
  • How to compare variances in R
    How to compare variances in R R
  • How to Calculate Ratios in R
    How to Calculate Ratios in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme