**R FOR HYDROLOGISTS **

LOADING AND PLOTTING THE DATA (Part 2)

In hydrology, it is very common to analyze the annual behavior of the levels in order to see if there is any recurrent patterns over the year (seasonality.) In order to observe the historical behavior of the river, we have to construct a plot with the level of all the years overlapped from the first of January to the 31st of December. This task can be solved in many possible ways, but this time we will use the capabilities that `ggplot2`

has to offer and organize information from the data frames.

If you don’t have the data, please first see the first part of the tutorial here.

Answers to these exercises are available here.

**Exercise 1**

First, we have to process a little bit of the data frame in order to provide it to the ` ggplot `

function in the right format. Please add a column ` YEAR `

and ` DOY `

(day of year) to the ` river_data `

. Hint: the ` lubridate `

package has the function ` yday `

(year day) you can install with the line.

` if(!require(lubridate)){install.packages(lubridate, dep=T)} `

**Exercise 2**

Create a plot with the level of all the years overlapped from the first of January to the 31st of December with the ` ggplot `

function.

**Exercise 3**

Now it is plotted, but it doesn’t seem very clear because all the lines have the same color. Please plot each line with a different color according to the year.

**Exercise 4**

That looks better. Now we want to see the average annual behavior so we will calculate the mean value of `LEVEL`

for each `DOY`

(day of the year) using the function `aggregate`

. Then assign it to the data frame `mean_data`

.

**Exercise 5**

Please add a ` DOY `

column to the `mean_data`

; also, a ` YEAR `

column with the value “2000.” This last column has to be inserted in order to overlap the plots with the function ` ggplot`

.

**Exercise 6**

Please overlap the plot generated in Exercise 3 with the `mean_data`

.

**Exercise 7**

The mean looks a little bit spiky. In order to visualize better, we will smooth the mean values with the function ` qplot`

.

**Exercise 8**

The default smoothing parameter flattens up too many details. Please adjust the parameter ` span `

to get more details.

**What's next:**

- Explore all our (>1000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below

## Leave a Reply