I decided to remake the Gapminder gif that I made the other day in Python, but in Rstudio this time. I’ll probably continue doing this for a while, as I try to figure out the advantages of using one program over the other. Here’s is a walk-through of what I did to recreate it:
#install these packages if you haven't already install.packages(c("devtools", "dplyr", "ggplot2", "readr")) devtools::install_github("dgrtwo/gganimate",force=TRUE) library(devtools) library(dplyr)
library(readr) library(viridis)
library(ggplot2) library(gganimate) library(animation) #Set up ImageMagick --for gifs install.packages("installr",dependencies = TRUE) library(installr)
#Configure your environment--change the location Sys.setenv(PATH = paste("C:/Program Files/ImageMagick-7.0.7-Q16", Sys.getenv("PATH"), sep = ";")) #change the path to where you installed ImageMagick #Again, change the location: magickPath <- shortPathName("C:/Program Files/ImageMagick-7.0.7-Q16/magick.exe") #ani.options(convert=magickPath)
If you need to download ImageMagick, go to this link
Load data and create plot
Once you’ve installed the appropriate packages and configured ImageMagick to work with Rstudio, you can load your data and plot as usual.
gapminder_data<-read.csv("https://python-graph-gallery.com/wp-content/uploads/gapminderData.csv", header=TRUE)
glimpse(gapminder_data) #print to make sure it loaded correctly
## Observations: 1,704
## Variables: 6
## $ country Afghanistan, Afghanistan, Afghanistan, Afghanistan, ...
## $ year 1952, 1957, 1962, 1967, 1972, 1977, 1982, 1987, 1992...
## $ pop 8425333, 9240934, 10267083, 11537966, 13079460, 1488...
## $ continent Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia, Asia...
## $ lifeExp 28.801, 30.332, 31.997, 34.020, 36.088, 38.438, 39.8...
## $ gdpPercap 779.4453, 820.8530, 853.1007, 836.1971, 739.9811, 78...
# Helper function for string wrapping.
# Default 20 character target width.
swr = function(string, nwrap=40) {
paste(strwrap(string, width=nwrap), collapse="\n")
}
swr = Vectorize(swr)
gapminder_plot<-ggplot(gapminder_data) +
aes(x = gdpPercap,
y = lifeExp,
colour = continent,
size = pop,
frame=year) +
scale_x_log10() +
scale_size_continuous(guide =FALSE) + #suppresses the second legend (size=pop)
geom_point() +
scale_color_viridis(discrete=TRUE)+ #optional way to change colors of the plot
theme_bw() +
labs(title=swr("Relationship Between Life Expectancy and GDP per Capita"),
x= "GDP Per Capita",
y= "Life expectancy",
caption="Data: Gapminder")
theme(legend.position = "none",
axis.title.x=element_text(size=.2),
axis.title.y=element_text(size=.2),
plot.caption = element_text(size=.1))</
#getOption("device") #try running this if your plot doesn't immediately show gapminder_plot
#if you want to save the plot:
ggsave("title.png",
plot = last_plot(), # or give ggplot object name as in myPlot,
width = 5, height = 5,
units = "in", # other options c("in", "cm", "mm"),
dpi = 300)
Notice that I created the swr function to wrap the title text. If I don’t include that function, the title runs off the plot, like this:
Animate the plot
Now you can animate the plot using gganimate. Also, if you want to change any of the axis-titles or any other feature of the plot, I like to reference STHDA.
#remember to assign a working directory first:
#setwd() <--use this to change the working directory, if needed
gganimate(gapminder_plot,interval=.5,"gapminderplot.gif")
All in all, I’d say that creating the gif was equally easy in Python and R. Although I had more trouble initally configuring Python with ImageMagic–I might have found it easier in R simply because I used Python to figure this out the first time. On the other hand, I like the way the Python gif looks much more than the gif that Rstudio rendered.
Looks like I’ll have to continue experimenting.