我有很多时间系列的情节。下面可以找到一个示例图和代码。我使用ggplot2
构建了这个图,并使用ggplotly()
使其具有交互性。
但是,平滑曲线上的日期格式会丢失。交互式图表将日期显示为一些数值。
我怎样才能解决这个问题?
非常感谢
structure(list(Date = structure(c(15736, 15764, 15795, 15825,
15856, 15886), class = "Date"), CLI = c(99.93, 100.3, 100.96,
100.71, 100.62, 101.15)), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"))
plot5 <- df %>%
ggplot(aes(x = Date, y = CLI))+
geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4")+
scale_x_date(date_breaks = "6 month", date_labels = "%m/%y")+
theme_pander()+
geom_line(stat='smooth', method = "glm", alpha=0.5, color = "firebrick2", formula = y ~ poly(x, 5))+
geom_ribbon(stat='smooth',method = "glm", se=TRUE,formula = y ~ poly(x, 5), alpha=0.01)+
labs(x = "Date",
y = "Composite Leading Indicator")
ggplotly(plot5)
发布于 2022-02-07 05:43:07
根据您的情况调整我在this帖子上的答案,在工具提示中获得日期格式的一个选项是使用text
美学手动创建工具提示并将数字转换为适当的日期,如下所示:
plot <- df %>%
ggplot(aes(x = Date, y = CLI)) +
geom_line(size = 0.5, alpha = 0.75, show.legend = FALSE, color = "steelblue4") +
scale_x_date(date_labels = "%m/%y") +
# theme_pander()+
geom_line(aes(text = paste(
"date: ", as.Date(..x.., origin = "1970-01-01"), "<br>",
"y:", ..y..
)), stat = "smooth", method = "glm", alpha = 0.5, color = "firebrick2", formula = y ~ poly(x, 5)) +
geom_ribbon(stat = "smooth", method = "glm", se = TRUE, formula = y ~ poly(x, 5), alpha = 0.01) +
labs(
x = "Date",
y = "Composite Leading Indicator"
)
ggplotly(plot, tooltip = c("text"))
https://stackoverflow.com/questions/71018739
复制相似问题