gganimate ggplot2 error when using transition_time() after transforming dataset in R… but no error if transformed outside of R

后端 未结 1 1991
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 07:33

Goal:

Import, transform / prep, and animate a coronavirus dataset from .xlsx using only R.

Text from Reproducible Error:

相关标签:
1条回答
  • 2020-12-22 07:59

    The problem is with your transformation of the column names into Dates. That seems to introduce NAs into the Dates, and makes the range indeterminate, which gganimate uses for the start and end of the animation.

    What worked for me was:

    names(wide) = janitor::make_clean_names(names(wide))
    

    and

    long <- wide %>%
      gather(Date, Cases, -county_name, -population) %>%
      rename(County = county_name, Population = population) %>%
      mutate(Date = as.Date(str_remove(Date, "cases_"), format = "%m_%d")) %>%
      mutate(Rate = Cases/Population)
    
    long %>% filter(is.na(Date))
    

    Alternatively, you could use str_remove(Date, "\\D+") instead of cleaning up the column names beforehand.

    0 讨论(0)
提交回复
热议问题