这是dump
版的示例数据集:
sample_data <-
structure(list(
user_id = c("E", "T", "B", "R", "I", "L", "Z", "M", "W", "A"),
month = structure(c(17836, 17956, 17683, 17622, 18017, 17318, 17471, 17928, 17987, 18017), class = "Date"),
value_foo = c(92.68, 98.5, 36.82, 64.58, 36.59, 81.86, 64.99, 59.14, 78.41, 63.4),
value_bar = c(3.28, 4.17, 730.25, NA, 2971.95, 12.2, 3.85, 14.14, 40.69, 21.67)
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L))
请注意,每个user_id只有一行。如果我进行静态绘图,其中x和y分别是value_bar和value_foo,则它看起来像这样:
g <- ggplot(sample_data, aes(value_bar, value_foo)) +
geom_rect(xmin = -Inf, xmax = 30, ymin = 60, ymax = Inf, fill = "green", alpha = 0.05) +
geom_rect(xmin = -Inf, xmax = 30, ymin = 45, ymax = 60, fill = "yellow", alpha = 0.05) +
geom_rect(xmin = -Inf, xmax = 30, ymin = 30, ymax = 45, fill = "orange", alpha = 0.05) +
geom_rect(xmin = 30, xmax = 300, ymin = 60, ymax = Inf, fill = "yellow", alpha = 0.05) +
geom_rect(xmin = 300, xmax = Inf, ymin = 60, ymax = Inf, fill = "orange", alpha = 0.05) +
geom_rect(xmin = 30, xmax = 300, ymin = 45, ymax = 60, fill = "orange", alpha = 0.05) +
geom_rect(xmin = 300, xmax = Inf, ymin = 45, ymax = 60, fill = "red", alpha = 0.05) +
geom_rect(xmin = 30, xmax = Inf, ymin = 30, ymax = 45, fill = "red", alpha = 0.05) +
geom_rect(xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = 30, fill = "red", alpha = 0.05) +
geom_point() +
geom_label(aes(label = user_id), hjust = 0, nudge_x = 50)
g
现在,我想使其变得生动起来,每个帧代表一个唯一的月份。 (我知道这里似乎没有必要,但是我的真实数据集包括来自4000多个人的纵向数据。)我使用了gganimate
程序包,并按照its provided example进行了操作:
p <- g +
transition_time(month) +
labs(title = "{frame_time}")
animate(p, fps = 2)
现在这不是我期望的。问题包括:
我想要一个动画,
我该怎么做呢?为什么我的解决方案不起作用?谢谢。