Quantum Forest

notes in a shoebox

Mucking around with maps, schools and ethnicity in NZ

I’ve been having a conversation for a while with @kamal_hothi and @aschiff on maps, schools, census, making NZ data available, etc. This post documents some basic steps I used for creating a map on ethnic diversity in schools at the census-area-unit level. This “el quicko” version requires 3 ingredients:

  • Census area units shape files (available from Statistics New Zealand for free here).
  • School directory (directory-school-current.csv available for free here).
  • R with some spatial packages (also free).

We’ll read the school directory data, aggregate ethnicity information to calculate the Herfindahl–Hirschman Index of diversity and then plot it.

Then I moved to create a map in R, for the sake of it:

And we get a plot like this:

Ethnic diversity in schools at the Census Area Unit level (0 very diverse, 1 not diverse at all).

Ethnic diversity in schools at the Census Area Unit level (0 very diverse, 1 not diverse at all).

Just because it is Monday down under.

P.S. Using the diversity-index-census-area-unit.csv and joining it with the shapefile in QGIS one can get something prettier (I have to work on matching the color scales):

Similar map produced with point and click in QGIS.

Similar map produced with point and click in QGIS.

Map rendering is so much faster in QGIS than in R! Clearly the system has been optimized for this user case.

2 Comments

  1. Hi, I am having trouble trying to reproduce your code. Especifically, you define a “Function to calculate” and I get this error message:

    Error: unexpected ‘,’ in:
    ” h = sum(frac^2)
    hn = if(total > 1,”

    I would appreciate if you could explain to me what is this line supposed to do: hn = if(total > 1, (h – 1/total)/(1 – 1/total), 1)

    Thanks.

    • Luis

      2015/04/07 at 11:54 am

      Hi Adriana,

      The code was seriously mangled by a problem with a WordPress plugin; I have cleaned the code and it should now run. Thanks for letting me know of the problem. That line was supposed to calculate and index when total was greater than 1 or just use 1 as the index.

Leave a Reply

© 2017 Quantum Forest

Theme by Anders NorenUp ↑