Amy Whitehead's Research

the ecological musings of a conservation biologist


Converting shapefiles to rasters in R

I’ve been doing a lot of analyses recently that need rasters representing features in the landscape. In most cases, these data have been supplied as shapefiles, so I needed to quickly extract parts of a shapefile dataset and convert them to a raster in a standardised format. Preferably with as little repetitive coding as possible. So I created a simple and relatively flexible function to do the job for me.

The function requires two main input files: the shapefile (shp) that you want to convert and a raster that represents the background area (mask.raster), with your desired extent and resolution. The value of the background raster should be set to a constant value that will represent the absence of the data in the shapefile (I typically use zero).

The function steps through the following:

  1. Optional: If shp is not in the same projection as the mask.raster, set the current projection (proj.from) and then transform the shapefile to the new projection ( using transform=TRUE.
  2. Convert shp to a raster based on the specifications of mask.raster (i.e. same extent and resolution).
  3. Set the value of the cells of the raster that represent the polygon to the desired value.
  4. Merge the raster with mask.raster, so that the background values are equal to the value of mask.raster.
  5. Export as a tiff file in the working directory with the label specified in the function call.
  6. If desired, plot the new raster using map=TRUE.
  7. Return as an object in the global R environment.

The function is relatively quick, although is somewhat dependant on how complicated your shapefile is. The more individual polygons that need to filtered through and extracted, the longer it will take. Continue reading


1 Comment

Wildlife Wednesday: Nesting Adélie penguins

Unlike their cousins the emperor penguin, and despite what most people expect, Adélie penguins do not nest on the ice. In fact, they need ice-free areas of gravel to breed on over the summer months. This is largely because they build nests of small stones in which to lay their eggs. Stones are a valuable resource in a penguin colony and it’s not uncommon to see birds stealing stones from their neighbour’s nest when they think no one is looking. This stealing behaviour means that the nests are often very evenly-spaced, at a distance that is slightly beyond the reach of the neighbour.

Occasionally someone will branch out into alternative building materials. For some reason, this bird decided that the dessicated remains of dead penguin chicks are better than stones. Unfortunately, the ladies didn’t agree and he wasn’t successful in finding a mate.  Sometimes it doesn’t pay to be different!

1 Comment

Wildlife Wednesday: Adélie penguins

I know I’ve featured Adélie penguins on Wildlife Wednesday before.  But it’s been a while and I have seen quite a few of them since last time. So over the next few weeks, I’m going to feature a series of Adélie penguin photographs that highlight different aspects of their natural history.  Please forgive me my penguin indulgences!

Adélie penguins at Cape Bird feed mostly on krill and silverfish, diving down to over 100 m to catch their prey. They have backwards pointing “spines” on the roof of their mouths and their tongues to stop the prey items escaping once they have managed to chase them down.


Remotely deleting files from R

Sometimes programs generate a LOT of files while running scripts. Usually these are important (why else would you be running the script?). However, sometimes scripts generate mountains of temporary files to create summary outputs that aren’t really useful in their own right. Manually deleting such temporary files can be a very time consuming and tedious process, particularly if they are mixed in with the important ones. Not to mention the risk of accidentally deleting things you need because you’ve got bored and your mind has wandered off to more exciting things… watching orca swim past from the hut window

…like watching orca swim past the hut window!

I had exactly this problem a few months ago when I had ~65,000 temp files from a modelling process that were no longer needed, inconveniently mixed in with the things I needed to keep. Clearly deleting these files manually wasn’t really going to be an option. There are a number of ways to tackle this problem but R provided a simple two-line solution to the problem. Continue reading


Wildlife Wednesday: Antarctica

Now I realise that Antarctica technically isn’t wildlife. But it’s full of wildlife, albeit of a somewhat limited variety (we saw a total of 10 species of vertebrates, plus a couple of crustaceans and some algae in two months). And it only seems fair that I share some of that wildlife with you. I haven’t had time to properly edit the ~65GB of photos we took at Cape Bird this year, so here is a photo for every one of the 56 days we were in Antarctica. I hope you’ll forgive me the odd non-wildlife photo!  Hovering over a photo will give you some inane caption, while you can click on one and enter slideshow mode for a better view.  Enjoy!

More southern stories will follow soon….

Leave a comment


This is just a super quick post to say bye for now & have a great (antipodean) summer. Yesterday I flew south from New Zealand to Ross Island in Antarctica to begin an eight week fieldtrip to monitor Adelie penguins.  This is the fourth year I’ve been luckly enough to be involved in this research and it’s always a hectic but exciting time of year.

We don’t have internet out at Cape Bird this year (as we shouldn’t at a remote field camp on a remote island at the bottom of the world), which means I can’t keep you posted with what we are up to.  However, it will mean that I have plenty of time to hone my blog posts for when we get back in early February, so expect a flurry of photos and posts then.  In the meantime, you can find out more about the work that we are doing and meet some of the wildlife out at Cape Bird.

Adélie penguin

Adélie penguin



Weddell seal

Weddell seal

Crabeater seal

Crabeater seal

Leopard seal

Leopard seal



I’ll be back in 2014 – have a fabulous christmas & new year!

1 Comment

New paper: Removal of livestock alters native plant and invasive mammal communities in a dry grassland–shrubland ecosystem

A common mechanism for establishing new areas for conservation is to remove potential threatening processes and then let nature take over, with the assumption that the ecosystem will revert back to a desirable state. For example, we may put up a fence to exclude undesirable species from a new reserve. We assume (& hope) that removing these species will lead to an increase in native species and ultimately benefit conservation. But is that really the case? Or will our new reserve become overrun with other undesirable species, such as exotic weeds or invasive mammals, and have negative consequences for conservation? We recently published a paper in Biological Invasions that investigated this question in the high country regions of New Zealand.

In New Zealand, much of the high country pastoral land has been leased to sheep and cattle farmers on long-term leases, covering ~27% of the total land area. However, a process of tenure review began in 1998 to evaluate the leasehold tenure of some of these properties. In some cases, areas of land was set aside for conservation purposes and the livestock removed. We were interested in what happens to the vegetation and invasive mammal communities after the removal of livestock. Do we end up with a native-dominated ecosystem that enhances conservation values? Or will these formerly grazed paddocks become weedy and full of invasive mammals that will need significant management and end up being a burden on the already limited conservation dollar? Is the vegetation really “greener” on the other side of the fence?


We know that sheep and cattle affect native vegetation in the high country by browsing or trampling, which can reduce seedling recruitment and increase the abundance of exotic plants. It is often assumed that removing livestock will reverse such processes, leading to the recovery of native biodiversity. However, plant communities are complex and the removal of grazing pressures may result in unexpected changes to community diversity and structure. For instance, a dense sward of exotic grasses may form after the removal of livestock if such grasses are more competitive than native shrubs. Sites retired from grazing may also be more attractive to invasive mammals, requiring more active management to achieve positive conservation outcomes. The variable nature of community responses to livestock removal make it difficult for conservation managers and policymakers to plan for the long-term impacts of a change from pastoral to conservation land. To manage former pastoral lease land for conservation, it is important therefore that we clearly identify the potential responses of native communities to livestock removal, and the mechanisms that drive these changes.

So we set out to investigate the impacts of livestock removal on mid-altitude dry grassland-shrubland communities, by comparing the presence and abundance of plant and invasive mammal species on currently grazed sites with that on conservation sites where pastoralism ceased 10–40 years ago. Areas were chosen on four properties in the eastern South Island of New Zealand where paired pastoral and conservation sites were separated by fences.

Removal of livestock had little impact on the total number of plant species present on either side of the fence. However, the composition and structure of these plant communities differed significantly . Sites on conservation land had higher native biodiversity, with small native herbs, grasses and shrubs more abundant than on the adjacent pastoral sites. Sites on pastoral land were dominated by exotic plants, particularly herbs and grasses. Exotic grasses had a negative impact on native biodiversity on both sides of the fence but the effect was stronger on pastoral land. Exotic hawkweeds (Hieracium/Pilosella) were equally abundant on both pastoral and conservation land, while native shrubs were more abundant than exotic shrubs on conservation land. These changes indicate that the study sites are undergoing successional changes towards a native-shrub-dominated ecosystem after the removal of livestock.

Dry grassland-shrubland ecosystems in New Zealand showed a significant response to the removal of livestock. Land grazed by sheep or cattle was dominated by exotic grasses, and carried many rabbits and hedgehogs. In comparison, land retired from grazing for conservation purposes was dominated by native herbs and shrubs, and had higher numbers of possums, hares and mice.

The change in tenure from pastoral to conservation land also had an impact on the invasive mammal communities present. Rabbits and hedgehogs were more abundant on pastoral sites, while possums, hares and mice were more abundant on conservation sites. Rabbits prefer shortgrass habitats, while hedgehogs may be attracted to areas with animal dung containing abundant invertebrates such as fly larvae and earthworms. By comparison, invasive mammals found on conservation land were generalist species, attracted to structurally complex and diverse habitats. It is not clear whether these patterns are driven ‘bottomup’ (i.e. by invasive mammals responding to available resources) or ‘top-down’ (i.e. by invasive mammals effectively engineering suitable habitat for themselves), or a combination of both.

Overall, removal of livestock led to the development of native-dominated plant communities, with a high abundance of shrubs. This has positive implications for conservation, as the low abundance of exotic weeds means there may be little need for active weed management. However, this benefit may be compromised by increases in the relative abundance of some invasive mammal species, potentially leading to negative implications for some species of conservation interest.

For more information about this research, check out our recently published paper in Biological Invasions:

Whitehead, Amy L., Byrom, Andrea E., Clayton, Richard I. & Pech, Roger P. (2013). Removal of livestock alters native plant and invasive mammal communities in a dry grassland–shrubland ecosystem. Biological Invasions DOI: 10.1007/s10530-013-0565-1

An earlier version of this post featured in an issue of Landcare Research‘s Vertebrate Pest Newsletter:
Whitehead, Amy L., Byrom, Andrea E., Clayton, Richard I. & Pech, Roger P (2011). Community responses to livestock removal from drylands. Kararehe Kino, 18, 9-10. [pdf]