heatmap with values (ggplot2)

前端 未结 2 1343
一向
一向 2020-11-28 05:03

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

\"enter

相关标签:
2条回答
  • 2020-11-28 05:13

    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") 
    

    0 讨论(0)
  • 2020-11-28 05:16

    There is another simpler way to make heatmaps with values. You can use pheatmap to do this.

    dat <- matrix(rnorm(100, 3, 1), ncol=10)
    names(dat) <- paste("X", 1:10)
    install.packages('pheatmap') # if not installed already
    library(pheatmap)
    pheatmap(dat, display_numbers = T)
    

    This will give you a plot like this

    If you want to remove clustering and use your color scheme you can do

    pheatmap(dat, display_numbers = T, color = colorRampPalette(c('white','red'))(100), cluster_rows = F, cluster_cols = F, fontsize_number = 15)
    

    You can also change the fontsize, format, and color of the displayed numbers.

    0 讨论(0)
提交回复
热议问题