<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/"> R – rud.is</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The CBC covered the recent (as of the original post-time on this blog entry) <a rel="nofollow" href="https://www.cbc.ca/news/canada/montreal/8-maps-that-explain-quebec-s-surprise-election-results-1.4848794" target="_blank">Quebec elections</a> and used a well-crafted hex grid map to display results:</p>
<p><a rel="nofollow" href="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/quebec-hexagon-electoral-map-ridings/" target="_blank"><img data-orig-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/quebec-hexagon-electoral-map-ridings.jpg?fit=780%2C733&amp;ssl=1" data-orig-size="780,733" data-medium-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/quebec-hexagon-electoral-map-ridings.jpg?fit=300%2C282&amp;ssl=1" data-attachment-id="11613" data-image-meta="{" data-image-description="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/quebec-hexagon-electoral-map-ridings.jpg?resize=780%2C733&ssl=1" data-permalink="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/quebec-hexagon-electoral-map-ridings/" data-large-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/quebec-hexagon-electoral-map-ridings.jpg?fit=530%2C498&amp;ssl=1" alt="" data-image-title="quebec-hexagon-electoral-map-ridings" width="450" data-comments-opened="1" class="wp-image-11613" data-recalc-dims="1"></a></p>
<p>They have a <a rel="nofollow" href="https://ici.radio-canada.ca/special/2018/elections-quebec/circonscriptions-portrait-cartes-vote-analyse-politique/en" target="_blank">great ‘splainer</a> on why they use this type of map.</p>
<p>Thinking that it may be useful for others, I used a browser Developer Tools inspector to yank out the javascript-created SVG and wrangled out the hexes using <a rel="nofollow" href="https://www.npmjs.com/package/svg2geojson" target="_blank"><code>svg2geojson</code><img alt="🔗" src="https://i0.wp.com/s.w.org/images/core/emoji/11/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a> and put them into a <a rel="nofollow" href="https://gitlab.com/hrbrmstr/quebec-hex-ridings" target="_blank">GeoJSON file</a> along with some metadata that I extracted from the minified javascript from the CBC’s site and turned into a data frame using the <a rel="nofollow" href="https://cran.r-project.org/web/packages/V8/index.html" target="_blank"><code>V8</code><img alt="🔗" src="https://i0.wp.com/s.w.org/images/core/emoji/11/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a> package. Since most of the aforementioned work was mouse clicking and ~8 (disjointed) lines of accompanying super-basic R code, there’s not really much to show wrangling-wise<sup>1</sup>, but I <em>can</em> show an example of using the new GeoJSON file in R and the <a rel="nofollow" href="https://cran.r-project.org/web/packages/sf/index.html" target="_blank"><code>sf</code><img alt="🔗" src="https://i0.wp.com/s.w.org/images/core/emoji/11/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a> package:</p>
<pre><code>library(sf)
library(ggplot2)

# get the GeoJSON file from: https://gitlab.com/hrbrmstr/quebec-hex-ridings or https://github.com/hrbrmstr/quebec-hex-ridings
sf::st_read(&quot;quebec-ridings.geojson&quot;, quiet = TRUE, stringsAsFactors = FALSE) %>% 
  ggplot() +
  geom_sf(aes(fill = regionname)) +
  coord_sf(datum = NA) +
  ggthemes::scale_fill_tableau(name = NULL, &quot;Tableau 20&quot;) +
  ggthemes::theme_map() +
  theme(legend.position = &quot;bottom&quot;)
</code></pre>
<p><a rel="nofollow" href="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/ggplot-sf/" target="_blank"><img data-orig-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/ggplot-sf.png?fit=2400%2C1800&amp;ssl=1" data-orig-size="2400,1800" data-medium-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/ggplot-sf.png?fit=300%2C225&amp;ssl=1" data-attachment-id="11614" data-image-meta="{" data-image-description="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/ggplot-sf.png?resize=780%2C585&ssl=1" data-permalink="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/ggplot-sf/" data-large-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/ggplot-sf.png?fit=530%2C398&amp;ssl=1" alt="" data-image-title="ggplot-sf" width="450" data-comments-opened="1" class="wp-image-11614" data-recalc-dims="1"></a></p>
<p>And, with a <em>little more</em> ggplot2-tweaking and some <code>magick</code>, we can even put it in the CBC-styled border:</p>
<pre><code>library(sf)
library(magick)
library(ggplot2)

plt &lt;- image_graph(1488, 1191, bg = &quot;white&quot;)
sf::st_read(&quot;quebec-ridings.geojson&quot;, quiet=TRUE, stringsAsFactors=FALSE) %>% 
  ggplot() +
  geom_sf(aes(fill=regionname)) +
  coord_sf(datum=NA) +
  scale_x_continuous(expand=c(0,2)) +
  scale_y_continuous(expand=c(0,0)) +
  ggthemes::theme_map() +
  theme(plot.margin = margin(t=150)) +
  theme(legend.position = &quot;none&quot;)
dev.off()

# get this bkgrnd img from the repo
image_composite(plt, image_read(&quot;imgs/background.png&quot;)) %>% 
  image_write(&quot;imgs/composite-map.png&quot;)
</code></pre>
<p><a rel="nofollow" href="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/composite-map/" target="_blank"><img data-orig-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/composite-map.png?fit=1488%2C1191&amp;ssl=1" data-orig-size="1488,1191" data-medium-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/composite-map.png?fit=300%2C240&amp;ssl=1" data-attachment-id="11615" data-image-meta="{" data-image-description="" src="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/composite-map.png?resize=780%2C624&ssl=1" data-permalink="https://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/composite-map/" data-large-file="https://i2.wp.com/rud.is/b/wp-content/uploads/2018/10/composite-map.png?fit=530%2C424&amp;ssl=1" alt="" data-image-title="composite-map" width="450" data-comments-opened="1" class="wp-image-11615" data-recalc-dims="1"></a></p>
<p>You can tweak the border color with <a rel="nofollow" href="https://cran.r-project.org/web/packages/magick/index.html" target="_blank"><code>magick</code><img alt="🔗" src="https://i0.wp.com/s.w.org/images/core/emoji/11/72x72/1f517.png?w=456&ssl=1" class="wp-smiley" data-recalc-dims="1"></a> as needed and there’s a <code>background2.png</code> in the <code>imgs</code> directory in the repo that has the white inset that you can further composite as needed.</p>
<p>With a teensy bit of work you should be able adjust the stroke color via <code>aes()</code> to separate things as the CBC did.</p>
<h3>FIN</h3>
<p>It’s important to re-state that the CBC made the original polygons for the hexes (well, they made a set of grid points and open source software turned it into a set of SVG paths) and the background images. All I did was some extra bit of wrangling and conversionating<sup>2</sup>.</p>
<p><span><sup>1</sup> I can toss a screencast if there’s sufficient interest.</span><br>
<span><sup>2</sup> Totally not a word.</span></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://rud.is/b/2018/10/10/geojson-version-of-cbc-quebec-ridings-hex-cartograms-with-example-usage-in-r/"> R – rud.is</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/a3M4U49LrYk" height="1"></a></div>