<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/animated-barplot-and-google-map-with-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://tomaztsql.wordpress.com/2017/01/13/animated-barplot-and-google-map-with-r/"> R – TomazTsql</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>It might happen that you will need a animated graph of any kind. For purposes of plotting demographic data and changes through the years, <a target="_blank" rel="nofollow" href="https://maps.google.com/">Google Maps</a> and plotting maps, merging and converting jpg files into a animated gif, sure will give a nice visualization effect.</p>
<p>Here is a sample of changes over the time period of three years on some dataset of <a target="_blank" rel="nofollow" href="https://www.visitljubljana.com/en/visitors/">my home town</a> and graph can tell little bit more as numbers <img alt="🙂" class="wp-smiley" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p><img data-image-description="" data-orig-file="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/ggmap_anim.gif?w=456&amp;ssl=1" data-orig-size="480,480" data-image-title="ggmap_anim" class="wp-image-3347" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="3347" data-permalink="https://tomaztsql.wordpress.com/2017/01/13/animated-barplot-and-google-map-with-r/ggmap_anim/#main" alt="ggmap_anim" data-medium-file="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/ggmap_anim.gif?w=456&amp;ssl=1?w=300" data-large-file="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/ggmap_anim.gif?w=456&amp;ssl=1?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/ggmap_anim.gif?w=456&ssl=1"></p>
<p>So besides plotting visualizations based on geographical data, additional plots available in any of R packages can be visualized in a similar way.</p>
<p>Playing and goofing around, I created a sample dataset:</p>
<pre># dataset
d &lt;- data.frame(val=c(2,3,4,3,4,3,4,3,2,1), 
                year=c(2015,2016,2017,2015,2016,2015,2015,2016,2017,2015)
                )</pre>
<p>Once that, is created, a function would do the trick:</p>
<pre>setwd(&quot;C:/DataTK&quot;)
library(ggplot2)
library(dplyr)

plot.bar.2 &lt;- function(df_plot, xvar, yvar, fill)
{
  require(ggplot2)
  require(dplyr)
  attach(df_plot)
  #number of steps - based on values in X-axis
  x_unique &lt;- unique(df_plot$xvar)   #xvar = year
  nof_steps &lt;- as.integer(length(df_plot$x_unique))

  for (i in 1:nof_steps) 
  {
    x &lt;- as.integer(x_unique[i])
    
    d1 &lt;- df_plot %>%
          arrange(xvar) %>%
          filter(xvar&lt;=x) %>%
          mutate(new_val = val) %>%
          select(xvar, new_val)
    
    d2 &lt;- df_plot %>%
          arrange(xvar) %>%
          filter(xvar>x) %>%
          mutate(new_val = 0) %>%
          select(xvar,new_val)
    
    dfinal &lt;- union_all(d1, d2)
    dfinal &lt;- data.frame(dfinal)
    colnames(dfinal)[1] &lt;- &quot;x&quot;
    colnames(dfinal)[2] &lt;- &quot;y&quot;
    
    name &lt;- paste('barplot00',i,'.png',sep=&quot;&quot;)
    png(name)
    ggplot(data=dfinal, aes(x=x, y=y, fill=x)) + geom_bar(stat=&quot;identity&quot;) + 
        guides(fill=FALSE)
    dev.off()
    rm(d1,d2,x,dfinal) 
  }

  system(&quot;magick -delay 150 -loop 0 *.png GeomBar_plot.gif&quot;)
  file.remove(list.files(pattern=&quot;.png&quot;))
  rm(x_unique, nof_steps)   
  detach(df_plot)
}

plot.bar.2(d, d$year, d$val, d$year)</pre>
<p> </p>
<p>A bit more “interactive” or animated graph is created.</p>
<p><img data-image-description="" data-orig-file="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.gif?w=456&amp;ssl=1" data-orig-size="480,480" data-image-title="barplot" data-comments-opened="1" class="wp-image-3341" data-recalc-dims="1" data-attachment-id="3341" data-permalink="https://tomaztsql.wordpress.com/2017/01/13/animated-barplot-and-google-map-with-r/barplot/#main" alt="barplot" data-medium-file="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.gif?w=456&amp;ssl=1?w=300" data-large-file="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.gif?w=456&amp;ssl=1?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i0.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.gif?w=456&ssl=1"></p>
<p>In comparison to static graph:</p>
<p><img sizes="(max-width: 480px) 100vw, 480px" data-image-description="" data-orig-file="https://i1.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.png?w=456&amp;ssl=1" data-orig-size="480,480" data-image-title="barplot" data-comments-opened="1" class="wp-image-3371" data-recalc-dims="1" data-attachment-id="3371" data-permalink="https://tomaztsql.wordpress.com/2017/01/13/animated-barplot-and-google-map-with-r/barplot-2/#main" alt="barplot.png" data-medium-file="https://i1.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.png?w=456&amp;ssl=1?w=300" data-large-file="https://i1.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.png?w=456&amp;ssl=1?w=480" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i1.wp.com/tomaztsql.files.wordpress.com/2017/01/barplot.png?w=456&ssl=1"></p>
<p>In addition to R code, the <a target="_blank" rel="nofollow" href="https://www.imagemagick.org/script/index.php">ImageMagic </a>program needs to be installed on your machine, as well. Also the speed, quality and many other parameters can be set, when creating animated gif.</p>
<p>Animated gif can be also included into your SSRS report, your Sharepoint site or any other site – like my blog <img alt="🙂" class="wp-smiley" src="https://i0.wp.com/s0.wp.com/wp-content/mu-plugins/wpcom-smileys/twemoji/2/72x72/1f642.png?w=456&ssl=1" data-recalc-dims="1"> and it will stay interactive. In Power BI, importing animated gif as a picture, unfortunately will not work.</p>
<p>As always, code is available at <a target="_blank" rel="nofollow" href="https://github.com/tomaztk/Animated_Barplot_with_R">GitHub</a>.</p>
<p>Happy R-coding!</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/tomaztsql.wordpress.com/3338/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/tomaztsql.wordpress.com/3338/" border="0"></a> <img alt="" src="https://i1.wp.com/pixel.wp.com/b.gif?resize=1%2C1&ssl=1" border="0" data-recalc-dims="1"></p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://tomaztsql.wordpress.com/2017/01/13/animated-barplot-and-google-map-with-r/"> R – TomazTsql</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/9NSYMYg_vPY"></a></div>