【问题标题】:Why doesn't plotly show the date properly on hover?为什么在悬停时不能正确显示日期?
【发布时间】:2018-04-02 08:18:30
【问题描述】:

我正在使用闪亮包创建仪表板。其中一个应该显示悬停日期的图没有这样做,而是显示日期的十进制值。 日期保存在 as.Date() 函数中的“日期”变量中:

custData$date <- as.Date(custData$date, "%m/%d/%Y")                

用于显示绘图的Server.R代码:

output$sidePlot2 <- renderPlotly({print(ggplotly(custData %>%
       group_by(date) %>% summarise(revenue = sum(lineTotal)) %>%
            ggplot(aes(x = date, y = revenue)) + geom_line() + geom_smooth(method =                  'auto', se = FALSE) + labs(x = 'Date', y = 'Revenue (£)', title =                       'Overall Revenue Trend by Date')
            )
       )
   }
)

【问题讨论】:

  • 可能是日期转换不对,能否分享重现您的数据?
  • 尝试使用devtools::install_github("tidyverse/ggplot2") 下载最新版本的ggplot2。我最近遇到了一个类似的问题,并通过从2.2.1(CRAN 版本)升级到2.2.1.9000来解决它

标签: r ggplot2 shiny plotly


【解决方案1】:

我认为您需要在 aes of ggplot 中定义 text

... %>% ggplot(aes(x = date, y = revenue,
           text = paste('Date: ', as.Date(date),
                        '<br>Revenue: ', revenue))) + 
  geom_line() + 
  ...

【讨论】:

    猜你喜欢
    • 2013-01-23
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2019-05-21
    • 2019-11-24
    相关资源
    最近更新 更多