[R] how to plot annual values directly

Duncan Mackay dulcalma at bigpond.com
Fri Aug 5 04:36:34 CEST 2016


Hi

By coincidence I will very soon have to  do something similar. So I thought
this would be a practice run

Data is 10 years of daily rainfall data
str(arm)
'data.frame':   3640 obs. of  7 variables:
 $ date : Date, format: "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04"
...
 $ year : int  1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
 $ month: int  1 1 1 1 1 1 1 1 1 1 ...
 $ day  : int  1 2 3 4 5 6 7 8 9 10 ...
 $ rain : num  0 19 0 0 0 0 0 8.2 5.6 1 ...
 $ doy  : num  1 2 3 4 5 6 7 8 9 10 ...
 $ ym   : Date, format: "1990-01-01" "1990-01-01" "1990-01-01" "1990-01-01"
...

head(arm)
            date year month day rain doy         ym
48578 1990-01-01 1990     1   1    0   1 1990-01-01
48579 1990-01-02 1990     1   2   19   2 1990-01-01
48580 1990-01-03 1990     1   3    0   3 1990-01-01
48581 1990-01-04 1990     1   4    0   4 1990-01-01
48582 1990-01-05 1990     1   5    0   5 1990-01-01
48583 1990-01-06 1990     1   6    0   6 1990-01-01

The ym is calculated using ?as.yearmon from the zoo package. 
The zoo package may be useful because of its aggregating functions

Need mean daily rain - use whatever aggregation factor/function that you
need

arm.A <- aggregate(rain ~ year, arm, function(x) mean(x[x>0]) )

library(lattice)

xyplot(rain ~ date, arm,
       groups = factor(arm$year),
       ylim  = c(0,60),
       avg   = arm.A$rain, # 
       panel = panel.superpose,
       panel.groups = function(x, y,  type, col,group.number,avg, ...) {
                  
                 # plot daily values
                  panel.xyplot(x, y, col = "grey80", type = "h") # delete if
necessary
                 # plot average
                  panel.xyplot(x, avg[group.number],  type = "l", col =
"black")

       }
)

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au

-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of lily li
Sent: Wednesday, 3 August 2016 04:10
To: R mailing list
Subject: [R] how to plot annual values directly

Hi R users,

I have a dataframe, with daily precipitation data, is it possible to plot
annual mean or annual sum values directly? Thanks for your help.

df
year   month   day   precip       time
2010     1          1        0.5     2010-01-01
2010     1          2        0.8     2010-01-02
2010     1          3        1.0     2010-01-03
2010     1          4        0.9     2010-01-04
...

fig1 = ggplot()+ geom_path(data=df, aes(x=time, y= precip)
show(fig1)

	[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list