This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to add value of column to barchart on hover?

Hi!

I have a dataset called final_result with the colnames

[1] "pathway"                               "genes_involved_in_pathway"             "kit_gene_counts"                      
 [4] "leiomyo_lipo_gene_counts"              "nos_leiomyo_gene_counts"               "nos_lipo_gene_counts"                 
 [7] "genes_leiomyo_lipo"                    "genes_nos_leiomyo"                     "genes_nos_lipo"                       
[10] "genes_leiomyo_lipo_commonly_expressed" "genes_nos_leiomyo_commonly_expressed"  "genes_nos_lipo_commonly_expressed"
gg <- ggplot(final_result, aes(x = reorder(pathway, -kit_gene_counts))) +
  geom_bar(aes(y = kit_gene_counts), stat = "identity", fill = "lightblue", width = 0.5) +
  geom_bar(aes(y = leiomyo_lipo_gene_counts), stat = "identity", fill = "orange", width = 0.5) +
  geom_text(aes(y = kit_gene_counts, label = kit_gene_counts), vjust = -0.2, color = "black") +
  geom_text(aes(y = leiomyo_lipo_gene_counts, label = leiomyo_lipo_gene_counts), vjust = -0.2, color = 
 "black") +
  labs(title = "Concordance between Genes from Kit in Pathways and DGE genes Leiomyo Lipo", x = 
 "Pathway", y = "Number of Genes") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

I want for the blue bar on hover to appear genes_leiomyo_lipo_commonly_expressed value and for the orange bar on hover the genes_leiomyo_lipo value. I tried different approaches none with the desired result.

This is the current result of the code above.

enter image description here

r ggplot ggplotly

Have you run ggplotly(gg)? It looks like you might just be plotting the default ggplot object.

In the meanwhile I found a solution but not what I want

gg <- ggplot(final_result, aes(x = reorder(pathway, -kit_gene_counts), text = paste("Normal:", 
 genes_leiomyo_lipo_commonly_expressed," DGE: ",genes_leiomyo_lipo))) +
 geom_bar(aes(y = kit_gene_counts), stat = "identity", fill = "lightblue", width = 0.5) +
 geom_bar(aes(y = leiomyo_lipo_gene_counts), stat = "identity", fill = "orange", width = 0.5) +
 geom_text(aes(y = kit_gene_counts, label = kit_gene_counts), vjust = -0.2, color = "black") +
 geom_text(aes(y = leiomyo_lipo_gene_counts, label = leiomyo_lipo_gene_counts), vjust = -0.2, color = 
"black") +
 labs(title = "Concordance between Genes from Kit in Pathways and DGE genes Leiomyo Lipo", x = 
 "Pathway", y = "Number of Genes") +
  theme_minimal() +
   theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

 ggplotly(gg)

This add value of genes_leiomyo_lipo_commonly_expressed and genes_leiomyo_lipo on hover, yet when I touch one popup per bar showing both values. Since I have two bars on top of each other I would like to add genes_leiomyo_lipo_commonly_expressed value when I hover blue bar and genes_leiomyo_lipo the orange bar

The reason why that's happening is because you're plotting geom_bar twice rather than plotting by a group in the original aes() call. If you use pivot_longer on your dataframe, that will simplify your code a ton and fix your problem.

0 answers

No answers yet.

Log in to answer this question.