【问题标题】:Plotly and ggplot2 hover text in barplot from R not workingPlotly 和 ggplot2 在 R 的条形图中悬停文本不起作用
【发布时间】:2017-03-22 15:51:00
【问题描述】:

这是我在这里的第一个问题,如果我在发布它或解释我的问题时犯了任何错误,我很乐意听到任何反馈。

我正在使用 Shiny、ggplot 和 Plotly 在 R 中构建一个工具。我有一个来自 plotly library 的示例,我可以看到在使用 ggplot 渲染图形时可能会有一个工具提示。

按照我的示例代码:

library(shiny)
library(ggplot2)
library(plotly)
library(dplyr)
library(reshape)

ui <- fluidPage(
  plotlyOutput("distPlot")
)

server <- function(input, output) {
  output$distPlot <- renderPlotly({
  dat <- data.frame(
  time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")),
  total_bill = c(14.89, 17.23)
)

p <- ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) +
  geom_bar(stat="identity")
    p <- p + geom_bar(stat = "identity")
    
    ggplotly(p)
  })
}

shinyApp(ui = ui, server = server)

当我使用上面的代码时,工具提示不会出现,但是如果我删除 fill = X2 参数,工具提示就会出现:

     p <- ggplot(data=dat, aes(x=time, y=total_bill)) +
  geom_bar(stat="identity")

在我的应用程序中,我需要使用填充参数来按颜色分隔我拥有的“组”并显示图例。

我使用 javascript 和其他解决方案在互联网上搜索了许多“解决方法”,但由于它是 plotly 的本机功能,我希望以更简单的方式使用它。

提前感谢您的帮助!!

【问题讨论】:

  • 试试g &lt;- plotly_build(p)。然后看g的结构,应该找到g$x$data[[i]]$text。看看那里的价值并改变它。

标签: r plot ggplot2 shiny plotly


【解决方案1】:

这是一个已知问题,您可以使用它来修复它:

p <- ggplotly(p)

for (i in 1:length(p$x$data)){
    p$x$data[[i]]$text <- c(p$x$data[[i]]$text, "") 
}
p

【讨论】:

  • 您好@GGamba 感谢您的回答,第一个选项工作正常,但是,第二个没有。我相信这是一个错误,我找不到解决方法。
猜你喜欢
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 2021-04-19
  • 2017-11-18
  • 2022-01-03
  • 2015-04-10
相关资源
最近更新 更多