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.
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
DOY (day of year) to the
river_data . Hint: the
lubridate package has the function
yday (year day) you can install with the line.
Create a plot with the level of all the years overlapped from the first of January to the 31st of December with the
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.
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
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
Please overlap the plot generated in Exercise 3 with the
The mean looks a little bit spiky. In order to visualize better, we will smooth the mean values with the function
The default smoothing parameter flattens up too many details. Please adjust the parameter
span to get more details.