0

I have created a histogram that can show, for each bar, several hyperlinks. However, the hover text keeps dissapearing before I am able to click them. There is a similar question here: Open Link on Datapoint Click with Plotly in R Shiny, but I need to show the names to be clicked on the plot.

Any help would be greatly appreciated

library("data.table")
library("dplyr")
library("ggplot2")


term_id = c("label1", "label2")
term_name = c("soap", "vanila")
adjust_pvalue = c(0,0)
freq = c(2,5)
ratio = c("(2/95)", "(5/13)")
pvalue = c(0,0)
links = c("<a href='https://en.wikipedia.org/wiki/SOAP'>SOAP</a>, <a href='https://es.wikipedia.org/wiki/Jab%C3%B3n'>Jabon</a>",
                        "<a href='https://en.wikipedia.org/wiki/Multilayer_perceptron'>MP</a>, <a href='https://es.wikipedia.org/wiki/Vanilla'>flower</a>")

mytable = data.frame(term_id = term_id, term_name = term_name, adjust_pvalue = adjust_pvalue,
           freq = freq,
           ratio = ratio, pvalue = pvalue, links = links)

p <- mytable %>%
  
  # prepare text for tooltip
  dplyr::mutate(text = paste("ID Number: ", term_id, "\nTerm: ", term_name,  "\nGene Ratio: ", ratio, "\nAdjust pvalue: ", adjust_pvalue, "\nCommon genes: ", links)) %>%
  # Classic ggplot
  ggplot(  ggplot2::aes(x= freq , y= reorder(paste0("",term_id,": ",term_name,""), -adjust_pvalue),  fill = adjust_pvalue, text=text)) +
  theme (text = element_text(size= 8)) +
  geom_bar(stat="identity", alpha=0.7) +
  scale_fill_gradientn(colours = rainbow(2), trans= "log") +
  theme(legend.position="right") +
  labs(title = "My Analysis", x = "Number", y = "", fill = "p.value") + #
  theme(plot.title = element_text(size = 15),
        axis.title.x = element_text(size = 10),
        axis.title.y = element_text(size = 10),
        axis.text = element_text(
          angle = 0))

# turn ggplot interactive with plotly layout.title
newgg <- ggplotly(p, tooltip="text") %>%
  config(mathjax = "cdn",
         toImageButtonOptions = list(
           format = "svg",
           filename = "myplot",
           width = 1000,
           height = 600
         )
  )
newgg

enter image description here

Thank you very much!

0 Answers0