Hello, i have been struggling with layer_fun or cell_fun of bubble and i found that the radii of bubble is not correctly added into the heatmap and column_split is causing the issue but i am unable to get it done with column_split. Below is the script used:
##############################
spring_winter_dna_cdna_cog_cat2<-spring_winter_rdna_cog2 %>%filter(!grepl("BLANK|FJORD|LAKE",Sample_id)) %>% na.omit() %>% distinct() %>%
group_by(Sample_id,cog_id) %>% summarise(rel_size_cdna_rep_mean=mean(rel_size_cdna_rep),size_sum_by_fact_cdna_mean=mean(size_sum_by_fact_cdna),cog_abb=dplyr::first(cog_abb),.groups = "drop") %>% mutate(
cog_abb = ifelse(nchar(cog_abb) > 1, "other", cog_abb)) %>% distinct() %>%
group_by(Sample_id,cog_abb) %>%
summarise(rel_size_cdna_rep_mean_sum=mean(rel_size_cdna_rep_mean), size_sum_by_fact_cdna_sum=mean(size_sum_by_fact_cdna_mean),.groups = "drop") %>%
left_join(cog_categgory,by="cog_abb") %>% mutate( category = replace_na(category, "Other multifunctional"))
cog_cdna_data<-spring_winter_dna_cdna_cog_cat2 %>%
select(Sample_id,rel_size_cdna_rep_mean_sum,category) %>%mutate(Sample_id = recode(Sample_id, !!!setNames(mapping_sw$newname, mapping_sw$oldname))) %>%
pivot_wider(names_from = Sample_id, values_from = rel_size_cdna_rep_mean_sum) %>%
column_to_rownames("category") %>%
as.matrix()
heatmap_matrix_cog_cat2<- heatmap_matrix_cog_cat[,ordered_columns]
cog_cdna_data2<- cog_cdna_data[rownames(heatmap_matrix_cog_cat2),ordered_columns]
bubble_colors <- colorRamp2(
c(0,mean(cog_cdna_data2), max(cog_cdna_data2, na.rm = TRUE) * 0.5, max(cog_cdna_data2, na.rm = TRUE)),
c("black" ,"yellow","purple", "red") # Light blue to dark blue
)
bubble_color_legend <- Legend(
col_fun = bubble_colors,
title = "Transcripts mapped\n(mean reads)",
title_gp = gpar(fontsize = 10),
break_dist = c(0.3, 0.3, 0.2),
title_position = "leftcenter-rot",
legend_height = unit(3, "cm"),
at = c(0,mean(cog_cdna_data2), max(cog_cdna_data2, na.rm = TRUE)*0.5, max(cog_cdna_data2, na.rm = TRUE)),
labels = c(
"0",
sprintf("%.0f\n(mean)", mean(cog_cdna_data2)),
sprintf("%.0f", max(cog_cdna_data2)*0.5),
sprintf("%.0f", max(cog_cdna_data2))
),
direction = "vertical",
)
radius_matrix <- sqrt(cog_cdna_data2 / max(cog_cdna_data2, na.rm = TRUE)) * 0.05
#Plot the heatmap
ht_rna_scg<-Heatmap(heatmap_matrix_cog_cat2,
cluster_rows = TRUE,
clustering_distance_rows="manhattan",
cluster_columns = FALSE,
#column_order = ordered_columns,
column_split = mapping_sw$habitat[match(ordered_columns, mapping_sw$newname)],
border_gp = gpar(col = "black"),
row_names_gp = gpar(fontsize = 10),
#show_row_names = FALSE,
column_names_gp = gpar(fontsize = 11),
# width =ncol(heatmap_matrix_cog_cat)*unit(8, "mm") ,
# height = nrow(heatmap_matrix)*unit(1.8, "mm") ,
# heatmap_width = unit(10, "cm"),
#heatmap_height = unit(20, "cm"),
#row_title = "COG categories",
#row_title_side = "right",
row_title_gp = gpar(fontsize=10),
column_title = "Sample",
column_title_side = "bottom",
column_title_gp = gpar(fontsize=10),
row_names_side= "right",
#row_dend_side= "left",
show_row_dend = FALSE,
top_annotation = top_anno_rna,
bottom_annotation = bottom_anno,
#left_annotation = row_anno_boxplot,
show_heatmap_legend = FALSE,
#heatmap_legend_param = list(title = "TPM",direction = "vertical"),
col = colorRamp2(c(0,2800,12000,24000,38000),
c("#FFFFEF","#89CFF0","#2C7BB6","#FFA500","#D7191C")),
cell_fun = function(j, i, x, y, width, height, fill) {
# i and j are from the ORIGINAL matrix (1-based indices)
value = cog_cdna_data2[i, j]
if (!is.na(value) && value > 0) {
# Calculate radius (you can use pre-calculated matrix too)
# r = radius_matrix[i, j] # If you pre-calculated
r = sqrt(value / max(cog_cdna_data2, na.rm = TRUE)) * 0.08
grid.circle(
x = x,
y = y,
r = unit(r, "npc"),
gp = gpar(
fill = bubble_colors(value),
col = "black",
lwd = 0.5,
alpha = 0.7
)
)
}
}
)
############################################
Please suggest the solution. AI is not recognizing the error
Hello, i have been struggling with layer_fun or cell_fun of bubble and i found that the radii of bubble is not correctly added into the heatmap and column_split is causing the issue but i am unable to get it done with column_split. Below is the script used:
##############################
spring_winter_dna_cdna_cog_cat2<-spring_winter_rdna_cog2 %>%filter(!grepl("BLANK|FJORD|LAKE",Sample_id)) %>% na.omit() %>% distinct() %>%
group_by(Sample_id,cog_id) %>% summarise(rel_size_cdna_rep_mean=mean(rel_size_cdna_rep),size_sum_by_fact_cdna_mean=mean(size_sum_by_fact_cdna),cog_abb=dplyr::first(cog_abb),.groups = "drop") %>% mutate(
cog_abb = ifelse(nchar(cog_abb) > 1, "other", cog_abb)) %>% distinct() %>%
group_by(Sample_id,cog_abb) %>%
summarise(rel_size_cdna_rep_mean_sum=mean(rel_size_cdna_rep_mean), size_sum_by_fact_cdna_sum=mean(size_sum_by_fact_cdna_mean),.groups = "drop") %>%
left_join(cog_categgory,by="cog_abb") %>% mutate( category = replace_na(category, "Other multifunctional"))
cog_cdna_data<-spring_winter_dna_cdna_cog_cat2 %>%
select(Sample_id,rel_size_cdna_rep_mean_sum,category) %>%mutate(Sample_id = recode(Sample_id, !!!setNames(mapping_sw$newname, mapping_sw$oldname))) %>%
pivot_wider(names_from = Sample_id, values_from = rel_size_cdna_rep_mean_sum) %>%
column_to_rownames("category") %>%
as.matrix()
heatmap_matrix_cog_cat2<- heatmap_matrix_cog_cat[,ordered_columns]
cog_cdna_data2<- cog_cdna_data[rownames(heatmap_matrix_cog_cat2),ordered_columns]
bubble_colors <- colorRamp2(
c(0,mean(cog_cdna_data2), max(cog_cdna_data2, na.rm = TRUE) * 0.5, max(cog_cdna_data2, na.rm = TRUE)),
c("black" ,"yellow","purple", "red") # Light blue to dark blue
)
bubble_color_legend <- Legend(
col_fun = bubble_colors,
title = "Transcripts mapped\n(mean reads)",
title_gp = gpar(fontsize = 10),
break_dist = c(0.3, 0.3, 0.2),
title_position = "leftcenter-rot",
legend_height = unit(3, "cm"),
)
radius_matrix <- sqrt(cog_cdna_data2 / max(cog_cdna_data2, na.rm = TRUE)) * 0.05
#Plot the heatmap
ht_rna_scg<-Heatmap(heatmap_matrix_cog_cat2,
cluster_rows = TRUE,
clustering_distance_rows="manhattan",
cluster_columns = FALSE,
#column_order = ordered_columns,
column_split = mapping_sw$habitat[match(ordered_columns, mapping_sw$newname)],
border_gp = gpar(col = "black"),
row_names_gp = gpar(fontsize = 10),
#show_row_names = FALSE,
column_names_gp = gpar(fontsize = 11),
# width =ncol(heatmap_matrix_cog_cat)*unit(8, "mm") ,
# height = nrow(heatmap_matrix)*unit(1.8, "mm") ,
# heatmap_width = unit(10, "cm"),
#heatmap_height = unit(20, "cm"),
#row_title = "COG categories",
#row_title_side = "right",
row_title_gp = gpar(fontsize=10),
column_title = "Sample",
column_title_side = "bottom",
column_title_gp = gpar(fontsize=10),
row_names_side= "right",
#row_dend_side= "left",
show_row_dend = FALSE,
top_annotation = top_anno_rna,
bottom_annotation = bottom_anno,
#left_annotation = row_anno_boxplot,
show_heatmap_legend = FALSE,
#heatmap_legend_param = list(title = "TPM",direction = "vertical"),
col = colorRamp2(c(0,2800,12000,24000,38000),
c("#FFFFEF","#89CFF0","#2C7BB6","#FFA500","#D7191C")),
cell_fun = function(j, i, x, y, width, height, fill) {
# i and j are from the ORIGINAL matrix (1-based indices)
value = cog_cdna_data2[i, j]
)
############################################
Please suggest the solution. AI is not recognizing the error