Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

heatmap with values (ggplot2)

Tags:

r

ggplot2

I've seen heatmaps with values made in various R graphics systems including lattice and base like this:

enter image description here

I tend to use ggplot2 a bit and would like to be able to make a heatmap with the corresponding cell values plotted. Here's the heat map and an attempt using geom_text:

library(reshape2, ggplot2) dat <- matrix(rnorm(100, 3, 1), ncol=10) names(dat) <- paste("X", 1:10) dat2 <- melt(dat, id.var = "X1") p1 <- ggplot(dat2, aes(as.factor(Var1), Var2, group=Var2)) +     geom_tile(aes(fill = value)) +     scale_fill_gradient(low = "white", high = "red")  p1  #attempt labs <- c(apply(round(dat[, -2], 1), 2, as.character)) p1 +  geom_text(aes(label=labs), size=1) 

Normally I can figure out the x and y values to pass but I don't know in this case since this info isn't stored in the data set. How can I place the text on the heatmap?

like image 752
Tyler Rinker Avatar asked Jan 12 '13 04:01

Tyler Rinker


People also ask

Can you make a heatmap in ggplot2?

To create a heatmap with the melted data so produced, we use geom_tile() function of the ggplot2 library. It is essentially used to create heatmaps.

How to use geom_ tile in ggplot2?

Heat map with geom_tile A heap map in ggplot2 can be created with geom_tile , passing the categorical variables to x and y arguments and the continuous variable to fill argument of aes . Note that depending on the plotting windows size the tiles might not be squared. If you want to keep them squared use cood_fixed .

How do you make a heatmap correlation in R?

Let's use the heatmaply package in R to plot a correlation heatmap using the heatmaply_cor( ) function. Correlation of the data is the input matrix with “Features” column as x and y axis parameters. Function: heatmaply_cor(x, limits = c(-1, 1), xlab, ylab, colors = cool_warm,k_row, k_col …)

How do I create a heat map in R?

A basic heatmap can be produced using either the R base function heatmap() or the function heatmap. 2() [in the gplots package]. The pheatmap() function, in the package of the same name, creates pretty heatmaps, where ones has better control over some graphical parameters such as cell size.


1 Answers

This has been updated to conform to tidyverse principles and improve poor use of ggplot2

Per SlowLeraner's comment I was easily able to do this:

library(tidyverse)  ## make data dat <- matrix(rnorm(100, 3, 1), ncol=10)  ## reshape data (tidy/tall form) dat2 <- dat %>%     tbl_df() %>%     rownames_to_column('Var1') %>%     gather(Var2, value, -Var1) %>%     mutate(         Var1 = factor(Var1, levels=1:10),         Var2 = factor(gsub("V", "", Var2), levels=1:10)     )  ## plot data ggplot(dat2, aes(Var1, Var2)) +     geom_tile(aes(fill = value)) +      geom_text(aes(label = round(value, 1))) +     scale_fill_gradient(low = "white", high = "red")  

enter image description here

like image 177
Tyler Rinker Avatar answered Oct 11 '22 16:10

Tyler Rinker