我有一个包含每月观察结果的数据框。我想创建一个每月观察结果的动图。然而,在 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/