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 of a single Z variable with a scale


In this recipe, we will make a heat map that shows the variation in values of one variable (z) along the x and y axes as a grid of colors and display a scale alongside.

Getting ready

We will only use the base graphics functions for this recipe. So, just open up the R prompt and type in the following code. We will use the sales.csv example dataset for this recipe. So, let's first load it:

sales<-read.csv("sales.csv")

We will use the RColorBrewer package for some good color palettes. So, let's make sure that it's installed and loaded:

install.packages("RColorBrewer")
library(RColorBrewer)

How to do it...

The sales dataset has monthly sales data for four cities. Let's make a heat map with the months along the x axis and the cities on the y axis:

rownames(sales)<-sales[,1]
sales<-sales[,-1]
data_matrix<-data.matrix(sales)

pal=brewer.pal(7,"YlOrRd")

breaks<-seq(3000,12000,1500)

#Create layout with 1 row and 2 columns (for the heatmap...