Heatmaps are representations of matrix data where the individual values contained are represented as colors. Heatmaps can be realized in ggplot2
using the traditional functions available, but we have also included plots in this chapter that represent very specific type of graphs.
In order to realize a heatmap with ggplot2
, you will simply need to use the geom_tile()
function. These plots can be realized usually by representing two variables on both axes and the combinations between these variables are color-mapped using a third variable. Let's first create a simple dataset that we can use.
x1 <- seq(-10, 10, length.out = 10) y1 <- seq(-10, 10, length.out = 10) d1 <- expand.grid(x = x1, y = y1) d1$z <- d1$x^2 - d1$y^2
We will generate two vectors and then generate all possible combinations between these vectors using the expand.grid()
.function. We will then just create a third variable, which is a function of the previous ones. What we end up with finally is a 3D curve that can be represented in two dimensions by mapping the third variable to colors
.
We can use, for instance, the qplot()
function to easily represent this data as shown here:
qplot(x=x, y=y, data=d1, fill=z, geom="tile")
As illustrated, we simply had to select the two variables and then map them to a third variable. In the geom
argument, you can select the tile
geometry. You can see the resulting picture in Figure 7.13:
As you can see from the resulting plot, these plots are usually represented as squares obtained by the combinations of the variables represented on the two axes. On the other hand, when the results are particularly dense, the plot can also appear as a continuous shade shifting from one color to the other if you are in a situation where the color mapping variable follows a regular pattern. We will see the same example with a simulation with more data points. Moreover, as an alternative to the default color scheme proposed by ggplot2
, in these kinds of plots it is particularly effective when representing the values as shades between green and red, as is done in this second example:
x2 <- seq(-10, 10, length.out = 100) y2 <- seq(-10, 10, length.out = 100) d2 <- expand.grid(x = x2, y = y2) d2$z <- d2$x^2 - d2$y^2 ggplot(data=d2, aes(x=x, y=y, fill=z)) + geom_tile() + scale_fill_gradient(low="red", high="green")
In this second example, you have also seen how this plot can be realized with the ggplot
() function as an alternative to the use of qplot()
. The resulting plot is represented in Figure 7.14.
You can find additional examples of this type of plot in the help page of the geom_tile()
function, as also reported in the Further reading section.