ggplotly工具提示中的日期格式

use*_*311 16 r shiny plotly r-plotly

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

下面是一小段代码:

 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"))
 p[![enter image description here][1]][1]
Run Code Online (Sandbox Code Playgroud)

jad*_*nes 21

如果仅text在工具提示中使用,则可以使用text传递给的元素渲染更复杂的工具提示ggplot.您只需要调用as.Date并使用一些<br>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"))
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!

  • 如果你尝试了这个解决方案并且你的线路消失了,请跟进:/sf/ask/3236900551/?rq = 1 (3认同)
  • @Khashir感谢评论,字面上做了"text = ..."解决方案然后我的线条消失了...我开始诅咒现在我有另一个问题哈哈 (3认同)