【问题标题】:date format in tooltip of ggplotlyggplotly 工具提示中的日期格式
【发布时间】:2017-11-29 22:44:43
【问题描述】:

我正在使用 ggplotly 来显示交互式时间序列图。 x 轴采用日期格式,但 plotly 中的悬停工具提示正在将日期格式转换为数字(附截图)。关于如何让日期在工具提示中显示为正确日期的任何想法?

下面是一小段代码:

 output$ggplot <- renderPlotly({

plotbycity<-df_postgres %>% group_by(city, date, bedroooms) %>%
  filter(city %in% input$checkGroup & bedroooms==input$radio) %>%
  summarise(count=n(),rent=median(rent)) %>%
  ungroup() 

plotbycity$date<-as.Date(plotbycity$date)


# Error handling
plotbycity<-plotbycity[!is.na(plotbycity$city),]
if (is.null(plotbycity)) return(NULL)

#plotbycity<-ungroup(plotbycity)
#dat <- dat[c("rent", "bedroooms", "date", "City")]
#dat <- melt(dat,id.vars=c("date", "City", "bedroooms"),na.rm=TRUE)   #

# draw the line plot using ggplot
 gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
                             text = paste('obs: ', count))) +
  geom_line() +
  ggtitle("Monthly Rents")
#   #theme_hc(bgcolor = "darkunica") +
#   #scale_fill_hc("darkunica")
 
 p <- ggplotly(gg, tooltip = c("x", "y", "text"))

【问题讨论】:

    标签: r shiny plotly r-plotly


    【解决方案1】:

    如果您在工具提示中仅使用text,则可以使用传递给ggplottext 元素来呈现更复杂的工具提示。您只需要调用as.Date 并使用一些&lt;br&gt; html 标签如下:

    # draw the line plot using ggplot
    gg <-ggplot(plotbycity, aes(x = date, y = rent, group = city, color = city,
        text = paste('Rent ($):', rent,
                     '<br>Date: ', as.Date(date),
                     '<br>Obs: ', count))) +
        geom_line() +
        ggtitle("Monthly Rents")
    
    p <- ggplotly(gg, tooltip = c("text"))
    

    希望有帮助!

    【讨论】:

    • 伟大的权宜之计解决方案。虽然这似乎是 ggplotly 应该理解的东西(数据类型),因此值得提交的错误......
    • 此外,如果您有多个变量,此解决方案不允许您在工具提示中写入变量的名称,尽管工具提示为每个变量获取不同的颜色。见dropbox.com/s/zts1vghb004f6an/ggvisDateProblem.R?dl=0
    • 如果您尝试了此解决方案并且您的线路消失了,请跟进:stackoverflow.com/questions/46241436/…
    • @Khashir 感谢您的评论,确实是“text = ...”解决方案,然后我的台词消失了......我开始诅咒现在我遇到了另一个问题哈哈
    • 我的台词最初也消失了。我发现如果我在 geom_line 之后添加一个 geom_point() 图层,并将文本 aes 放在 geom_point 中,线条就会保持不变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多