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!