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)
希望有所帮助!