library(ggplot2)library(dplyr)library(reshape2)
<- volcano %>% melt() %>% as_tibble() volcano.tidy
%>% volcano.tidy ggplot(aes(x = Var1, y = Var2, fill = value)) + # create a heatmap geom_raster() + # color palette scale_fill_viridis_c(direction = -1, option = "A") + # titles labs( title = "Volcano Topography", x = "X", y = "Y", fill = "volcano height") + # legend color bar guides( fill = guide_colorbar( barheight = unit(200, "pt"), title.position = "left", title.theme = element_text(angle = 90, hjust = .5))) + # theme theme_classic(base_size = 15) + theme( plot.title = element_text(face = "bold", hjust = .5), panel.grid = element_blank(), axis.line = element_blank())
Draw Heatmap in ggplot2 to Visualize Volcano Topography

This brief demo illustrates how to create a heatmap using geom_raster()
in ggplot2.
Continue Exploring — 🚀 one level up!
Check out the following heatmap with synchornized lineplot that visualize the sunspot activities in the past two hundred years.
Check out these beautiful heatmaps showing the disease incidences before and after the vaccine introduction throughout the U.S. history.
And check out this amazing heatmap visualizing the population distribution in Africa. In particular, it leverages magical mathematical transformations in the color scale (with a simple line of code) to unveil the underlying highly skewed data pattern.
All above heatmaps are generated using the generic function geom_tile()
or geom_raster()
. Alternatively, a heatmap can be created in the form of a 2D histogram using geom_bin2d()
– check out the following awesome 2D histogram with a world map overlay that visualizes the hurricane activities in North Atlantic Ocean.