2D Density Plot

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

# load the gcookbook package for the data
library(gcookbook)

# load the ggplot2 package
library(ggplot2)

# reset the graphing device
dev.off()

# create the ggplot2 data
p <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
  # add a layer with the points
  geom_point() +
  # and a layer for the density heatmap with the alpha and the color determined by density (the .. refers to the fact that density is a variable that was created inside the ggplot() function)
  stat_density2d(aes(alpha=..density.., fill=..density..), geom="tile", contour=FALSE)
null device
          1
p

png