2

I am trying to animate a plot of changing counts of something in each region of the UK over the course of 10 years.

I have an shapefile of UK regions which can be downloaded from this URL and loaded using:

uk_map <- read_sf(filepath)

I have a dataframe of counts that I want to plot for each year:

yearCountsAllRegions<-data.frame(year = c(2012, 2012, 2012, 2012, 2012, 2012, 2012, 
2012, 2012, 2012, 2012, 2012, 2012, 2013, 2013, 2013, 2013, 2013, 
2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2014, 2015, 
2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 2016, 
2016, 2016, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 
2017, 2017, 2017, 2017, 2017, 2018, 2018, 2018, 2018, 2018, 2018, 
2018, 2018, 2018, 2018, 2018, 2018, 2018, 2019, 2019, 2019, 2019, 
2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2019, 2020, 2020, 
2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 2020, 
2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 2021, 
2021, 2021, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
n = c(0L, 53L, 40L, 0L, 0L, 13L, 180L, 118L, 137L, 6L, 14L, 
0L, 0L, 0L, 145L, 49L, 0L, 5L, 47L, 304L, 104L, 127L, 15L, 25L, 
1L, 32L, 61L, 197L, 90L, 3L, 35L, 54L, 315L, 186L, 128L, 21L, 
47L, 35L, 79L, 67L, 282L, 91L, 17L, 130L, 58L, 396L, 248L, 133L, 
29L, 36L, 20L, 85L, 97L, 263L, 108L, 0L, 147L, 67L, 357L, 244L, 
130L, 28L, 43L, 13L, 130L, 97L, 284L, 150L, 0L, 180L, 90L, 490L, 
278L, 129L, 55L, 63L, 12L, 230L, 85L, 290L, 149L, 37L, 163L, 
122L, 491L, 373L, 140L, 72L, 98L, 13L, 327L, 83L, 300L, 190L, 
41L, 181L, 196L, 556L, 390L, 133L, 80L, 123L, 12L, 138L, 34L, 
116L, 75L, 0L, 91L, 117L, 259L, 179L, 81L, 15L, 77L, 0L, 0L, 
74L, 240L, 161L, 24L, 124L, 196L, 463L, 261L, 116L, 13L, 83L, 
0L, 165L, 5L, 11L, 2L, 0L, 0L, 7L, 9L, 15L, 7L, 5L, 0L, 0L, 40L), 
objectid = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, NA, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, NA, 1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, NA, 1L, 2L, 3L, 4L, 
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, NA, 1L, 2L, 3L, 4L, 5L, 6L, 
7L, 8L, 9L, 10L, 11L, 12L, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 
9L, 10L, 11L, 12L, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L, NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
NA, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, NA, 1L, 
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, NA))

I add an ID column to the sf object and merge it with my count data:

uk_map$objectid <- 1:12

uk_map_data <- merge(uk_map, yearCountsAllRegions, by = "objectid")

I am able to plot a faceted version:

ggplot(data=uk_map_data)+
  geom_sf(aes(fill=n,geometry=geometry))+
  scale_fill_continuous(high = "#132B43", low = "white",
                        name="Number of\nOperations")+
  theme_void()+facet_wrap(~year)

However, if I try to animate this, I get an error:

ggplot(data=uk_map_data)+
  geom_sf(aes(fill=n,geometry=geometry))+
  scale_fill_continuous(high = "#132B43", low = "white",
                        name="Number of\nOperations")+
  theme_void()+transition_time(time = year)

Error: arguments have different crs

The same happens if I try to use transition_states:

ggplot(data=uk_map_data)+
  geom_sf(aes(fill=n,geometry=geometry))+
  scale_fill_continuous(high = "#132B43", low = "white",
                        name="Number of\nOperations")+
  theme_void()+transition_states(year)

Error: arguments have different crs

In addition: Warning messages:

1: In lapply(row_vars$states, as.integer) : NAs introduced by coercion

2: In expand_panel(..., self = self) : NAs introduced by coercion

Thank you so much in advance for your help!

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
  • 1
    Hi. Saw your post on upwrok too :) Seems that this is a recent bug in gganimate: https://github.com/thomasp85/gganimate/issues/479 – novica Mar 31 '23 at 17:12
  • Can you add the results of `sessionInfo()` to your question? I'm able to run your code without any error. – Z.Lin Apr 01 '23 at 06:35
  • R version 4.2.3 (2023-03-15 ucrt) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 22621) Matrix products: default locale: [1] LC_COLLATE=English_United Kingdom.utf8 LC_CTYPE=English_United Kingdom.utf8 LC_MONETARY=English_United Kingdom.utf8 LC_NUMERIC=C [5] LC_TIME=English_United Kingdom.utf8 attached base packages: [1] stats graphics grDevices utils datasets methods base – Jonathan Lewin Apr 01 '23 at 10:06
  • other attached packages: [1] plotly_4.10.1 gifski_1.6.6-1 tweenr_2.0.2 transformr_0.1.4 gganimate_1.0.8 rmapshaper_0.4.6 raster_3.6-20 lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0 [11] dplyr_1.1.1 purrr_1.0.1 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1 tidyverse_2.0.0 sf_1.0-12 stringdist_0.9.10 gpclib_1.5-6 plyr_1.8.8 [21] broom_1.0.4 rgeos_0.6-2 ggmap_3.0.2 ggplot2_3.4.1 rgdal_1.6-5 maptools_1.1-6 mapdata_2.3.1 maps_3.4.1 sp_1.6-0 – Jonathan Lewin Apr 01 '23 at 10:07
  • loaded via a namespace (and not attached): [1] bitops_1.0-7 progress_1.2.2 httr_1.4.5 bslib_0.4.2 tools_4.2.3 backports_1.4.1 utf8_1.2.3 R6_2.5.1 KernSmooth_2.23-20 [10] lazyeval_0.2.2 DBI_1.1.3 colorspace_2.1-0 withr_2.5.0 tidyselect_1.2.0 prettyunits_1.1.1 curl_5.0.0 compiler_4.2.3 textshaping_0.3.6 [19] cli_3.4.1 geojsonsf_2.0.3 sass_0.4.5 labeling_0.4.2 scales_1.2.1 classInt_0.4-9 proxy_0.4-27 – Jonathan Lewin Apr 01 '23 at 10:08
  • systemfonts_1.0.4 digest_0.6.31 [28] foreign_0.8-84 rmarkdown_2.21 jpeg_0.1-10 pkgconfig_2.0.3 htmltools_0.5.5 fastmap_1.1.1 jsonvalidate_1.3.2 htmlwidgets_1.6.2 rlang_1.1.0 [37] rstudioapi_0.14 httpcode_0.3.0 jquerylib_0.1.4 generics_0.1.3 farver_2.1.1 jsonlite_1.8.4 magrittr_2.0.3 Rcpp_1.0.10 munsell_0.5.0 [46] fansi_1.0.4 lifecycle_1.0.3 terra_1.7-18 stringi_1.7.12 yaml_2.3.7 jqr_1.2.3 grid_4.2.3 parallel_4.2.3 – Jonathan Lewin Apr 01 '23 at 10:08
  • geojsonio_0.11.0 [55] crayon_1.5.2 lattice_0.20-45 geojson_0.3.4 hms_1.1.3 knitr_1.42 pillar_1.9.0 geojsonlint_0.4.0 codetools_0.2-19 lpSolve_5.6.18 [64] crul_1.3 glue_1.6.2 evaluate_0.20 V8_4.2.2 data.table_1.14.8 png_0.1-8 vctrs_0.6.1 tzdb_0.3.0 RgoogleMaps_1.4.5.3 [73] gtable_0.3.3 cachem_1.0.7 xfun_0.38 e1071_1.7-13 rsconnect_0.8.29 ragg_1.2.5 viridisLite_0.4.1 class_7.3-21 units_0.8-1 [82] timechange_0.2.0 – Jonathan Lewin Apr 01 '23 at 10:08

1 Answers1

0

Try specifying the crs and datum parameters of your base map in the code. Something like this:

ggplot(data=uk_map_data)+
  geom_sf(aes(fill=n,geometry=geometry))+
  scale_fill_continuous(high = "#132B43", low = "white",
                        name="Number of\nOperations")+
  theme_void()+transition_time(time = year)+
  coord_sf(crs = st_crs(uk_map_data), datum = NA)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
jaykay
  • 71
  • 7