Unconfigured Ad

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts
  • crazyhottommy
    Senior Member
    • Apr 2012
    • 187

    K-means clustering and heatmap

    Hi everyone,

    I was plotting a ChIP-seq data using the pheatmap, see code below:

    km<- kmeans(m1,2) # determin how many cluster you want, I specify 2 here

    m.kmeans<- cbind(m1, km$cluster) # combine the cluster with the matrix

    dim(m.kmeans)
    # [1] 903 602
    # the last column is 602
    o<- order(m.kmeans[,602]) # order the last column

    m.kmeans<- m.kmeans[o,] # order the matrix according to the order of the last column

    pheatmap( m.kmeans[,1:601], cluster_rows = F, cluster_cols = F, col= hmcols, breaks = bk, legend=FALSE, show_rownames=FALSE, show_colnames=FALSE)

    It works fine for me, I clustered the data to two groups by specifying K=2, the problem is that group 1 sometimes shows up in the upper part of the heatmap, sometimes it shows up in the bottom part of the figure if I plot it several times.

    I think it has to do with the assignment of the group number, say, the first group is assigned to 1, the other is assigned to 2. However, next time if you plot the same data, the first group assigned to 2, the other is assigned to 1. R randomly assigns the number to the groups.

    How can control this?

    Thank you every much!
  • JackieBadger
    Senior Member
    • Mar 2009
    • 385

    #2
    I have had the same issues using k-means clustering and discriminate analysis of principle components (DAPC) package. While I can't answer your question, the package has a help mailing list that may be able to provide a solution.

    Comment

    • jmw86069
      Member
      • Jun 2009
      • 31

      #3
      Easiest fix is to call set.seed(123); before you run k-means, and/or before you run pheatmap. Depends upon which step(s) you want to be reproducible. If it is just the order on the heatmap itself, where you're not re-running k-means, you can also precompute the row dendrogram and send it to pheatmap. Doing it that way may be better for you in the long run since it gives you more control over how the clustering is scored and linked. I recommend hcluster() from the amap package, it is much faster than default hclust, as fast as other competing hclust replacements, but also offers scoring by a few types of correlation.

      Comment

      • scyxr
        Junior Member
        • Sep 2012
        • 2

        #4
        The same issue for me all the time. How can we assign each cluster number such as cluster 1 ,cluster 2 into the heat map. Hopefully someones knowing about this could give us the help.
        Thx.

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          From Collection to Sequencing: Why Sample Preparation and Preservation Define Sequencing Data
          by SEQadmin2


          Data variability is still an issue in sequencing technologies despite the advances in reproducibility and accuracy of these platforms. But the problem does not originate in the sequencing itself, but in the previous steps, before the sample reaches the sequencer.


          The first step is collection, followed by preservation and sample preparation for analysis. Most scientists overlook those steps, but not being careful might just be skewing the experiment’s results.
          ...
          06-02-2026, 10:05 AM
        • SEQadmin2
          Single-Cell Sequencing at an Inflection Point: Early Impacts of New Platforms and Emerging Trends
          by SEQadmin2


          With the launch of new single-cell sequencing platforms in 2026, the field stands at an exciting inflection point. This article surveys the most impactful advances in the field and discusses how they’re reshaping research in cancer, immunology, and beyond.


          Introduction

          Single-cell sequencing technologies have undergone remarkable advances over the past decade, transitioning from low-throughput experimental approaches to highly scalable platforms capable of...
          05-22-2026, 06:42 AM
        • SEQadmin2
          Environmental Genomics in the Age of NGS: From Microbes to Conservation Strategies
          by SEQadmin2

          Studying ecosystems means dealing with complex, multi-species communities that are hard to observe at scale. This complexity, however, hides many important questions to be answered, from how biogeochemical cycles work and how climate change can affect species distribution to how conservation strategies can work best.


          Genomics, particularly since the expansion of NGS, has transformed ecosystem ecology. By sequencing environmental DNA, we can now assess biodiversity without direct...
          05-06-2026, 09:04 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, Yesterday, 08:59 AM
        0 responses
        13 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-02-2026, 12:03 PM
        0 responses
        21 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 06-02-2026, 11:40 AM
        0 responses
        18 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 05-28-2026, 11:40 AM
        0 responses
        31 views
        0 reactions
        Last Post SEQadmin2  
        Working...