Book Image

R Graphs Cookbook Second Edition

Book Image

R Graphs Cookbook Second Edition

Overview of this book

Table of Contents (22 chapters)
R Graphs Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating heat maps


Heat maps are colorful images that are very useful to summarize a large amount of data by highlighting hotspots or key trends in the data.

How to do it...

There are a few different ways to make heat maps in R. The simplest is to use the heatmap() function in the base library:

heatmap(as.matrix(mtcars), 
Rowv=NA, 
Colv=NA, 
col = heat.colors(256), 
scale="column",
margins=c(2,8),
main = "Car characteristics by Model")

How it works...

The example code has a lot of arguments, so it might look difficult at first sight. However, if we consider each argument in turn, we can understand how it works. The first argument to the heatmap() function is the dataset. We are using the built-in dataset mtcars, which holds data such as fuel efficiency (mpg), number of cylinders (cyl), weight (wt), and so on for different models of cars. The data needs to be in a matrix format, so we use the as.matrix() function. Rowv and Colv specify whether and how dendrograms should be displayed to the left...