Heatmaps In R Using ggplot2

Hi. I have played around with heatmaps in R and the ggplot2 package. Here is what I have worked with.


Table Of Contents

References

Dataset One: Strength Of Thermonplastic Composite Experiment Results

Dataset Two: Treatment and Block Effects On Egg Production


References

  • R Graphics Cookbook by Winston Chang (2012)
  • http://www.sthda.com/english/wiki/ggplot2-quick-correlation-matrix-heatmap-r-software-and-data-visualization
  • R Documentation for the datasets and ggplot() functions.
  • The faraway dataset library and the ggplot2 library in R


Dataset One: Strength Of Thermonplastic Composite Experiment Results

This first dataset is a small dataset with 9 row (observations) and 3 columns (variables). This data is from an experiment to test the strength of a thermoplastic composite. The image below is a screenshot of the R documentation of this dataset.

 

The ggplot2 and faraway libraries are loaded into R.

In the faraway package, this data is called composite. I save this data into a variable called compData. I also print out the contents of this table.

The structure of the dataset can be found by using the str() function.

The lines of code and output consists of changing the column names (for capitalization) and check the summary of the data. The summary() function gives some key summary statistics such as mean, median, counts for factors and more.

A Basic Heatmap Plot

Creating a basic heatmap plot does not require much code.  In the aes() part of ggplot(), I have x = Tape, y = Laser and the fill colours associated with Strength. The add-on function geom_tile() is needed to obtain the tiles/squares in the plot.


A Heatmap Plot With Labels And More Features

The heatmap plot still needs some adjustments. The labels could be fixed and a title needs to be added. The next lines of code will produce a more detailed heatmap plot.

 

Some Notes

  • scale_x_discrete() with the labels argument can give labels such as Slow, Medium and Fast
  • scale_fill_gradient2() allows for customization of the numbers in the scale legend
  • labs() are for the x-axis, y-axis labels, the legend title and the title
  • The theme() function allows for the visual customization of labels and titles.
  • geom_text() allows for labels in each tile


Dataset Two: Treatment and Block Effects On Egg Production

This second dataset is about treatment and block effects on egg production. The full details can be found in the screenshot image below.

I save the eggprod data into a variable called eggprod_data. Then I take a look at the data, its data structure and some summary statistics.

The column names can be renamed using colnames().


A Basic Heatmap Plot

Here is a basic heatmap plot which describes this data.

Like in the first heatmap in the first dataset, more can be done in terms of labelling and visual details.


An Updated Heatplot

Here is the code and output for this updated heatplot with more labels and titles.

 

Leave a Reply