ggplotly 工具提示中的日期格式 [英] date format in tooltip of ggplotly

查看:19
本文介绍了ggplotly 工具提示中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using ggplotly to show an interactive time-series plot. The x axis is in date format, yet the hover tool tip in plotly is converting the date format to a numeric (screenshot attached). Any ideas on how to get the date to show as a proper date in the tooltip?

下面是一小段代码:

 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"))

推荐答案

如果您在工具提示中仅使用 text,则可以使用 text 传递给 ggplot 的元素.您只需要调用 as.Date 并使用一些 <br> html 标记,如下所示:

If you use just text in your tooltip, you can render a more complex tooltip by using the text element you pass to ggplot. You just need to call as.Date and use some <br> html tags as follows:

# 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 工具提示中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆