blakeshaffer / polar_unemployment_animated_chart

Polar animated chart of unemployment rates by Canadian census metropolitan areas (CMA)
0 stars 0 forks source link

gganimate does not animate #1

Open PhilSmith26 opened 6 years ago

PhilSmith26 commented 6 years ago

Blake, I tried to reproduce the animated unemployment chart using your code. It worked without any hitches up to and including the g<-ggplot line of code. But I could not get the last line, gganimate to work. The error messages I got were:

sh: convert: command not found Error in cmd.fun(sprintf("%s --version", convert), intern = TRUE) : error in running command I cannot find ImageMagick with convert = 'convert' Error in file(file, "rb") : cannot open the connection In addition: Warning messages: 1: In im.convert(img.files, output = path.expand(movie.name), convert = convert, : Please install ImageMagick first or put its bin path into the system PATH variable 2: In file(file, "rb") : cannot open file 'unemployment_polar_gg.gif': No such file or directory

I checked to see if the ggplot line worked by plotting g and that worked (without the animation). I tried installing some additional packages - magick and yihui/animation - but that did not help either. Any idea what I am missing here?

Philip

blakeshaffer commented 6 years ago

Hi Philip,

animation can be finicky for me at times as well. My first guess is it has to do with installing ImageMagick on your computer. I use a Mac and installed is from here: http://cactuslab.com/imagemagick/

If that fails, a crude (but effective) way to make the animations is use a loop to create a series of plots saved with a sequential filename in a separate folder, than aggregate them using gifmaker.me. It sounds a bit hokey, but it works! I've used it in the past when I too had issues with imagemagick.

For this particular animation, I had made the code to do just that. It's pasted below:

### If the gganimate method doesn't work you can use the following method
### Below I create a plot function that takes the frame variable (date) as a variable
### Then loop through all the year-month combinations in the data, saving a .png file each time
### I could wrap that code with saveGIF{} but I've had trouble with that function
### So instead i dump all the .png files into a temp directory and then use gifcreator.me (online) to create the gif (easy to do)

### First, perform the master_code, stopping at the gganimate plot

### Function to create ggplot for each date ###
library(scales) #for oob squish
plotg<-function(d){
  plotdata <- data %>% filter(date==d)
  ggplot(plotdata,aes(x=GEO,y=Value,frame=fct_date))+
    geom_bar(aes(fill=prov),stat="identity",alpha=.7)+
    geom_text(aes(label=Value),color="grey20",hjust=0.5,size=3)+
    geom_label(aes(x=0,y=0,label=paste0(year,"-",month)),size=4)+
    scale_y_continuous(limits=c(0,15),oob=squish)+
    coord_polar()+
    labs(title="Unemployment rate by CMA",
         y="",
         x="",
         caption="Source: CANSIM 282-0135\nChart by @bcshaffer")+
    theme(text=element_text(family="Avenir"),
          panel.grid.major.y=element_blank(),
          axis.text.y=element_blank(),
          axis.text.x=element_text(size=8),
          axis.ticks.y=element_blank(),
          plot.caption = element_text(size=rel(0.7),face="italic"),
          plot.background = element_rect(
            fill = "grey92",
            colour = "black",
            size = 1
          ))
}
### Create loop over dates
### First, create a folder to dump all the .png files into
setwd("~/Dropbox/Charts and Data/temp/")

for (y in 2002:2017){   #or whatever years you need to loop over
for (m in 1:12){
  d<-paste0(y,"-",ifelse(m<10,"0",""),m,"-01")  #create a date variable by pasting the Y-M-01
  plotg(d)
  ggsave(paste0(y,ifelse(m<10,"0",""),m,".png"),width=8,height=6.5)  #save each file as YYMM.png into the temp folder set above
}
}

### Now go to gifcreator.me, upload all the files in the above folder
### Scale the .png files down to 70% within gifcreator.me, if desired
### Set the interval to 200ms
### Make the final frame interval 5000ms to create a pause