首页 > 解决方案 > 在 R Shiny flexdashboard 中单击带有 Plotly 的直方图栏上的超链接悬停文本

问题描述

我创建了一个直方图,可以为每个条形显示几个超链接。但是,在我能够单击它们之前,悬停文本一直消失。这里有一个类似的问题:Open Link on Datapoint Click with Plotly in R Shiny,但我需要在图上显示要单击的名称。

任何帮助将不胜感激

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

在此处输入图像描述

非常感谢!

标签: rshinyhoverplotlyflexdashboard

解决方案


推荐阅读