I want to create a panel plot using xyplot of a line graph whose x-axis is months of the year and y-axis is the average rainfall in a given month over the 6 years the data spans. There should be two levels in this panel plot: odd and even months. Creating this plot without splitting it into levels is quite straightforward (creating a for loop to compute a vector of averages) but the approach is not useful if you want to split the plots into different levels. Here is the code: dfmt <- "%d/%m/%Y" date <- seq(as.Date("01/01/2010", dfmt), as.Date("31/12/2015", dfmt), "day") month <- months(date) rainfall <- runif(2191, 0, 150) monthsOfYear <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") parity <- match(month, monthsOfYear) %% 2 # even parity = 0, odd parity = 1 z <- data.frame(rainfall, date, month, parity) The problem with using xyplot( y ~ x | f, data=z .. ) is the x and y I want to plot are not columns in z but rather some kind of statistical summary of columns.
Richard M. Heiberger
2016-Jan-15 16:06 UTC
[R] Panel plots for means of cyclical observations
## Kieran, ## I think the root problem is that you allowed the levels of month to be alphabetical. ## continuing with your example ##You need to take control of the levels with a statement like levels(z$month) z$month <- factor(z$month, levels=monthsOfYear) levels(z$month) ## now you can write something like z$month.parity <- factor(z$month, levels=unlist(matrix(levels(z$month), 6, 2, byrow=TRUE))) levels(z$month.parity) ## [1] "January" "March" "May" "July" "September" "November" ## [7] "February" "April" "June" "August" "October" "December" xyplot(rainfall ~ date | month, layout=c(2, 6), data=z) xyplot(rainfall ~ date | month.parity, layout=c(6, 2), data=z) ## I hope the modifications to deal with your summary measures will be straightforward. ## Rich On Fri, Jan 15, 2016 at 7:16 AM, Kieran <kroberts012 at gmail.com> wrote:> I want to create a panel plot using xyplot of a line graph whose > x-axis is months of the year and y-axis is the average rainfall in a > given month over the 6 years the data spans. > > There should be two levels in this panel plot: odd and even months. > > Creating this plot without splitting it into levels is quite > straightforward (creating a for loop to compute a vector of averages) > but the approach is not useful if you want to split the plots into > different levels. > > Here is the code: > > dfmt <- "%d/%m/%Y" > date <- seq(as.Date("01/01/2010", dfmt), as.Date("31/12/2015", dfmt), > "day") > month <- months(date) > rainfall <- runif(2191, 0, 150) > monthsOfYear <- c("January", "February", "March", "April", > "May", "June", "July", "August", "September", "October", > "November", "December") > > parity <- match(month, monthsOfYear) %% 2 > # even parity = 0, odd parity = 1 > > z <- data.frame(rainfall, date, month, parity) > > The problem with using xyplot( y ~ x | f, data=z .. ) is the x and y I > want to plot are not columns in z but rather some kind of statistical > summary of columns. > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
Hi Continuing on from the data.frame code for z that you supplied Using the data without summarizing first z$mth = format(date "%m") z$mth = format(date, "%m") xyplot(rainfall ~ as.numeric(mth), z, groups = yr, type = "l", auto.key = T, scales = list(x = list(at = 1:12, labels = month.abb, rot = 60)), panel = panel.superpose, panel.groups = function(x,y, ...){ panel.average(x,y, fun = mean, horizontal = FALSE, ...) } ) ## xyplot You may want to look into the zoo package as it has several date grouping functions. untested z.tom <- aggregate(rainfall ~ year +month, z, mean, na.rm = T) xyplot(rainfall ~ month, z.tom, groups = year, scales = ... , panel = panel.superpose) 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 Kieran Sent: Friday, 15 January 2016 22:16 To: R-help at r-project.org Subject: [R] Panel plots for means of cyclical observations I want to create a panel plot using xyplot of a line graph whose x-axis is months of the year and y-axis is the average rainfall in a given month over the 6 years the data spans. There should be two levels in this panel plot: odd and even months. Creating this plot without splitting it into levels is quite straightforward (creating a for loop to compute a vector of averages) but the approach is not useful if you want to split the plots into different levels. Here is the code: dfmt <- "%d/%m/%Y" date <- seq(as.Date("01/01/2010", dfmt), as.Date("31/12/2015", dfmt), "day") month <- months(date) rainfall <- runif(2191, 0, 150) monthsOfYear <- c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") parity <- match(month, monthsOfYear) %% 2 # even parity = 0, odd parity = 1 z <- data.frame(rainfall, date, month, parity) The problem with using xyplot( y ~ x | f, data=z .. ) is the x and y I want to plot are not columns in z but rather some kind of statistical summary of columns. ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.