Pages (Desktop)

Pages (Mobile)

Extracting data and making climate maps using WorldClim datasets

In this guide, i'll show you another way in which you can get climate data, by using WorldClim global climate datasets. This guide will take you through all the steps for downloading, opening, extracting, and plotting the data using R.

I'll show you how you can make some good looking climate maps using WorldClim data, for anywhere in the world. And, the guide will also look at some of the differences between the WorldClim datasets and CRU datasets (we looked at this in earlier guides), and why you might choose to use one or the other.

Guide Information

Title Extracting data and making climate maps using WorldClim datasets
Author Benjamin Bell
Published January 26, 2018
Last updated August 17, 2021
R version 3.4.2
Packages base; raster

This guide has been updated:

  • Since this guide was originally published, WorldClim have released a new version of their dataset (version 2.1). If using the later version with this guide, you will need to update your code to reflect the new file names.

This is a 4 part guide:

Part 1: Getting Climate Data Downloading CRU climate data and opening it in R.
Part 2: Working with extracted CRU climate data How to use the downloaded climate data, from calculating trends to plotting the data.
Part 3: Extracting data and making climate maps using WorldClim datasets How to obtain WorldClim climate data, importing it into R, and how to calculate trends and plot climate maps.
Part 4: RasterStacks and raster::plot An update to part 3, this guide shows you an easier way to import WorldClim data into R by using RasterStacks.

WorldClim global climate datasets

This is part three of my guides for getting climate data and working with it using R. If you missed part one or part two, you can go back and read them, but it is not necessary to have worked through them to follow this guide (although it may help!).

WorldClim is a collection of global gridded climate data "layers", which include temperature (mean, max, min), precipitation, solar radiation, wind speed and water vapour pressure. Unlike CRU datasets which contain data for every month between 1901 and 2016, WorldClim contains monthly averages for the period between 1970 and 2000. However, the resolution of WorldClim is much higher, with grid squares representing an area ~1 km2, versus the ~55 km2 resolution of CRU data. Both datasets are based on weather station data.

WorldClim data is also stored in a different file format. You might remember that CRU data came as NetCDF files, which are basically large data arrays. WorldClim data on the other hand can be download as GeoTiff files. These are essentially image files that contain additional metadata - such as map projection and coordinates.

Ultimately, the dataset you choose to work with will depend on the application, what kind of data you need, and personal preference. CRU data is better for looking at long-term data and identifing trends, while WorldClim might be better if you just want to know the typical climate conditions for an area.

Of course, WorldClim and CRU are not the only climate datasets out there, there are many, many more! But, since most datasets tend to provide either NetCDF or GeoTiff files, you should be able to use these guides as a basis for working with other datasets.

© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Downloading WorldClim data

Update: WorldClim have updated their dataset to version 2.1. Download links have been updated to reflect this, but the code in this guide still refers to version 2.0 when dealing with the file names. You should update your code if using the newer version with this guide.

For this guide, we'll look at average temperature. The latest version of WorldClim datasets is version 2.0. This includes files for "current" climate conditions (1970 - 2000), the previous version 1.4, also contains data for past climate (6000, and 20,000 years ago!), and future climate scenarios. For detailed information about the dataset, check out the associated research paper.

So, head over to the WorldClim site to download version 2.0 of the average temperature data. For this guide, you should download the "30 seconds" file (which is the highest resolution), and the link should be named "tavg 30s". It'll look something like this:

The .zip file will contain 12 separate files named "wc2.0_30s_tavg_01.tif", "wc2.0_30s_tavg_02.tif" etc. Each file contains data for one month with 01 being January, 02 February etc. Extract the files to a new R project folder, e.g. "WorldClim".

© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Extracting WorldClim data

To work with GeoTiff files in R, we'll once again use the "raster" package. If you have not already installed this package, go ahead and install.packages("raster") to install it. And remember to load the package after installation library(raster)

For this guide, we are going to load all the data into R by creating separate raster objects: RasterLayers, for each month.

Update: An alternative way to load all the data into R at the same time uses RasterStacks. Have a look at part 4 of this guide to find out how.

# Import WorldClim Mean Temp data
temp1 <- raster("wc2.0_30s_tavg_01.tif")
temp2 <- raster("wc2.0_30s_tavg_02.tif")
temp3 <- raster("wc2.0_30s_tavg_03.tif")
temp4 <- raster("wc2.0_30s_tavg_04.tif")
temp5 <- raster("wc2.0_30s_tavg_05.tif")
temp6 <- raster("wc2.0_30s_tavg_06.tif")
temp7 <- raster("wc2.0_30s_tavg_07.tif")
temp8 <- raster("wc2.0_30s_tavg_08.tif")
temp9 <- raster("wc2.0_30s_tavg_09.tif")
temp10 <- raster("wc2.0_30s_tavg_10.tif")
temp11 <- raster("wc2.0_30s_tavg_11.tif")
temp12 <- raster("wc2.0_30s_tavg_12.tif")

Lets take a look at one of the files in more detail. Simply type "temp1" into the R console:

> temp1
class       : RasterLayer 
dimensions  : 21600, 43200, 933120000  (nrow, ncol, ncell)
resolution  : 0.008333333, 0.008333333  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : wc2.0_30s_tavg_01.tif 
names       : wc2.0_30s_tavg_01 
values      : -46.9, 34.4  (min, max)

This provides some information about the file. For example, we can see how many data points there are (ncell), the coordinate system used, and the min/max values of the data. The temperature data is in degrees Celsius (℃), and we can see that the coldest average temperature in January was -46.9℃ somewhere in the world!

To extract data for your sample sites from WorldClim datasets, we'll use the same commands as we did for CRU data. Even though the raw data files are in different formats, loading them as raster objects in R means we can manipulate them in the same way.

For this example, we'll create a data frame with the sample site coordinates.

# Create a data.frame with sample site coordinates
site <- c("Manchester", "Liverpool", "Oxford", "London")
lon <- c(-2.24, -2.98, -1.25, -0.11)
lat <- c(53.47, 53.4, 51.74, 51.49)

samples <- data.frame(site, lon, lat, row.names="site")

Here, we created three vector objects containing our data, then we created the data frame object by telling R which vectors to use to construct the data frame. The vectors will become columns in the data frame. Specifying row.names="site" tells R that the "site" vector should be used for the row names in the data frame. To check that the data frame was created as you intended, simply type "samples" into the R console.

> samples
             lon   lat
Manchester -2.24 53.47
Liverpool  -2.98 53.40
Oxford     -1.25 51.74
London     -0.11 51.49

In the previous guide we extracted all the raw data to a new data frame using one command. Since our WorldClim data is stored in separate RasterLayer objects, we will instead "build" our new data frame by extracting each month separately, adding this data as a new column to the data frame.

# Extract data from WorldClim for your sites
temp.data <- samples 
temp.data$Jan <- extract(temp1, samples)
temp.data$Feb <- extract(temp2, samples)
temp.data$Mar <- extract(temp3, samples)
temp.data$Apr <- extract(temp4, samples)
temp.data$May <- extract(temp5, samples)
temp.data$Jun <- extract(temp6, samples)
temp.data$Jul <- extract(temp7, samples)
temp.data$Aug <- extract(temp8, samples)
temp.data$Sep <- extract(temp9, samples)
temp.data$Oct <- extract(temp10, samples)
temp.data$Nov <- extract(temp11, samples)
temp.data$Dec <- extract(temp12, samples)

First, we duplicated the "samples" data frame, and called it "temp.data" - this is the data frame that will contain our extracted temperature data. extract(temp1, samples) tells R to extract the data contained in the "temp1" object, using the coordinates in the "samples" object. temp.data$Jan tells R that the extracted data should be added as a new column named "Jan" to the existing data frame "temp.data" - Then we simply repeat for each month.

The data frame should now contain average temperature data for each month for your sample sites.

Update: With RasterStack objects it is possible to extract all of the data in one command, rather than "building" the data frame as shown here. Part 4 of this guide shows you how to do it. These instructions remain if you are only dealing with a few files and do not wish to use a RasterStack.

> temp.data
             lon   lat Jan Feb Mar Apr  May  Jun  Jul  Aug  Sep  Oct Nov Dec
Manchester -2.24 53.47 4.2 4.3 6.1 8.0 11.3 14.2 16.4 16.1 13.4 10.4 7.0 4.9
Liverpool  -2.98 53.40 4.5 4.5 6.2 8.1 11.4 14.2 16.3 16.0 13.6 10.5 7.3 5.4
Oxford     -1.25 51.74 4.5 4.6 6.7 8.5 12.0 15.0 17.3 16.9 14.1 10.8 7.2 5.3
London     -0.11 51.49 5.2 5.1 7.5 9.4 13.1 16.1 18.3 18.2 14.8 11.6 8.2 6.1

So that's how you can extract data for your sample sites from WorldClim datasets. You could now perform different kinds of analysis on the data, or plot some climate graphs. Refer back to part two of this guide for help. For example, you could calculate mean annual temperature for your sites by using rowsMeans() e.g.

> rowMeans(temp.data[3:14])
Manchester  Liverpool     Oxford     London 
  9.691667   9.833333  10.241667  11.133333 

Or mean summer temperatures:

> rowMeans(temp.data[8:10])
Manchester  Liverpool     Oxford     London 
  15.56667   15.50000   16.40000   17.53333  
© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Plotting climate maps using WorldClim data

Since the resolution of WorldClim datasets is very high, its possible to plot some good looking climate maps without too much work! So, lets make some climate maps - first, we'll create a colour scale for our temperature data - you are free to use any colours you like for your maps, so why not experiment creating different schemes? try ?colorRampPalette and ?colorRamp in the R console for help.

tempcol <- colorRampPalette(c("purple", "blue", "skyblue", "green", "lightgreen", "yellow", "orange", "red", "darkred"))

Next, we'll plot a map of global January temperatures using this colour scheme:

plot(temp1, col=tempcol(100))

The number in brackets after "tempcol" tells R how many colours to "create", using the colours defined in the "tempcol" object as its palette. i.e. it blends these colours together. So, tempcol(100) creates 100 colours which will create a "smooth" colour scale.

You could also create a map of the January temperatures for the UK by defining the boundaries of the plot, using xlim=c() and ylim=c() e.g.

plot(temp1, xlim=c(-12, 4), ylim=c(48, 64), col=tempcol(100))

Which will result in the following map:

But now the colour scale looks a bit misleading. This is because R automatically sets the scale to the min and max values that you are plotting, so our colour scale no longer makes sense. It is possible to override this using zlim=c() e.g.

plot(temp1, xlim=c(-12, 4), ylim=c(48, 64), zlim=c(-10,30), col=tempcol(100))

Now this map looks more realistic!

You can also use zlim=c() to define the range of values you want to plot. Lets re-plot the world map, but this time we'll just plot the areas with temperatures above 25℃ in August. We'll also use the inbuilt colour scale "heat.colors".

plot(temp8, zlim=c(25, 50), col=rev(heat.colors(20)))

Could be useful for planning your next holiday? - But, perhaps it is now a bit hard to see where these places are. Lets add a simple outline of the world using the "maps" package - install.packages("maps") to install it.

library(maps)
plot(temp8, zlim=c(25, 50), col=rev(heat.colors(20)))
map("world", add=TRUE)

Which results in the following map:

© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Subsetting WorldClim data

You may have noticed that it can take R quite a long time to plot maps using the WorldClim dataset (unless you have a very powerful computer!) - this is because, as their name implies, they contain data for the whole world, so when loaded into R, they use a lot of memory. If you're only interested in a small part of the world, it makes sense to subset the data before you plot it. We did this in part one of the guide when we plotted a map of the UK with the CRU dataset.

After subsetting the WorldClim raster object to a smaller area, you can also combine the objects - for example, to create seasonal or annual temperature maps. (You can also do this with the full data, if your computer is powerful enough!)

Lets take a look at another part of the World. Most of my research has been focused in Morocco, so lets look at temperatures there. First, we'll define the extent() and then use crop() to subset our WorldClim data to Morocco.

# Subset Morocco data
ma.area <- extent(-15, 0, 27, 37)
ma.temp1 <- crop(temp1, ma.area)
ma.temp2 <- crop(temp2, ma.area)
ma.temp3 <- crop(temp3, ma.area)
ma.temp4 <- crop(temp4, ma.area)
ma.temp5 <- crop(temp5, ma.area)
ma.temp6 <- crop(temp6, ma.area)
ma.temp7 <- crop(temp7, ma.area)
ma.temp8 <- crop(temp8, ma.area)
ma.temp9 <- crop(temp9, ma.area)
ma.temp10 <- crop(temp10, ma.area)
ma.temp11 <- crop(temp11, ma.area)
ma.temp12 <- crop(temp12, ma.area)

When you plot the these new objects, it should be much quicker. Lets plot a map of August temperatures in Morocco. We'll also add some place names for reference points.

# Plot
plot(ma.temp8, zlim=c(-10,40), col=tempcol(100))
map("world", add=TRUE, lwd=1.5) # Add country outlines
# Add place names
place <- c("RABAT", "Casablanca", "Marrakech", "Agadir")
place.lon <- c(-6.83,  -7.60, -7.99, -9.6)
place.lat <- c(33.96,  33.544, 31.63, 30.421)
points(place.lon, place.lat, pch=15, col="black", cex=1.5)
text(place.lon, place.lat, labels=place, pos=2, cex=1.5)
# Add title
title(main="Mean August Temperatures\n(1970 - 2000)")

From this map we can easily see the temperature range across Morocco during August. Looks like fairly nice summer temperatures for most of the country, getting much cooler in the mountains, and much hotter along the southern border with Algeria.

If you wanted to quickly know the temperature value for any point on the map, you could use the click() command from the raster package. After inputting the command in the R console, simply click anywhere on the map, and the value of where you clicked will be output to the R console. To stop clicking, press escape on the keyboard, or "stop" on the menu bar.

> click(ma.temp8, xy=TRUE)
        x        y value
1 -5.8625 34.22917  26.5
        x        y value
1 -6.9625 33.00417  25.7
        x        y value
1 -8.4125 32.70417  25.4
          x       y value
1 -4.629167 30.8625  33.5
          x        y value
1 -8.245833 29.27917  31.3

(Your results will vary!)

© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Creating seasonal plots

Using the subsetted WorldClim data for Morocco, we can easily create seasonal temperature maps. First, we'll create the seasonal data.

ma.temp.spring <- mean(ma.temp3, ma.temp4, ma.temp5)
ma.temp.summer <- mean(ma.temp6, ma.temp7, ma.temp8)
ma.temp.autumn <- mean(ma.temp9, ma.temp10, ma.temp11)
ma.temp.winter <- mean(ma.temp12, ma.temp1, ma.temp2) 

In this code we are simply creating new raster objects which contain the average temperature values for each season.

Now, we'll plot all four seasons in one figure:

# Full guide available at http://www.benjaminbell.co.uk
# 4-part seasonal temperature plot using subsetted WorldClim data

layout(matrix(1:4, ncol=2, byrow=TRUE)) # Set up plot layout

# Spring
par(mar=c(4, 4, 4, 4)) # Set margin
plot(ma.temp.spring, zlim=c(-10,40), col=tempcol(100))
map("world", add=TRUE, lwd=1.5) # Add country outlines
points(place.lon, place.lat, pch=15, col="black", cex=1) # Add places
text(place.lon, place.lat, labels=place, pos=2, cex=1) # Add place names
title(main="Mean Spring Temperatures") # Title

# Summer
par(mar=c(4, 4, 4, 4)) 
plot(ma.temp.summer, zlim=c(-10,40), col=tempcol(100))
map("world", add=TRUE, lwd=1.5) 
points(place.lon, place.lat, pch=15, col="black", cex=1) 
text(place.lon, place.lat, labels=place, pos=2, cex=1)
title(main="Mean Summer Temperatures") 

# Autumn
par(mar=c(4, 4, 4, 4)) 
plot(ma.temp.autumn, zlim=c(-10,40), col=tempcol(100))
map("world", add=TRUE, lwd=1.5) 
points(place.lon, place.lat, pch=15, col="black", cex=1)
text(place.lon, place.lat, labels=place, pos=2, cex=1)
title(main="Mean Autumn Temperatures")

# Winter
par(mar=c(4, 4, 4, 4)) 
plot(ma.temp.winter, zlim=c(-10,40), col=tempcol(100))
map("world", add=TRUE, lwd=1.5) 
points(place.lon, place.lat, pch=15, col="black", cex=1)
text(place.lon, place.lat, labels=place, pos=2, cex=1)
title(main="Mean Winter Temperatures")

Which results in the following plot:

And that's it! - Making climate maps is really easy and straight forward using WorldClim data and R. You can use what you've learned in this guide to create all kinds of climate plots for anywhere in the world. Perhaps you want to plot a figure which has all 12 months of data on it instead? Or perhaps you want to plot a map which compares maximum and minimum temperatures for an area? - Just download the appropriate datasets from the WorldClim website. Update: I've now created a guide to show you exactly how to do this. Check it out!

Of course, its possible to create the same plots using other climate datasets, including the CRU data - although they might not look as nice since the resolution of the data is lower. However, its possible to interpolate the data to make the maps look much better. This will be the subject of a future guide!

Thanks for reading, please leave any questions or comments below.

This is a 4 part guide:

Part 1: Getting Climate Data Downloading CRU climate data and opening it in R.
Part 2: Working with extracted CRU climate data How to use the downloaded climate data, from calculating trends to plotting the data.
Part 3: Extracting data and making climate maps using WorldClim datasets How to obtain WorldClim climate data, importing it into R, and how to calculate trends and plot climate maps.
Part 4: RasterStacks and raster::plot An update to part 3, this guide shows you an easier way to import WorldClim data into R by using RasterStacks.
© Benjamin Bell. All Rights Reserved. http://www.benjaminbell.co.uk

Further reading

Extracting CRU climate data - Part 1 of this guide!

Working with the extracted climate data - Part 2 of this guide!

Working with RasterStack objects and WorldClim Data - Part 4 of this guide!

34 comments

  1. Considering that a single worldclim dataset of 12 layers (months) is almost 3 Gbs in size. Is there a way of using a polygon to extract for my specific area without necessarily downloading the whole globe? In otherwords, extracting for my study area without downloading the whole globe.

    ReplyDelete
  2. Can I use a shapefile or bounding box to extract worldclim data remtoly without downlading the whole global layer?
    For specific RCP
    Specific time e.g 2050
    Specific month e.g July

    Downloading 3 Gb data only to use about 10 Mbs is just a big waste of resources.

    ReplyDelete
    Replies
    1. Hi Wyclife,

      I don't think this is possible from the WorldClim website. But, you can download smaller tiles of WorldClim v1.4 data from within R using the getData function from the raster package.

      Check out ?getData for details, but the necessary code would be something like:


      library(raster)
      mysite <- getData('worldclim', var='tmin', res=0.5, lon=5, lat=45)


      Change the lon and lat coordinates to your location, and change the var argument to the type of data you want (the example here is min temperature).

      There is more information about WorldClim 1.4 data here: https://www.worldclim.org/formats1

      Hope this helps,
      Ben

      Delete
  3. Hi Ben,

    When I am getting the following error while loading the file:

    > temp1 <- raster("wc2.0_30s_tavg_01.tif")
    Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
    Cannot create RasterLayer object from this file; perhaps you need to install rgdal first

    Then I tried to install rgdal, but got the following error:

    > install.packages("rgdal")
    Installing package into ‘C:/Users/raisa/Documents/R/win-library/3.4’
    (as ‘lib’ is unspecified)
    --- Please select a CRAN mirror for use in this session ---
    Warning message:
    package ‘rgdal’ is not available (for R version 3.4.2)

    Please suggest a way or point out the error.

    Thank you
    Saumitra Rai
    IIRS, Dehradun
    BITS Pilani, India

    ReplyDelete
    Replies
    1. Can you update R to the latest version and try again?

      Delete
    2. I installed R 4.0.1 and it is working just fine now. Thanks for the advice.

      Delete
  4. Hi Ben,

    For my project I want to plot a map (image) of temperature anomaly of a specified area in Ladakh (India) for few particular years (say 1980, 1990, 2000, 2005, 2010 etc) I want to take the annual mean temperature from 1901-1980 as the baseline. Since WorldClim Dataset has a monthly mean and the data is in fragments, I do not suppose it would be appropriate to use. While on the other hand, in part 2 of the blog though you have explained how to plot graph of temperature anomaly using CRU Dataset you have left out if there is any way of plotting Map (image) of temperature anomaly of a specified area using CRU Dataset.

    If you have covered this issue in any other blog or if you know of any other resources that may help me, then please provide the link of the same. If both CRU and WorldClim Dataset are not appropriate for the purpose, then which dataset may help me? Do tell if I am wrong and if WorldClim Dataset can be used for the purpose.

    Thank you
    Saumitra Rai
    IIRS, Dehradun
    BITS Pilani, India

    ReplyDelete
    Replies
    1. Hi Saumitra,

      You are right that the CRU data is best for this purpose. It is not possible to use the Worldclim data as you have described, as this is provided as the mean climate for the period 1970 to 2000 only.

      It is possible to plot a map of temperature anomaly, rather than just a graph (as shown in part 2 of the guide) - To do so, you would need to perform the calculations on the gridded data raster, rather than extracting the data and performing the calculation on the extract.

      I haven't written a guide on doing this yet - but it sounds like it would be useful, so i will try and add one in the future!

      Best wishes


      Delete
  5. The precipitation data has lesser value than actual ranges in the my area. can I use it directly? would you specify the ranges of values into (higher, medium, lower)?

    ReplyDelete
    Replies
    1. Hi, in areas with poor station coverage (e.g. mountain areas), the precipitation data from CRU is often not a good representation of actual precipitation.

      No reason why you cant provide a range (min, max, and also show the mean or median).

      If you could find actual station data, that would of course be the best source of data.

      Best wishes,
      Ben

      Delete
  6. Hello, are you sorry to extract data from specific periods, for example from 1990 to 2000? Can you help me please?

    ReplyDelete
    Replies
    1. Hi Jorge - You cannot do this with WorldClim data. You need to use the CRU datasets to get specific periods. There are guides on this blog showing you how.

      Best wishes

      Delete
    2. Olá Ben- Thanks for the information. From what I understood is that I must first directly download the CRU data for a specific period example (Tem-min 1980-1989) and then work the same as the WorldClim data. Is that the same? Is there any way to download directly from R?
      Hugs

      Delete
    3. Yes - get the data first, then you can start to work with it in R. The way to handle the CRU is similar to WorldClim, but there are some differences, so it is probably best to look at the specific guides to using CRU data.

      Start here: https://www.benjaminbell.co.uk/2018/01/getting-climate-data.html

      This focusses on getting the CRU data, and opening it up in R.


      You can download anything from within R :) - But in this case, its simply easier to download the CRU data via a web browser.

      There are packages that allow you to download and manipulate CRU data in R - although i am not familiar with how they work.

      Best wishes,
      Ben

      Delete
    4. Thanks Ben, good information !!

      Delete
  7. Hi, if using precipitation data instead of temperature, would I change the code to 'precip'?

    ReplyDelete
    Replies
    1. Hi Nathan,

      For the worldClim precipitation data, make sure to download the appropriate dataset, and you can use this code in the same way. Probably is a good idea to change “temp” to “precip” for the object names, but really the key is to load the precipitation data, rather than the temp data.

      Best wishes,
      Ben

      Delete
  8. Dear Benjamin, thank you very much for your blog. I am learnig ftom it. At the moment one problem to plot a map using WorlClimate. The X axis is bigger than necesary (more than 10 degree and I like between -70 to -68.) Can I modify it? Thank you in advance

    ReplyDelete
    Replies
    1. Hi,

      You should be able to set the x axis limits to whatever you want, try adding: xlim=c(-70, -68) to the plot code.

      Best wishes,
      Ben

      Delete
  9. Hi Benjamin. I do not have any doubt so far. I just like to thank you so much for this website. A lot of people that I know are learning from your site. Thanks Benjamin for making our lives simpler:)

    ReplyDelete
    Replies
    1. Thank you for the kind comments, and I'm glad this helped!

      Ben

      Delete
  10. Hi Benjamin,
    Firstly thanks for the website. I am new to R and learning to use it for my work. I found your website very useful.
    My doubt here is
    after unzipping files from downloaded data of precipitation, the file name is "wc2.1_30s_prec_01"(in properties it is tiff file but .tif did not come)
    Because it 2.1 data for prec, I changed line "temp1 <- raster("wc2.0_30s_tavg_01.tif")" to "temp1 <- raster("wc2.1_30s_prec_01.tif")".
    and when I run it I am seeing this error
    Error in .local(.Object, ...) :

    Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
    Cannot create a RasterLayer object from this file. (file does not exist)
    I am not able to figure out. Could you clarify my doubt please.

    Thanks in advance

    ReplyDelete
    Replies
    1. Hi,

      Apologies for the delayed response - if the file you have downloaded does not include a file extension, you probably shouldn't use the file extension in the R code.

      In R, change the working directory to where you have downloaded the files. Use the setwd() command. Then, use the list.files() command, and it should show you all the files in that directory.

      In your R code, use the same name as shown by list.files(), and it should work.

      Best wishes,
      Ben

      Delete
  11. Thank you very much for your website

    ReplyDelete
  12. Hi Benjamin, When I use xlim=c() and ylim=c() to crop map, the boundary of figures does not change. Could you tell me why it happens?

    Best wishes,
    Maolin

    ReplyDelete
    Replies
    1. Hi Maolin,

      The xlim and ylim arguments for the plot function simply change what is shown within the plot region - e.g. the graphic that is produced on screen, and it does not actually crop the data that you are plotting.

      Therefore, it is best to crop the data (the map/dem) itself using the crop() and extent() functions.

      Best wishes,
      Ben

      Delete
    2. Dear Ben,

      Thanks for your reply, but when I use crop() and extent() function. My figures still have white space. Could you give me your email and then I can send you my figures.

      Best wishes,
      Maolin

      Delete
    3. Arh, you get white space when the size of the thing you are plotting, or more correctly, the aspect ratio, does not match the size/aspect ratio of the plot window. This problem occurs because the default plotting device does not automatically change size to match the content being plotted.

      This can get a bit confusing, as the plot "window" is made up of 3 regions: the plot region (where the plotted data goes), the figure region (where axes, labels etc. goes) and then the device region. The device region contains the figure and plot region and it is often the same size as the figure region, but can be changed when you start to customise the plots.

      I do have a guide that gives an overview of this: https://www.benjaminbell.co.uk/2018/02/creating-multi-panel-plots-and-figures.html

      Usually when you plot something, you will plot to the screen - this is a "graphics device" and the actual device depends on which operating system you are using. Additionally, i believe Rstudio uses its own graphics device. But, you can also plot straight to a graphics file using the correct device - e.g. pdf() would create a pdf file.

      Assuming you are just plotting to the screen, the simplest way to get rid of the white space is just to resize the plot window with your mouse. But this is not ideal, and you would have to do it for every figure. This also may not work in Rstudio.

      The better option is to tell R the width and size of the graphics device before plotting. For example, if you are plotting to screen on Windows, you can setup the graphics device before your plot code and tell it what size to be: windows(width=15, height=10). On mac, it would be quartz(width=15, height=10) and Linux x11(width=15, height=10). Then you would run the plot code.

      You will have to play around with the width and height arguments to get it to match your data/map.

      Generally, I would advocate plotting straight to a file, which you would also have to set the size, e.g.

      pdf(“map.pdf”, width=15, height=10)
      plot(x)
      dev.off()

      Hope this helps,
      Ben

      Delete
  13. Hello Ben, I really enjoy your blog and have learned a lot from it. Now I want to generate the biovars for 2020 using the 2.5-minute historical monthly weather data from 2020, which includes 19 bioclimatic variables, using the dismo package in R. However, I have failed many times, and the biovars file for 2020 that I generated to calculate aboveground biomass even resulted in negative values. I don't know what went wrong, can you help me? Here is my code:
    #Packages that need to be installed
    #install.packages("raster")
    #install.packages("rgdal")
    #install.packages("dismo")

    #Load package
    library(raster)
    library(sp)
    #library(rgdal)
    library(dismo)

    #Read tmax
    tmaxpath <- "D:/cdata/tmax2020"
    setwd(tmaxpath)
    tmax_file <- list.files(tmaxpath ,pattern=".tif$", full.names = FALSE)
    tmax_raslist = list()

    print( tmax_raslist)

    for(i in 1:12){
    tmax_raslist = c(tmax_raslist, raster(tmax_file[i]))
    }
    tmax_stack<-stack(tmax_raslist)

    #Read tmin
    tminpath <- "D:/cdata/tmin2020"
    setwd(tminpath)
    tmin_file <- list.files(tminpath ,pattern=".tif$", full.names = FALSE)
    tmin_raslist = list()
    for(i in 1:12){
    tmin_raslist = c(tmin_raslist, raster(tmin_file[i]))
    }
    tmin_stack<-stack(tmin_raslist)

    #Read prec
    precpath <- "D:/cdata/prec2020"
    setwd(precpath)
    prec_file <- list.files(precpath ,pattern=".tif$", full.names = FALSE)
    prec_raslist = list()
    for(i in 1:12){
    prec_raslist = c(prec_raslist, raster(prec_file[i]))
    }
    prec_stack<-stack(prec_raslist)

    #Calculate 19 variables using the biovars function of the dismo package
    x<-biovars(prec=prec_stack,tmin=tmin_stack,tmax=tmax_stack)

    #Output the calculation results as a tif file with 19 bands
    filepath <- "D:/cdata/WorldClimateBiovar2020"
    setwd(filepath)
    output= 'BioVars.tif'
    writeRaster(x, output, format = "GTiff")

    ReplyDelete
    Replies
    1. Hi, I'm not familiar with the dismo package, or how it works. Usually, errors result from incorrect data/object formats, i.e. the package function may be expecting data in a certain format, which you may be supplying in another, thus resulting in errors.

      Do you have any more information about the error that is being generated?

      Ben

      Delete
  14. Dear Ben,
    Greetings! I am currently engaged in research on the distribution of mangrove forests. I have read the paper "Predicting Global Patterns in Mangrove Forest Biomass," which has been immensely enlightening and helpful for my research.
    Recently, while attempting to apply the method outlined in the paper to calculate the global above-ground mangrove biomass for the year 2020, I encountered some technical difficulties. Specifically, I observed negative values in the calculated biomass for certain regions in the Northern Hemisphere, which is obviously inconsistent with expectations. Despite multiple attempts to rectify and identify the source of the issue, I regret to say that I have been unable to find a solution.
    To explain my workflow in detail, I used data from the Historical monthly weather data at a 2.5-minute level, including minimum temperature, maximum temperature, and precipitation for the year 2020. Based on this data, I wrote R code and generated bioclimatic variable data comprising 19 bands (BIO1 to BIO19). Subsequently, I calculated the above-ground biomass (AGB) of mangrove forests using the formula:
    AGB(t ha^(-1) )=0.295BIO10+0.658BIO11+0.0234BIO16+0.195BIO17-120.3
    For instance, in the case of mangroves south of Kolkata, India, Calculate the AGB of a certain point (88.6519725°N, 21.9435456°E), where BIO10, BIO11, BIO16, and BIO17 are 29.34℃, 21.67℃, 1106.90mm, and 30.4mm respectively. The value of AGB is -65.56.the calculated AGB value turned out to be negative, which is perplexing.
    I am reaching out to you for guidance and suggestions. Attached, please find my R code and relevant data for your reference. Your advice and insights would be greatly appreciated.
    Thank you very much for taking the time to read my email amidst your busy schedule. I eagerly await your valuable feedback.
    R code:
    #Load package
    library(raster)
    library(sp)
    library(dismo)

    #read tmax
    tmaxpath <- "D:/cdata/tmax2020"
    setwd(tmaxpath)
    tmax_file <- list.files(tmaxpath ,pattern=".tif$", full.names = FALSE)
    tmax_raslist = list()
    for(i in 1:12){
    tmax_raslist = c(tmax_raslist, raster(tmax_file[i]))
    }
    tmax_stack<-stack(tmax_raslist)

    #read tmin
    tminpath <- "D:/cdata/tmin2020"
    setwd(tminpath)
    tmin_file <- list.files(tminpath ,pattern=".tif$", full.names = FALSE)
    tmin_raslist = list()
    for(i in 1:12){
    tmin_raslist = c(tmin_raslist, raster(tmin_file[i]))
    }
    tmin_stack<-stack(tmin_raslist)

    #read prec
    precpath <- "D:/cdata/prec2020"
    setwd(precpath)
    prec_file <- list.files(precpath ,pattern=".tif$", full.names = FALSE)
    prec_raslist = list()
    for(i in 1:12){
    prec_raslist = c(prec_raslist, raster(prec_file[i]))
    }
    prec_stack<-stack(prec_raslist)

    #Calculate 19 variables using the dismo package's biovars function
    x<-biovars(prec=prec_stack,tmin=tmin_stack,tmax=tmax_stack)

    # Output the result of the calculation as a tif file with 19 bands
    filepath <- "D:/WorldClimate/biovar20"
    setwd(filepath)
    output= 'BioVars.tif'
    writeRaster(x, output, format = "GTiff")

    ReplyDelete
    Replies
    1. Hi there,

      Have you got your coordinates the right way round?

      When i put 88.6519725°N, 21.9435456°E into Google Maps, it shows me an area in the sea to the north of Norway.

      In R, ensure that you are using decimal degrees for your coordinates, and also that you input longitude first, then latitude (this is the opposite to what you might normally do!).

      It might be worth plotting your sites on a map (in R), before you do the climate analysis, just to check the coordinates are correct.

      Best wishes,
      Ben

      Delete
  15. Hi Ben, Thanks so much for the helpful guide. I used WorldClim 2.1 to determining bioclimatic variables and elevations for some sites in Costa Rica and Panamá where I do my research. However, I'm finding many of the elevation values from WorldClim 2.1 don't match what I've found with a GPS or with Google Earth at the same points. Do you have any ideas about the validity of elevation values for this new version? I used a similar extraction approach as those used for other variables, and only saw one tif file for elevation, and I wonder if I did something wrong.

    ReplyDelete
    Replies
    1. Hi Kristen,

      Worldclim's elevation is based on the SRTM DEM which has a 30 m resolution - therefore, the elevation values will be an average elevation for that 30 x 30 m square.

      Your GPS is likely to be more accurate since it should have a much higher resolution (a few metres), similarly, Google Maps also has much higher resolution, thus elevation will be more accurate.

      Ben

      Delete

Comments are moderated. There may be a delay until your comment appears.