1

早上好 SO 社区,我有以下代码片段:

library( animation )
library( dplyr )    
library( gganimate )
library( ggplot2 )

someData = data.frame( years = seq( -5000, 1000, by = 100 ) ) %>%
mutate( values = sample( 1:20, length( years ), replace = TRUE ),
      label = ifelse( years < 0, paste0( abs( years ), ' B.C.E.' ), years ) )

chart = ggplot( someData ) + 
geom_line( aes( x = years, y = values, frame = years, cumulative = TRUE ) ) +
ggtitle( 'Year:  ' )

gganimate( chart, interval = .2 )

这样,年份就会出现在情节的标题中(按数字顺序排列)。有没有办法把这些label值——按照它们在数据框中出现的顺序——代替?

我的第一次尝试是frame = label在美学中使用,geom_line但标签按字母顺序出现。因此,我尝试通过以下方式更改级别顺序:

someData$label = factor( someData$label, levels = someData$label, order = TRUE )

但这给了我以下错误:

Error in frame_vec == f : comparison of these types is not implemented
In addition: Warning message:
In FUN(X[[i]], ...) :
Incompatible methods ("Ops.ordered", "Ops.factor") for "=="

任何帮助将不胜感激,谢谢。

4

1 回答 1

1

实际上有一个关于此功能的待处理拉取请求,但尚未合并到原始存储库中。同时,您可以使用 安装修改后的包devtools::install_github("nteetor/gganimate"),然后您可以执行以下操作:

gg_animate(chart, title_frame = ~ with(someData, label[years == .]))
于 2017-12-08T13:17:42.113 回答