【问题标题】:Labeling Dates In gganimate在 gganimate 中标记日期
【发布时间】:2019-11-14 01:51:01
【问题描述】:

假设我们有以下数据框:

> 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 轴上标记“日期”列,使其保持“月/日期”格式?

【问题讨论】:

    标签: r ggplot2 gganimate


    【解决方案1】:

    ggplot 可以处理日期,但它们必须是日期类型。否则,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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 2011-04-29
      • 1970-01-01
      • 2015-09-11
      • 1970-01-01
      相关资源
      最近更新 更多