Skip to content

Mapping Australian electoral divisions with ggplot2

August 18, 2013

I’ve seen some creative visualisations of issues surrounding the Australian election recently though not as many maps as I expected. ‘ggplot2’ is the go-to package for plotting in R so I thought I’d see if I could plot the Australian electoral divisions with ggplot2. By using the Australian Electoral Commission’s GIS mapping coordinates and mutilating Hadley Whickam’s tutorial it was a pretty easy process.

1. Download the AEC boundary GIS data (warning 24mb).

2. Extract the file to your R working directory.

3. Run code…

The data.frame this process creates has 2.5m observations so mapping can take a while. I’m sure there are much more effective ways to map GIS data but I wanted to stick to ggplot2 in this instance.

require("rgdal") # requires sp, will use proj.4 if installed

#I upped my memory limit as the file we are going to map is pretty large

australia = readOGR(dsn=".", layer="COM20111216_ELB_region")
australia@data$id = rownames(australia@data)
#This step isn't in the tutorial, need to do this due to a couple of errors in the AEC GIS data.
australia.buffered = gBuffer(australia, width=0, byid=TRUE)
australia.points = fortify(australia.buffered, region="id")
australia.df = join(australia.points, australia@data, by="id")

#This will show you the variables in the dataset

ggplot(australia.df) +
#Don't want a legend with 150 variables so suppress the legend
geom_polygon(show_guide = FALSE ) +
  geom_path(color="white") +
  #for some reason it maps too much ocean so limit coords (EDIT: due to Christmas Island)

This gives you


While it’s a nice picture, it’s of little use as it is impossible to see small electorates.

State by state mapping. might be more useful Here is some code to map the ACT. I suggest anyone experimenting should play around with mapping the ACT data as it doesn’t take long to process.

ggplot(subset(australia.df, STATE == "ACT")) +
  geom_polygon() +
  geom_path(color="white") +
  #include limits to remove Jervis bay plotting

Which gives:

To include your own data for mapping just add it to the australia@data data.frame, merging by australia@data$ELECT_DIV. The charts look good, but to make them really eye-catching I suggest you take them into inkscape.


From → R

  1. Richard permalink

    I’m very interesting in what you are showing here. I have data by (electoral division) which I would like to show as a heat map (i.e. fill = VARIABLE1). My csv file has two fileds: ELECT_DIV and VARIABLE1. Could you show which commands I need to link this file with australia@data (and australia.df) so ggplot can use values from VARIABLE1?


    • australia@data$ELECT_DIV contains the names of the electoral divisions. Just add your data to australia@data by joining on that.

  2. Richard permalink

    just reading my comment above… it should say “interested”!

  3. We had a go at this recently and put a package on CRAN that includes a gg object for the electoral boundaries (looks just like yours!). R also have a function for the dorling algo to help with the tiny clustered electorates in the cities. More detail in the vignette here:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: