Tag Archives: mapping

Using a Treemap to show gauged areas

According to Wikipedia a Treemap is a method for displaying hierarchical data using nested rectangles.  Its easier to explain with an example which also shows how Treemaps are useful in hydrology.

In this post we’ll use a Treemap to provide information on the catchment of Western Port, a bay to the east of Melbourne; in particular, the proportion of the catchment that is gauged.  By the way, according to the Vicnames database, the official name is Western Port, not Western Port Bay.

There are 4 main gauged tributaries draining to Western Port:

  • Cardina Creek
  • Bunyip River
  • Lang Lang River
  • Bass River

Figure 1 shows the gauged portions of these catchments overlaid on a shaded relief base map.


Figure 1: Gauged catchments draining to Western Port

We can also plot the the gauged and ungaged portions of these catchments on a treemap along with the remainder of the Western Port Catchment (Figure 2).  Or just the gauged portions compared to the rest (Figure 3).


Figure 2: Gauged and ungauged portions of the 4 catchments


Figure 3: Gauged portions of the four catchments compared with the rest of the Western Port Catchments

If we are going to estimate flow or pollution loads to Western Port than we need to factor up the results from the gauges.  A treemap shows how much extrapolation is necessary.  Code to produce these treemaps is available as a gist.


Data cleaning: latitude and longitude

When cleaning a dataset you need to check everything.  I’m trying to make sense of the extreme storms archive provided by the Bureau of Meteorology.  Most of the records are fine but there are some errors in latitude and longitude (Figure 1).

Australian towns like Cobram, Rutherglen and Wodonga (the bottom rows in Figure 1) are all in the southern hemisphere so, by convention the latitudes are negative.    Similarly, the whole of Australia is well south of the equator so latitudes of zero of -1 are not correct.  The numbers, -1, 0 and 1 seem to be sentinel values that indicate missing so need to be recoded.


Figure 1: Errors in latitude and longitude

Wikipedia lists the extreme points of Australia.  Those for the mainland plus Tasmania are:

  • Northernmost point – Cape York Peninsula, Qld -10o41′
  • Southernmost point – South East Cape, Tas -43o39′
  • Westernmost point – Steep Point, WA 113o09′ E
  • Easternmost point – Cape Byron, NSW 153o38’E

Points outside this range may refer to islands, or be in error.  Best to check.

Latitudes and longitudes can be looked up for a locality using the geocode function in the ggmap package in R.  We can also go from coordinates to locality using revgeocode.

geocode('Rutherglen, Australia')
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=Rutherglen,%20Australia&sensor=false
lon lat
1 146.4625 -36.05556

# Which state is Rutherglen in?

x <- geocode('Rutherglen, Australia', output = 'all')
[1] "Victoria"
[1] "VIC"

# From coordinates to locality

revgeocode(c(146.4625, -36.05556))
# [1] "62 Main St, Rutherglen VIC 3685, Australia"

# Which state are these coordinates in?

x <- revgeocode(c(146.4625, -36.05556), output = 'all')

#[1] "Victoria"

#[1] "VIC"

# Alternatively
x <- geocode('Rutherglen, Australia', output = 'more')
# Victoria

Some manual checking will be required but a lot can be automated which will avoid many gross errors.

Boggy Ck @ Angleside

Mapping gauging stations in R

If you have the location (latitude and longitude) of a gauging station or a rain gauge, it’s nice to make a map for a report or just to confirm your sites are where you think they are.

In R we can link to google maps, extract areas of interest and then use ggplot to add points and annotations.  The example below shows how to plot the site of a gauge in north east Victoria; Boggy Creek at Angleside.

Try out different zoom arguments in the ggmap function and different maptypes.  Google provides 4 maptypes:

  • satellite
  • terrain
  • roadmap
  • hybrid

It is also possible to obtain maps from Open Street Map, set source = ‘osm’.

For further examples visit:


loc <- c(lon = 146.362, lat = -36.6063 )

my.map <- ggmap(get_map(loc, zoom = 12, source = "google", maptype = 'hybrid')) 
my.map + geom_point(data = as_data_frame(as.list(loc)), 
                    aes(lon, lat), size = 3, color='red') +
         xlab("") + 
         ylab("") +
         geom_text(data = as_data_frame(as.list(loc)), aes(lon, lat), 
            label = ' Boggy Creek @ Angleside',
            color = 'red',
            size = 4,
            hjust = 1.05, 
            vjust = -0.7 )