r - 如何在 gganimate 中取消按字母顺序排列帧顺序?

标签 r ggplot2 gganimate

我有一个包含每月观察结果的数据框。我想创建一个每月观察结果的动图。然而,在 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 在绘图时关心因素水平的顺序。

您可以从一开始就将月份向量创建为具有所需级别的因子,这取决于您。

编辑:引用@Gregor Thomas的评论

monthly.data$month = Factor(monthly.data$month,levels = Month.name)

关于r - 如何在 gganimate 中取消按字母顺序排列帧顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60643954/

相关文章:

r - 样本函数 R 不产生均匀分布的样本

r - 如何更改R中所有用户的文件权限

R ggplot2 aes arguments - 我怎么知道哪些参数有效?

R ggplot2 对只有几个数据点的折线图进行了轻微的平滑处理

r - 如何在 RStudio Viewer 中更改动画图 (gif) 的分辨率?

r - 使用 gganimate 制作动画时间

r Shiny : allow user to download shiny output in a desired directory

r - 检查包含空向量的列表的 "emptiness"(R 不识别为空列表)

r - 将带有线型的变量传递给 ggplot 线型

r - 用变化的刻度进行缩放(轴限制)