Create animated gif for multiple cities.
authorFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Mon, 6 Nov 2017 12:54:00 +0000 (20:54 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.biz>
Mon, 6 Nov 2017 12:54:00 +0000 (20:54 +0800)
AU-taxstats.R

index 76f9dcf00d2fc8f5bb644727e8e6aec73bb8e27a..8bbcb263b533e828f8b023e61505cdc8f11207fa 100644 (file)
@@ -5,6 +5,7 @@ devtools::install_github("tidyverse/ggplot2") # needed for geom_sf
 library(ggplot2)
 library(viridis)
 library(ggthemes)
+library(animation) # for saveGIF
 
 # Obtain the tax dataset if not available yet
 if(!file.exists("data/taxstats2015individual06ataxablestatusstateterritorypostcode.csv")) 
@@ -56,17 +57,27 @@ sa3$incomeperearningcapita <- (sa3$TotalIncome / sa3$TotalIncomeEarners)/1000
 # As SA3s are still to narrow around cities compared to in the country,
 # let's simply look at Melbourne
 
-ggplot(dplyr::filter(sa3, data.table::`%like%`(GCC_NAME16, "Melbourne") )) +
+cities = c("Perth","Melbourne","Sydney","Adelaide","Brisbane")
+
+# Create a plot for each of these cities. This is wrapped in a function
+# for use by saveGIF
+
+plots <- function() {lapply(cities, function(x){
+ plot <- ggplot(dplyr::filter(sa3, data.table::`%like%`(GCC_NAME16, x) )) +
   geom_sf(aes(fill = incomeperearningcapita, color = incomeperearningcapita)) +
   scale_fill_viridis(name = "") +
   scale_color_viridis(name = "") +
   coord_sf(datum = NA) + # Work around https://github.com/tidyverse/ggplot2/issues/2071 to remove gridlines
-  labs(title = "Melbourne \nincome distribution",
+  labs(title = paste0(x," \nincome distribution"),
        subtitle = "2014/15, in 1000s AUD",
        caption = "\nSource: Australian Taxation Office") +
   theme_economist() +
   theme(legend.position = "bottom",
         legend.text = element_text(angle = 45, hjust = 1, size = 8),
         axis.text = element_blank(),
-        axis.ticks = element_blank()) 
-  
+        axis.ticks = element_blank())
+ print(plot)
+})
+}
+
+saveGIF(plots(),movie.name = "AUCitiesIncomeDistribution.gif", interval = 2)