如何在 gganimate 中 un-alphabetize 帧顺序?

How to un-alphabetize frame order in gganimate?

我每个月都有一个包含观察值的数据框。我想每个月创建一个观察结果的 gif。然而,在 gganimate 中,帧是按字母顺序排列的(例如从四月开始而不是一月)。如何更改帧的顺序?

或者,我可以使用月份数将绘图正确设置为 运行。但是,在那种情况下,我将不得不将框架标题更改为一月、二月、三月等,而不是 1、2、3 ......我意识到 this response 可以解决问题,但它仍然没有回答是否可以以某种方式指定 gganimate 中帧的顺序。

monthly.data <- data.frame(month = rep(c("January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December"), 10), xval = rnorm(n = 120,10,3), yval=rnorm(120,10,3))

    # This plots it in alphabetical order
    ggplot(monthly.data, aes(x = xval, y = yval))+
      geom_point()+
      transition_states(month)+
      ggtitle("{closest_state}")

    # Ordering the dataframe correctly doesn't help
    mothly.data <- arrange(monthly.data, factor(month, levels = c("January", "February", "March", "April", "May","June", "July", "August", "September", "October", "November", "December")))

    ggplot(monthly.data, aes(x = xval, y = yval))+
      geom_point()+
      transition_states(month)+
      ggtitle("{closest_state}")

ggplot 关心绘图时因子水平的顺序。

您可以将月向量创建为 factor,从一开始就具有所需的水平,这取决于您。

编辑:参考@Gregor Thomas 的评论

monthly.data$month = factor(monthly.data$month, levels = month.name)