I've made a collection of histograms in ggplot2 and used ImageMagick to animate them into a gif.
The idea is that the x-axis scale is supposed to be the same in all the graphs but this isn't quite true because the y-axis wiggles around due to the varying widths of the labels. How can I anchor the graph so that all the graphs have exactly the same axis positions?
Here's my ggplot code if it helps:
hist.fn<-function(tier,ddf){
df<-ddf[ddf$tier==tier,]
l<-match(tier,levels(df$tier))
hist.png<-ggplot(df,aes(df$"Premium Adult Individual Age 40" ))+
geom_histogram()+
labs(title=paste0(tier," premiums in federal exchanges"),
x ="Premium", y = "Frequency")+
coord_cartesian(xlim=c(0, 1500))+
theme_bw()+
theme(text = element_text(size=14), plot.title = element_text(face="bold"),axis.title.x =element_text(face="bold"),axis.title.y =element_text(face="bold"))
file=paste0(l,"hist.jpg")
ggsave(filename=file, plot=hist.png, width=13, height=8, dpi=50)
return(hist.png)
}
data.df$tier%>% levels %>% lapply(FUN=hist.fn,ddf=data.df) ->histograms.of.tiers
system("magick -delay 75 *hist.jpg hist.gif")