Creating Your Own .gifs! Part 2

This week we are going finish our Creating your own .gif fun. So last blog we were able to make a gif using ImagJ and R. Using this method has its pro's and con's. Pro's you can use ImageJ to edit your images as you see fit. Con's you have to do additional steps outside of R. So today's post we will use R and ImageMagic. These steps will allow you to create a .gif without leaving R for additional steps.  Now I saved this post for last because it it slightly more complicated to install ImageMagic. It took me several weeks searching bog post, stack flow posts, and any other tutorials to figure out how to download and run ImageMagic properly. When most people do posts on creating .gifs they leave out the importance of downloading and running ImageMagic. And even if they did it was either for Windows or Mac but never both. SO.... I will be take the time to explain how to install it for both Mac and Windows (I'm sorry Linux users I'm not there yet).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Part 2: Making a .gif using R and ImageMagic

ImageMagic has a lot more features then I am going to show you today. Feel free to explore it after we have fun creating these .gifs.

Downloading ImageMagic for a PC

Downloading ImageMagic for a PC is actually really easy thanks to a great library package called: installr

So open up R and install the installr package: 

# Install installr package    
> install.packages("installr")

Then install the ImageMagic using this line of code:

> require(installr)       
# Then install ImageMagick
> install.ImageMagick()     

Let it do its thing and follow any prompt that it may give you to update other packages. Make sure to check all the boxes for items to download. Once its installed you can move on to the: Creating Images using R. 

Downloading ImageMagic for Mac

Downloading ImageMagic is a bit more complicated. The "installr" package is a PC only library. So before we even open R we have to download ImageMagic through the Mac Terminal via a software package management system like Homebrew or MacPorts. If you have either of these go ahead and move to the ImageMagic instillation part. I decided to use Homebrew so the following instructions are how to install Homebrew. 

Start by opening the Mac Terminal:

Finder --> Go --> Utilities --> Terminal
Note make sure that you are logged in as the Administrator if applicable.

Type in the Terminal console:

/usr/bin/ruby -e "$(curl –fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Follow the prompts if it gives you any. To check if Homebrew is downloaded properly - you can test this by typing in brew doctor into the terminal console. This will pop up a summary report. Once you are satisfied that Homebrew is installed you can then download imageMagick. 

Type in the Mac Terminal console:

brew install imagemagick

Follow any prompts given. Once its complete ImageMagick will be installed. 

Creating gifs using R

So last time we talked about what a gif is and how it is created. The same code will apply here, however we will add a line of code that will take all the images that we have created and combine them into a single gif. We will use the same image from the last blog with a new colors to show how this is done. So lets get started!

The first thing I recommend, again, is to set the user directory. That way all your images will be saved to a specific folder that you created for this project. Its also smart to make a brand new folder to save all the images in.

setwd('C:/Users/Desktop/GifProject/')

Once you have your directory set, you can start creating your images.We are going to use the same code as before with a few changes: The code is below:

# Frame per second speed
Speed = 10

# this creates equal seized wedges for out chart
slices <- rep(25,20) 

# Number of frames we will need to make a smooth continuous gif (since its a circle and I rate will be 10 degrees at a time I will need 36)
frames = 36 # we need a full circle rotation so we need 36 photos
Start = 0 # Start the circle starting at 0 degrees
  
  #Loop to create and print the images
  for(i in 1:frames){ 
    # creating a name for each plot file with leading zeros
    if (i < 10) {name = paste('000',i,'plot.png',sep='')} 
    if (i < 100 && i >= 10) {name = paste('00',i,'plot.png',sep='')}
    if (i >= 100) {name = paste('0', i,'plot.png', sep='')}
    
    # Name and print the image
    png(name,width=4.5, height=4.5,units = "in",res=72)
    # Give it a black background
    op <- par(bg = "black")
    
    # pie graph
    pie(slices,labels = NA, col=c("plum3","cyan3"),
          init.angle = Start) # Plot the pie graph 


    dev.off() # Says that is the end of making the image
    
    Start = Start-10 # changes the angle of the next graph by 10 degrees going clockwise

  }
  

This will create all of the images needed to create the gif. Now we need to create the .gif. To do so you have to edit this next code based on your computer.

If you have a PC you will have to find the pathway to the ImageMagic's Convert function and use replace the purple highlighted section with that pathway. 

# Note may need to change the pathway to match where ImageMagick is located on your computer. 
> GifPath = paste('"C:/Program Files/ImageMagick-7.0.6-Q16/convert.exe" -delay ',Speed, ' *.png Circ_Speed',Speed,'.gif',sep = "")

# Run the GifPath code
>system(GifPath)

If you have a Mac you just need to copy and past the following code below:

> GifPath = paste('convert -delay ',Speed, ' *.png Circ _Speed',Speed,'.gif',sep = "")

# Run the GifPath code
>system(GifPath)

Note that the GifPath not only defines how to create the .gif  but also names the file. I have it automating the .gif image name to be Circ_Speed10.gif. 

If you don't want to have this automated you can use the following code:

> system(" 'C:/Program Files/ImageMagick-7.0.6-Q16/convert.exe' -delay 20 *.png Circ25_Speed10.gif"

or

> system("convert -delay 20 *.png Circ25_Speed10.gif"

This next part is just for cleaning up your folder but isn't totally necessary. If you want to get rid of all of the .png files that were used to create the .gif you can add this next line of code.

# Delete all the .png files so all you are left with is the gif image
> invisible(file.remove(list.files(pattern = ".png")))


When you are done you should have an .gif image that looks like the one below:




I hope these two posts are helpful for creating .gif images with your graphs!  Have fun creating!!!

Comments

Popular posts from this blog

Creating your own .gifs! Part 1

Women Who Code Connect!

Open Source Platforms ... Why aren't you using them yet?