ggplotly工具提示中的日期格式

发布于 2025-01-30 02:16:38 字数 1128 浏览 3 评论 0原文

我正在使用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"))

“”

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?

Below is a short piece of the code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离去的眼神 2025-02-06 02:16:38

如果您仅在工具提示中使用文本,则可以使用text元素将您传递给ggplot的元素呈现更复杂的工具提示。您只需要调用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"))

希望对您有所帮助!

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

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文