简体   繁体   中英

How to cluster a heatmap based on columns using ggplot in R?

Many questions have been asked before about clustering a heatmap. However, I can't seem to adapt any of the other answers to my specific problem. Also, a lot of other answers rely on using some other package to create the heatmap plot (whereas I want to stick with ggplot ).

But basically, I am trying to rearrange a heatmap created in ggplot based on columns. Hopefully the example below will explain what I mean.

If I create some data and plot a heat map like so:

library(colorspace)
library(ggplot2)

#create data
df <- data.frame(
  a = paste0("a", c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)),
  b = paste0("b", c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5))
  )

# create data to fill heatmap
c = c(-10, -9, -8, -2, -9,
      2, 5, 2, 0, 5,
      -5,-6,-7,-8,-9,
      2, 5, 2, 0, 5,
      -5,-6,-7,-8,-9
)

# From discrete to continuous
df$a <- match(df$a, sort(unique(df$a)))
df$b <- match(df$b, sort(unique(df$b)))

# set colour palette
pallette <- rev(sequential_hcl(palette = "Plasma", n = 100)) 

# plot
ggplot(df, aes(a, b)) +
  geom_tile(aes(fill = c)) +
  scale_fill_gradientn(
    limits = range(c),
    colors = pallette,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    )
  )

This creates something that looks like this: 热图

What Im trying to achieve is to reorder\cluster the columns so that columns with similar values are grouped together.

For example, in the plot above, columns a2 and a4 have high values... whereas columns a1,a3,a5 have low values. I'm trying to group together the columns with similar values. So, going from, say, high to low values, the above plot might be reordered like the following - a2,a4,a3,a5,a1... or something similar.

Any suggestions as to how I could do this?

You can reorder your x axis by decreasing c . You don't absolutely have to include c as a column in your dataset, but I would still recommend you have a , b , and c in your df :

df$c <- c  # not strictly required

ggplot(df, aes(reorder(a, -c), b)) +
  geom_tile(aes(fill = c)) +
  scale_fill_gradientn(
    limits = range(c),
    colors = pallette,
    guide = guide_colorbar(
      frame.colour = "black",
      ticks.colour = "black"
    )
  )

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM