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

plot_heatmap Error: cannot take a sample larger than the population when 'replace = FALSE' #59

Open
bednarsky opened this issue Nov 16, 2024 · 5 comments · May be fixed by #64
Open

plot_heatmap Error: cannot take a sample larger than the population when 'replace = FALSE' #59

bednarsky opened this issue Nov 16, 2024 · 5 comments · May be fixed by #64
Labels
bug Something isn't working

Comments

@bednarsky
Copy link

bednarsky commented Nov 16, 2024

Error:

Attaching package: ‘fastcluster’

The following object is masked from ‘package:stats’:

hclust

Error in sample.int(length(x), size, replace, prob) :
cannot take a sample larger than the population when 'replace = FALSE'
Calls: sample -> sample.int

@bednarsky bednarsky changed the title plot_heatmap cannot take a sample larger than the population when 'replace = FALSE' plot_heatmap Error: cannot take a sample larger than the population when 'replace = FALSE' Nov 16, 2024
@bednarsky
Copy link
Author

bednarsky commented Nov 18, 2024

+ randomColor <- function() {
+   paste0("#", paste0(sample(c(0:9, letters[1:6]), size = 6, replace = TRUE), collapse = ""))
+ }

# make color mapping
if (is.numeric(metadata[[metadata_col]])){
    #check if divergent or sequential?
    row_annot <- HeatmapAnnotation(df=metadata[,metadata_col, drop=FALSE], which="row")
    
}else{
    n_cat <- length(unique(metadata[[metadata_col]]))
    qual_col_pals = brewer.pal.info[brewer.pal.info$category == 'qual',]
    all_brewer_colors <- unique(unlist(mapply(brewer.pal, qual_col_pals$maxcolors, rownames(qual_col_pals))))
+    if (n_cat <= length(all_brewer_colors)){
+        # get nice colors
        colors <- sample(all_brewer_colors,n_cat)
+    }else{
+        # too many groups, get random colors
+        colors <- replicate(n_cat, randomColor())
+    }
    names(colors) <- unique(metadata[[metadata_col]])
    colors_list <- list()
    colors_list[[metadata_col]] <- colors# put here the mapped colors
    
    row_annot <- HeatmapAnnotation(df=metadata[,metadata_col, drop=FALSE], which="row", col=colors_list)
}

@sreichl sreichl added the bug Something isn't working label Nov 18, 2024
@sreichl
Copy link
Collaborator

sreichl commented Nov 18, 2024

Wow thats a lot of code instead of one line ^^. Sure this is all necessary?

If you have tested the proposed fix feel invited to make a PR.

@bednarsky
Copy link
Author

It's not really that much more, highlighted it above now. Could also just remove the 'nice colors' part completely, but think it's nice that you first select for varying colors if there aren't that many

@sreichl
Copy link
Collaborator

sreichl commented Nov 18, 2024

ah okay, thought it was replacing only the one line haha

@bednarsky
Copy link
Author

No, I just assumed you know all your code by heart :D

@bednarsky bednarsky linked a pull request Nov 19, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants