ggplotly工具提示中的日期格式
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您仅在工具提示中使用
文本
,则可以使用text
元素将您传递给ggplot
的元素呈现更复杂的工具提示。您只需要调用as.date
,然后使用一些&lt; br&gt;
html标签如下:希望对您有所帮助!
If you use just
text
in your tooltip, you can render a more complex tooltip by using thetext
element you pass toggplot
. You just need to callas.Date
and use some<br>
html tags as follows:Hope that helps!