<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/visualising-an-ethnicity-statistical-classification-by-ellis2013nz/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://ellisp.github.io/blog/2018/02/10/ethnicity"> Peter's stats stuff - R</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Official statistical classifications can be big, complex things. For example, the complete version of the “International Standard Industrial Classification of All Economic Activities, Rev.4” comes as <a rel="nofollow" href="https://unstats.un.org/unsd/cr/registry/regdnld.asp?Lg=1" target="_blank">either a 300 page PDF or a 1.3GB Microsoft Access database</a>. Classifications typically have a hierarchy of four or more levels and hundreds or thousands of categories, each with a code, a name and a detailed definition. One feature that causes endless definition is that the <em>names</em> aren’t necessarily unique identifiers; it’s quite common for the same name to be used for nested categories at different levels of the hierarchy, particularly where going down a level in that particular direction gets you a category that has only one sub-category.</p>
<p>Statistics New Zealand’s <a rel="nofollow" href="http://archive.stats.govt.nz/methods/classifications-and-standards/classification-related-stats-standards/ethnicity.aspx" target="_blank">ethnicity classification</a> is a more or less typical example of a relatively straightforward classification. “Ethnicity” is a key but very artificial and constructed social variable, that is essential for many analyses. It appears to have a simple “common knowledge” definition but that breaks down very quickly when you start to think hard about it. It’s perhaps related to but very different from the similarly human-constructed categories of “citizenship” and “race”, with much potential for confusion. Ultimately, it comes down to self-identification (which is part of the standard for measuring it) – you are the ethnicity that you think you are, within the disciplines, constraints and opportunities created by social norms of categorisation. Nothing makes this more explicit and noticeable than the brutal, hard nosed black and white compulsion of a statistical standard! Recommended reading – <a rel="nofollow" href="https://www.goodreads.com/book/show/119561.The_Order_of_Things" target="_blank">Foucault’s The Order of Things</a>.</p>
<p>The ethnicity classification has four levels of hierarchy, with 1, 2, 3 or 5 digits in the code (there are no four digit codes because there can be more than 10 sub-categories per grouping in the fourth level, so it needs two digits). For example, code “1” means “European”, “11” means “New Zealand European”, and “111” and “11111” also mean “New Zealand European” – there are no subcategories of “New Zealand European” at the 3rd and 4th level of the hierarchy, so this forms an example of a non-unique category label. If you have data that includes the observation “New Zealand European” you don’t know which level of the classification hierarchy it is at (and hence what to compare with) unless you also have the code.</p>
<p>The term “New Zealand European” is part of both the everyday language and the official classification, but it’s <a rel="nofollow" href="http://salient.org.nz/2013/04/pakeha-or-nz-european-the-white-choice/" target="_blank">potentially divisive</a>. Certainly, it confuses me as an Australian of European descent who happens to be living in New Zealand. With the <a rel="nofollow" href="https://www.census.govt.nz/" target="_blank">New Zealand census</a> coming up I was reminded of a long outstanding “must do sometime” note to myself to better understand the official ethnicity classification, so I set out to visualise it. A network graph is ideal, and here’s what I came up with:</p>
<p><iframe width="450" src="http://ellisp.github.io/img/0117-ethnicity.html"></iframe></p>
<ul>
<li>Zoom in to read it properly – with a mouse, just hover over the frame and use the mouse scroll wheel.</li>
<li>The four levels in the hierarchy are colour-coded, with the larger text in the red boxes indicating the top level categories of five ethnic groups plus “other” and “residual categories”.</li>
<li>Move a node around to see dynamically everything it’s connected to.</li>
<li>Move the whole diagram around by clicking and dragging on some white space.</li>
<li>Hover over a category to see its code.</li>
<li>Click on an individual category to highlight what it is linked to.</li>
<li>Double-click to collapse or expand a category’s sub-categories.</li>
</ul>
<p>I quite like this as a way of exploring a hierarchical classification and think I’ll use it for others that I need to understand. In this case, it makes it easier for me to self-identify my own ethnicity as “European”, “Other European”, “Australian”, “Australian” (code 12811). That’s not how I spontaneously think of myself – but it’s definitely where I fit in this classification. Note that, importantly, the New Zealand standard requires users to self-identify with at least three different ethnicities (ie “leafs” in this tree) if they want.</p>
<p>There’s also a <a rel="nofollow" href="http://ellisp.github.io/img/0117-ethnicity-large.html" target="_blank">larger version</a> available for using a full desktop sized screen. Probably neither of them will look great on a mobile device; due to the way I’ve set up my website I’m avoiding using knitr directly to generate webpages, and this is one instance where it makes it a bit fiddlier to include a nice object like this graph; so they are just showing in iframes. There is actually a method to <a rel="nofollow" href="https://stackoverflow.com/questions/34439928/embedding-an-r-htmlwidget-into-existing-webpage" target="_blank">properly embed an htmlwidget into an existing webpage</a>, but it’s a bit of a fiddle</p>
<p>Here’s the R code that drew this. It’s straightforward, other than perhaps a couple of tricks used to identify all the “edges” connecting categories with their sub-categories. Of the various options out there for creating JavaScript network diagrams from R, I find <code>visNetwork</code> the most intuitive to use and easiest to customise.</p>
<figure>
<pre><code data-lang="r"><span>library</span><span>(</span><span>tidyverse</span><span>)</span><span>
</span><span>library</span><span>(</span><span>openxlsx</span><span>)</span><span>
</span><span>library</span><span>(</span><span>htmlwidgets</span><span>)</span><span>
</span><span>library</span><span>(</span><span>stringr</span><span>)</span><span>
</span><span>library</span><span>(</span><span>visNetwork</span><span>)</span><span>
</span><span>library</span><span>(</span><span>RColorBrewer</span><span>)</span><span>
</span><span>download.file</span><span>(</span><span>&quot;http://archive.stats.govt.nz/~/media/Statistics/surveys-and-methods/methods/class-stnd/ethnicity/ETHNIC05-v2-classification-all.xlsx&quot;</span><span>,</span><span>
</span><span>mode</span><span> </span><span>=</span><span> </span><span>&quot;wb&quot;</span><span>,</span><span> </span><span>destfile</span><span> </span><span>=</span><span> </span><span>&quot;ethnic05.xlsx&quot;</span><span>)</span><span>
</span><span>nodes</span><span> </span><span>&lt;-</span><span> </span><span>read.xlsx</span><span>(</span><span>&quot;ethnic05.xlsx&quot;</span><span>)</span><span>
</span><span>names</span><span>(</span><span>nodes</span><span>)</span><span> </span><span>&lt;-</span><span> </span><span>c</span><span>(</span><span>&quot;id&quot;</span><span>,</span><span> </span><span>&quot;label&quot;</span><span>)</span><span>
</span><span># clean up:
</span><span>unlink</span><span>(</span><span>&quot;ethnic05.xlsx&quot;</span><span>)</span><span>
</span><span># edges ie connection from one node to another
</span><span>edges</span><span> </span><span>&lt;-</span><span> </span><span>expand.grid</span><span>(</span><span>nodes</span><span>$</span><span>id</span><span>,</span><span> </span><span>nodes</span><span>$</span><span>id</span><span>,</span><span> </span><span>stringsAsFactors</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>rename</span><span>(</span><span>from</span><span> </span><span>=</span><span> </span><span>Var1</span><span>,</span><span> </span><span>to</span><span> </span><span>=</span><span> </span><span>Var2</span><span>)</span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>f_length</span><span> </span><span>=</span><span> </span><span>str_length</span><span>(</span><span>from</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>((</span><span>str_length</span><span>(</span><span>to</span><span>)</span><span> </span><span>==</span><span> </span><span>f_length</span><span> </span><span>+</span><span> </span><span>1</span><span>)</span><span> </span><span>|</span><span> </span><span>str_length</span><span>(</span><span>to</span><span>)</span><span> </span><span>==</span><span> </span><span>5</span><span> </span><span>&amp;</span><span> </span><span>f_length</span><span> </span><span>==</span><span> </span><span>3</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>filter</span><span>(</span><span>str_sub</span><span>(</span><span>to</span><span>,</span><span> </span><span>1</span><span>,</span><span> </span><span>f_length</span><span>)</span><span> </span><span>==</span><span> </span><span>from</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>select</span><span>(</span><span>from</span><span>,</span><span> </span><span>to</span><span>)</span><span> </span><span># extra columns in the nodes data frame for the visualisation
</span><span>nodes</span><span> </span><span>&lt;-</span><span> </span><span>nodes</span><span> </span><span>%>%</span><span>
</span><span>mutate</span><span>(</span><span>
</span><span># `title` is for tooltips:
</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>id</span><span>,</span><span> </span><span># `group` is used for colour, so we use it to indicate the level of the classification:
</span><span> </span><span>group</span><span> </span><span>=</span><span> </span><span>str_length</span><span>(</span><span>id</span><span>))</span><span>
</span><span># a nice, ordered colour palette:	</span><span>pal</span><span> </span><span>&lt;-</span><span> </span><span>brewer.pal</span><span>(</span><span>5</span><span>,</span><span> </span><span>&quot;YlOrRd&quot;</span><span>)[</span><span>4</span><span>:</span><span>1</span><span>]</span><span>
</span><span># draw diagram and save as a web page:
</span><span>visNetwork</span><span>(</span><span>nodes</span><span>,</span><span> </span><span>edges</span><span>,</span><span> </span><span>width</span><span> </span><span>=</span><span> </span><span>&quot;1700px&quot;</span><span>,</span><span> </span><span>height</span><span> </span><span>=</span><span> </span><span>&quot;900px&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>visPhysics</span><span>(</span><span>stabilization</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>timestep</span><span> </span><span>=</span><span> </span><span>.3</span><span>,</span><span>
</span><span>barnesHut</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>centralGravity</span><span> </span><span>=</span><span> </span><span>0.6</span><span>,</span><span>
</span><span>gravitationalConstant</span><span> </span><span>=</span><span> </span><span>-4000</span><span>,</span><span>
</span><span>springLength</span><span> </span><span>=</span><span> </span><span>80</span><span>,</span><span> </span><span>avoidOverlap</span><span> </span><span>=</span><span> </span><span>0</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>visOptions</span><span>(</span><span>highlightNearest</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>collapse</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>visGroups</span><span>(</span><span>groupname</span><span> </span><span>=</span><span> </span><span>&quot;1&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>pal</span><span>[</span><span>1</span><span>],</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>&quot;box&quot;</span><span>,</span><span> </span><span>font</span><span> </span><span>=</span><span> </span><span>list</span><span>(</span><span>color</span><span> </span><span>=</span><span> </span><span>&quot;white&quot;</span><span>,</span><span> </span><span>size</span><span> </span><span>=</span><span> </span><span>23</span><span>))</span><span> </span><span>%>%</span><span>
</span><span>visGroups</span><span>(</span><span>groupname</span><span> </span><span>=</span><span> </span><span>&quot;2&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>pal</span><span>[</span><span>2</span><span>],</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>&quot;ellipse&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>visGroups</span><span>(</span><span>groupname</span><span> </span><span>=</span><span> </span><span>&quot;3&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>pal</span><span>[</span><span>3</span><span>],</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>&quot;ellipse&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>visGroups</span><span>(</span><span>groupname</span><span> </span><span>=</span><span> </span><span>&quot;5&quot;</span><span>,</span><span> </span><span>color</span><span> </span><span>=</span><span> </span><span>pal</span><span>[</span><span>4</span><span>],</span><span> </span><span>shape</span><span> </span><span>=</span><span> </span><span>&quot;ellipse&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>visEdges</span><span>(</span><span>smooth</span><span> </span><span>=</span><span> </span><span>FALSE</span><span>,</span><span> </span><span>arrows</span><span> </span><span>=</span><span> </span><span>&quot;end&quot;</span><span>)</span><span> </span><span>%>%</span><span>
</span><span>saveWidget</span><span>(</span><span>file</span><span> </span><span>=</span><span> </span><span>&quot;0117-ethnicity-large.html&quot;</span><span>,</span><span> </span><span>selfcontained</span><span> </span><span>=</span><span> </span><span>TRUE</span><span>,</span><span> </span><span>title</span><span> </span><span>=</span><span> </span><span>&quot;Stats NZ ethnicity classification&quot;</span><span>)</span></code></pre>
</figure>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://ellisp.github.io/blog/2018/02/10/ethnicity"> Peter's stats stuff - R</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 width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/NAZOyVx78Jo" height="1"></a></div>