早上好 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 "=="
任何帮助将不胜感激,谢谢。