Hi: On Fri, Sep 10, 2010 at 7:16 AM, Markus Loecher <markus.loec...@gmail.com>wrote:
> Dear all, > I am struggling to modify the axis labels/ticks in a panel provided to > xyplot. > To begin with, I do not know the equivalent of the xaxt="n" directive for > panels that would set the stage for no default x axis being drawn. > My goal is to draw ticks and custom formatted labels at certain hours of > the > week. > When I execute the code below, I get an error message in the plot window > that suggests a problem with some args parameter. > > A second problem concerns the shaded rectangles I try to draw. Clearly, the > range returned by par('usr') does not correspond to the true y ranges. > > Any help would be greatly appreciated, > > Thanks, > > Markus > > PS I am using R version 2.10.0 on MACOS and the lattice package version > 0.18-3 (latest) > > ------------------------------------------------------------------------------------ > library(lattice); > > #multivariate time series, one row for each hour of the week: > Xwide = cbind.data.frame(time=as.POSIXct("2010-09-06 00:00:00 EDT") + > (0:167)*3600, Comp.1= sin(2*pi*7*(0:167)/168), Comp.2 = > cos(2*pi*7*(0:167)/168)); > #to pass this to xyplot, first need to reshape: > Xlong <- reshape(Xwide, varying = c(2:3), idvar = "time", direction="long", > timevar = "PC"); > #get descriptive shingle labels > Xlong[,"PC"] <- factor(Xlong[,"PC"], labels = paste("PC",1:2)); > A less mentally taxing approach :) library(reshape) xlong <- melt(Xwide, id = 'time') names(xlong)[2:3] <- c('PC', 'Comp') > > xyplot(Comp ~ time | PC ,data = Xlong, pane1l = WeekhourPanel, scales = > list(x=list(at = Hours24-4*3600, > labels=as.character(format(Hours24-4*3600,"%H"))))); > When attempting to run this, I got Error in xyplot.formula(Comp ~ time | PC, data = Xlong, pane1l = WeekhourPanel, : object 'Hours24' not found Attempting to pull Hours24 out of the function didn't work... > Hours24 <- seq(r[1]+12*3600, r[2], by=24*3600) Error in seq(r[1] + 12 * 3600, r[2], by = 24 * 3600) : object 'r' not found One problem is that to use Hours24 in scales, it has to be defined in the calling environment of xyplot() - in other words, it has to be defined outside the panel function and outside of xyplot() if your present code is to have any hope of working. I think I got that part figured out: in the console, type r <- range(Xwide$time) Hours24 <- seq(r[1]+12*3600, r[2], by=24*3600) I at least get a plot now by running your xyplot() function with the panel function, but all the labels are 08 on the x-axis. Here's why: format(Hours24-4*3600,"%H") [1] "08" "08" "08" "08" "08" "08" "08" This comes from the labels = part of your panel function. I got the same plot with this code (apart from adding the lines): xyplot(Comp ~ time | PC ,data = Xlong, type = 'l', scales =list(x = list(at = Hours24-4*3600, labels=as.character(format(Hours24-4*3600,"%H"))))) which indicates that something in your panel function is awry. I'd suggest starting out simply. Put both plots on the same panel using PC as a grouping variable in the long data frame. It will automatically use different colors for groups, but you can control the line color with the col.lines = argument; e.g., col.lines = c('red', 'blue'). Next, I'd work on getting the axis ticks and labels the way you want with scales. It also appears that you want to set a custom grid - my suggestion would be to do that last, after you've controlled the axis ticks and labels. Once you have that figured out, you have the kernel of your panel function. In most applications I've seen in lattice, the idea is to keep the panel function as simple as possible and pass the 'global' stuff to the function call. There's something broken in your panel function, but it's a run-time error rather than a compile-time error, so you can either debug it or try simplifying the problem (and the panel function) as much as possible. HTH, Dennis WeekhourPanel <- function(x,y,...){ > r <- range(x); > #print(r) > Hours8 <- seq(r[1], r[2], by=8*3600); > Hours24 <- seq(r[1]+12*3600, r[2], by=24*3600) > #axis.POSIXct(1, at= Hours8, format="%H"); > panel.xyplot(x,y, type="l", ...); > panel.grid(0,3); > panel.abline(v= Hours24-4*3600, lty=2, col = rgb(0,0,1,0.5)); > panel.abline(v=Hours24+6*3600, lty=2, col = rgb(0,1,0,0.5)); > bb <- par('usr') > y0 <- bb[3]; > for (i in seq(r[1], r[2], by=48*3600)) panel.rect(xleft=i, ybottom=y0, > xright=i+24*3600-1, ytop=bb[4], col = rgb(0.75,0.75,0.75,0.3), border = > NA); > panel.axis(1, at= Hours24-4*3600, > labels=as.character(format(Hours24-4*3600,"%H"))); > #panel.axis(1, at= Hours24+6*3600, labels=format(x,"%H")); > #panel.axis(3, at= Hours24, labels=format(x,"%a"), line = -1, tick = > FALSE); > > } > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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.