Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add the names of my categories in the plot? #1218

Open
yeroslaviz opened this issue Nov 7, 2024 · 0 comments
Open

How to add the names of my categories in the plot? #1218

yeroslaviz opened this issue Nov 7, 2024 · 0 comments

Comments

@yeroslaviz
Copy link

I have the following table of data

    head(df.heatmap.ego)
    # A tibble: 6 × 11
      ID         Description      geneID `1403_AD4_2__S3` `1403_h4_1__S1` June24_AD1_S10 June24_AD2_S11
      <chr>      <chr>            <chr>             <dbl>           <dbl>          <dbl>          <dbl>
    1 GO:0043588 skin development COMP               9.70           3223.           4.16           9.55
    2 GO:0043588 skin development FLG              421.            11082.         827.           642.  
    3 GO:0043588 skin development IGFBP5         11650.            73528.       13051.         16871.  
    # ℹ 4 more variables: June24_AD3_S12 <dbl>, June24_H1_S7 <dbl>, June24_H2_S8 <dbl>, June24_H3_S9 <dbl>

I would like to use ComplexHeatmap to plot this table as a Heatmap, where my categories in the description column show me the genes common to different categories.

For that I would like to create rowAnnotations object. This is what I have so far:

    library(ComplexHeatmap)
    
    # creating the matrix and set rownames
    df_matrix <- as.matrix(df.heatmap.ego %>% select(-ID, -Description, -geneID))
    rownames(df_matrix) <- df.heatmap.ego$geneID
    # Scaling the data row-wise (genes)
    df_matrix <- t(scale(t(df_matrix)))
    
    # Assign colors for each GO category
    go_categories <- factor(df.heatmap.ego$Description)
    go_colors <- RColorBrewer::brewer.pal(n = length(levels(go_categories)), "Set3")
    names(go_colors) <- levels(go_categories)
    
    
    
    # Next, I create a binary matrix of geneID vs. description
    # Get unique categories
    go_terms <- unique(df.heatmap.ego$Description)
    
    # Initialize an empty matrix
    go_binary_matrix <- matrix(0, nrow = nrow(df_matrix), ncol = length(go_terms))
    rownames(go_binary_matrix) <- df.heatmap.ego$geneID
    colnames(go_binary_matrix) <- go_terms
    
    # Next, I fill the matrix with 1s where a gene is associated with a GO term
    for (i in 1:nrow(df.heatmap.ego)) {
      gene <- df.heatmap.ego$geneID[i]
      category <- df.heatmap.ego$Description[i]
      go_binary_matrix[gene, category] <- 1
    }
    
    # Create a list of annotation bars for each category
    annotations <- lapply(colnames(go_binary_matrix), function(category) {
      rowAnnotation(category = go_binary_matrix[, category],
                    col = list(category = c("0" = "white", "1" = "blue")),
                    name = category)
    })
    
    # Combine all annotations into a single row annotation object
    combined_annotations <- HeatmapAnnotation(
      df = do.call(cbind, lapply(annotations, function(x) x$category)),
      col = list(category = c("0" = "white", "1" = "blue"))
    )
    
    
    # Step 3: Create the heatmap and add the annotations
    # First, create an empty heatmap and then add each annotation individually
    heatmap_obj <- Heatmap(df_matrix, 
                           name = "Scaled Expression", 
                           row_title = "Genes", 
                               column_title = "Samples")
    
    # Add each annotation to the heatmap
    for (annotation in annotations) {
      heatmap_obj <- heatmap_obj + annotation
    }
    
    # Step 4: Generate the heatmap
    draw(heatmap_obj)

But this give me this image:

Screenshot 2024-11-07 at 4 01 06 PM

How do I change it to have the name of the category instead of the literal "category"?

thanks

Assa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant