假设我们有以下数据框:
> df
date number
1 9/1 1
2 9/2 2
3 9/3 3
4 9/4 4
5 9/5 5
,其中“日期”列的格式为“月/日期”。
基于此数据,我想制作一个动画,该动画在x轴的“日期”列中具有相同的“月/日期”格式。
但是,R抛出一条错误消息,即“日期”列必须是有限数字。因此,我在“日期”列中删除了“ /”,现在可以正常使用了(请参阅附件 image)和以下代码:
df <- data.frame("date" = c(91, 92, 93, 94, 95), "number" = c(1,2,3,4,5))
p <- ggplot(df, aes(x = date, y = number, group =1)) +
geom_line() +
geom_point() +
transition_reveal(date)
p
但是,如何在动画的x轴上标记“日期”列,使其保持“月/日期”格式?
答案 0 :(得分:0)
ggplot可以处理日期,但是它们必须是date类型。否则,ggplot无法知道如何正确显示它们。据我所知,没有没有年份的日期的方法。但是,如果您的数据少于一年,ggplot将不会显示年份,而只会显示月份和日期。这是一个示例:
library(ggplot2)
library(gganimate)
library(gifski)
df <- data.frame("date" = c(91, 92, 93, 94, 95), "number" = c(1,2,3,4,5))
df$date <- as.Date(c("2019-09-01", "2019-09-02", "2019-09-03", "2019-09-04", "2019-09-05"))
p <- ggplot(df, aes(x = date, y = number, group =1)) +
geom_line() +
geom_point() +
transition_reveal(date)
p