14 October 2013

How to Build A Climate Diagram in R

Walter & Leith climate diagrams illustrate precipitation and temperature changes throughout the year in one standardized chart.  They are especially useful to determine water stress or other significant climatic factors on plants.  This step-by-step guide teaches how to generate your own Walter & Leith climate diagram using the software called R.  Here are some samples that I made for several places in the Andean highlands following this process:

This climate diagram of Juliaca shows water stress from May to April, and excess water in January and February.  The red line is temperature, measured on the left axis.  The purple line is precipitation, measured on the right axis.  The x-axis is one year, measured in months, from January to December.  Below are the same diagram for four other locations in the Andes.  They show the wide variation in climate found in this bioregion.



R is a command-line based open source software that is hugely flexible for computations and quantitative visuals. This post assumes basic knowledge of R. If you don't know anything about R, see this post for an introduction and links to more information. You will also need basic working knowledge of excel or similar spreadsheet software. I find that OpenOffice's spreadsheet software works even better than Excel and it's open source to boot.

For any more advanced R users reading this post, there are definitely more efficient ways to do this task. I put together this tutorial for a 2-hour introductory workshop with people who had never used the software before, so I chose to make it as simple as possible. Please feel free to do this your way, and to post comments with any recommendations.

Downloads you will need:


In this tutorial we will use the climatol package. Its description and guide can be found here:
http://cran.r-project.org/web/packages/climatol/index.html
You will need to download this package, using R, prior to starting this process.


Part 1: Gathering Data


Before starting, have this data on hand for your location of interest:
  1. Name of the location.
  2. Elevation of the location in meters above sea level.
  3. The range of years that the climate data was collected.
  4. The monthly climate data as per the table below (Abs min t is optional).

Part 2: Preparing the data in a spreadsheet


1. Create a spreadsheet with the following information in the upper left-hand corner of the file (replace the numbers to match your data - this is just an example).


Precip = average precipitation in mm per month
Max temp = maximum average temperature per month in ºC
Min temp = minimum average temperature per month in ºC
Abs min t = daily minimum temperature per month in ºC.

If you do not have the abs min t data, simply copy the minimum temperature data from the column above, but do not leave the cells blank.

2. Delete the first column in excel so that only the data remains. Be sure to delete any extra text and formatting anywhere in the file.

3. Save the file as a .csv

Part 3: Working in R


1. In R, use the Package drop down menu to install the climatol package. You will need an intenet connection to do this.

2. Attach the installed package to your session using this command:

>require(climatol)


3. Set the working directory to the folder where you keep your files using the Misc drop down menu. Verify it with this command, which should return the file path to the directory you selected in the drop down command.

> getwd()

4. Load and assign a name to your data:

> name=read.csv("folder/subfolder/datafile.csv")

5. View your data and check for anything unexpected. Reload as needed.

>name

COMMON ERROR: If you see anything strange here, then go back to step 2 and delete rows and columns that are adjacent to your data (even if they are blank). Be sure to put your cursor in cell A1, which should be the upper left-hand corner of your table, before saving.  You can also try a "paste special" of "values" only into a new spreadsheet and resaving.  If it still isn't normal-looking, then try opening your spreadsheet in Openoffice (it's free) and converting into .csv from there, with no extra formatting.

6. Create the plot (this returns in a new window):

> diagwl(name, est="Location",alt=elevation, per="dates", mlab="en")

COMMON ERROR: If you do not see a window with the graph open up, select the button that looks like a bar graph at the top of the R window, and try the command again, using the up arrow.

7. Adjust the colors if you like, using the color chart pdf:

> diagwl(name,est="Location",alt=elevation,per="dates",mlab="en",pcol="#color1",sfcol="#color2")

These are the color parameter names, with their default colors:

pcol Color pen for precipitation ("#005ac8").
Tcol Color pen for temperature ("#e81800").
Pfcol Fill color for probable frosts ("#79e6e8").
Sfcol Fill color for sure frosts ("#09a0d1").

8. Assign a name to the plot:

> plotname=diagwl(name,est="Location",alt=elevation,per="dates",mlab="en",
pcol="#color1",sfcol="#color2")

9. Generate a plot file of the plot in your working directory as .eps:

For an .eps file, use this string of commands:

> postscript("plotname.eps", horizontal = FALSE, onefile = FALSE, paper = "special", height = 10, width = 10)
> setEPS()
> postscript("plotname.eps")
> diagwl(name,est="Location",alt=elevation,per="dates",mlab="en",
pcol="#color1",sfcol="#color2")
> dev.off()


10. Repeat from step 4 to create any additional diagrams while in the same session.

I originally developed this tutorial as part of a workshop for graduate students in Environmental Science and Policy at Central European University in Budapest, Hungary.





14 comments:

  1. can anyone help me to do climatogramm if i give my informatitons?

    ReplyDelete
  2. This blog has instructions for you to build your own climate diagram using R for free. All you have to do is input the data. If you try this and get stuck, feel free to leave your question here. Or are you looking for someone to make the climate diagram for you as a consultant?

    ReplyDelete
  3. the second thanks i'm zenit

    ReplyDelete
  4. zenit, in that case you can send me your project information and the details as to what you are looking for and by when to my skype account which is biodivercities. From there I can follo up with you.

    ReplyDelete
  5. Hello Jennifer. Thank you for your helpful outline on working with climatol package. I want to raise an issue that I found with your instructions, namely on how to prepare the excel data folder prior to importing it into R Studio. The package instructions of climatol on R specifically say:

    "Climatic data must be passed as a 4x12 matrix of monthly (January to December) data, in the following order:

    Row 1:
    Mean precipitation

    Row 2:
    Mean maximum daily temperature

    Row 3:
    Mean minimum daily temperature

    Row 4:
    Absolute monthly minimum temperature"

    Please adjust your instructions accordingly to avoid confusion for future users, and misrepresented graphs.

    Kind regards,
    Coco

    ReplyDelete
    Replies
    1. Coco, thank you for your clarification on the instructions. I have provided instructions that match precisely the steps that I took. Have you tried executing the graph with the rows as you have indicated? Perhaps the R package has been updated? I am not wanting to change my instructions without having some verification that your method worked for you.

      Delete
  6. The elongations of both vertical axes seem to be different, isn't that provide misleading presentation from the chart?

    ReplyDelete
    Replies
    1. I think you are referring to the fact that the vertical axis on the left has different units than the one on the right. There is no avoiding this sense the data is in different units and in order to display the data overlapped, we must include 2 different axes. I have color coded them to match the corresponding data so that it is clear which data belongs to which axis. I hope this helps.

      Delete
  7. Hey, you did a really nice job here, short and simple. However is there a way of changing the C to °C or at least putting whitespace behind the number before C?

    ReplyDelete
  8. Is there a way of changing C to °C or at least put some whitespace behind the temperature?

    ReplyDelete
    Replies
    1. Since R allows you to completely customize these types of things, I am sure there is a way to accomplish this. However, I suspect that these parameters are set within the climatol package and would therefore require a more extensive knowledge of the program to access these variables. If you find out, please come back and paste the answer here for future users. If not, I would suggest that you can do a workaround by simply providing your own summary data on top, and cropping the resulting image before pasting it in your report.

      Delete
    2. I found a way just call this function:

      > fix(diagwl)

      and change the following lines

      mtext("C", 2, col = tcol, las = 1, line = 3, adj = 0, at = 55)

      to

      mtext(expression(degree*C), 2, col = tcol, las = 1, line = 3, adj = 0, at = 55)

      and

      mtext(paste(round(mean(tm * 10))/10, "C ", round(sum(p)),
      " mm", sep = ""), line = 1, adj = 1)

      to

      term1 <- round(mean(tm * 10))/10
      term2 <- round(sum(p))
      mtext(bquote(paste(.(term1), degree, "C ",.(term2), " mm", sep = "")),
      line = 1, adj = 1)

      however this is just a temporary solution, once climatol is reloaded again it is set back to default.

      Delete
    3. Thank you for posting this - so helpful!

      Delete