Re: [R] lattice panel.lmline problem
On 9/10/07, Ross Darnell [EMAIL PROTECTED] wrote: Thanks Frede I didn't know about the r type. For the record, this is probably what you wanted: xyplot(... panel = panel.superpose, panel.groups = function(x, y, ...) { panel.xyplot(x, y, ...) if (length(x) 0) panel.lmline(x, y, ...) }) x[subscripts] inside the panel function doesn't give anything meaningful ('x' is already subscripted). -Deepayan Ross Darnell -Original Message- From: Frede Aakmann Tøgersen [mailto:[EMAIL PROTECTED] Sent: Mon 10-Sep-07 4:45 PM To: Ross Darnell; r-help@stat.math.ethz.ch Subject: SV: [R] lattice panel.lmline problem Why not use the more simple xyplot(total.fat~x|variable,groups=Group, data=tmp1,type=c(p,r)) ??? See ?panel.xyplot and especially the type argument of that panel function. [...] __ R-help@stat.math.ethz.ch 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.
Re: [R] labelling specific points xyplot
On 9/6/07, GOUACHE David [EMAIL PROTECTED] wrote: Hello R-helpers, I'm trying to add labels to points in xyplot graphs, but I want lo label only those points which have a certain level of my grouping variable, and have encountered a few problems. An example dataframe that goes with the following code is at the end of this message. 1st step, adding labels (from another column in the dataframe) to my chosen points : I've figures this out, but I am passing my whole dataframe as an extra argument (arg1) which I realize is probably useless, so if anybody can give me a way to work around this... xyplot(pp~nn,groups=vari,data=test,auto.key=list(space=right),arg1=test,arg2=2,arg3=loc, panel=function(x,y,groups,arg1,arg2,arg3,...) { panel.superpose(x,y,groups,...) étiq-rep(,times=length(x)) étiq[groups==arg2]-as.character(arg1[groups==arg2,arg3]) étiq-as.character(étiq) panel.text(x,y,labels=étiq,pos=1,cex=0.5,...) } ) What I would also like is to write a function that is robust for multiple panel displays or subsetting : This is not the only way, but the most transparent one I can think of: xyplot(pp ~ nn, groups = vari,data = test, labels = test$loc, do.label = (test$vari == 2), auto.key=list(space=right), panel = function(x, y, groups, subscripts, labels, do.labels, ...) { panel.xyplot(x, y, groups = groups, subscripts = subscripts, ...) labels - labels[subscripts] do.labels - do.labels[subscripts] panel.text(x[do.labels], y[do.labels], labels = labels[do.labels], pos = 1, cex=0.5, ...) }) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] [Lattice] Incrase the height of strips in Trellis plots
On 9/5/07, Frede Aakmann Tøgersen [EMAIL PROTECTED] wrote: This should give you something close to what you want: xyplot(Petal.Length ~ Petal.Width | Species, iris, strip = strip.custom(par.strip.text = list(cex = 2)), par.settings = list(layout.heights=list(strip=1.45))) The par.settings argument alters locally the default par settings of lattice plots, see e.g. ?trellis.par.get and the ?xyplot about the par.settings argument. Another possibility that predates par.settings (but is basically equivalent) is xyplot(Petal.Length ~ Petal.Width | Species, iris, par.strip.text = list(lines = 2, cex = 2)) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Multiple xyplots
On 9/5/07, Tom Wright [EMAIL PROTECTED] wrote: Hi everyone, I'm hoping you can give me some pointers. I have a requirement to draw multiple (103) xy line plots onto one output device. Ideally the plots should be displayed in a hexagonal grid (example at www.maladmin.com/example.jpg). I can calculate the locations for each waveform but am wondering how to create multiple plotting areas. I have come accross references to a package grid (which doesn't seem to be in my CRAN mirror probability.ca) and lattice but I'm not sure if I'm on the correct lines. Any advice gratefully received. grid seems like the right choice to me (it comes bundled with R, so it's not available as a separate package). Here's an example that may give you a few hints: library(grid) grid.newpage() for (i in 1:30) grid.lines(x = 0:20/21, y = sin(70 * runif(1) * 0:20/21), vp = viewport(x = runif(1), y = runif(1), height = 0.05, width = 0.05)) See the package documentation, or Paul Murrell's book R Graphics for more. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: key with expression function
On 9/5/07, Folkes, Michael [EMAIL PROTECTED] wrote: HI all, I'm trying (unsuccessfully) to add the degree symbol to each line of text in my legend (within xyplot). Here is the line of code, which fails to interpret the expression function: auto.key =list(points = FALSE,text=paste(levels(as.factor(divertSST2$temp)),expression(degree)). ..), I just get: 7 degree 8 degree 9 degree That's because paste(foo, expression(degree)) [1] foo degree If I place 'expression' outside or just after the paste function it also doesn't work. auto.key = list(text = expression(paste(foo, degree))) should work. I think the problem is that you want a vector of expressions, and that's a bit harder to get. I'm not sure what the best solution is, but if everything else fails, you could try using parse(text=) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] lattice xyplot with bty=l
On 9/4/07, Patrick Drechsler [EMAIL PROTECTED] wrote: Hi, what is the correct way of removing the top and right axes completely from a lattice xyplot? I would like to have a plot similar to using the bty=l option for traditional plots. There is no direct analog (and I think it would be weird in a multipanel plot). Combining a few different features, you can do: library(grid) xyplot(1:10 ~ 1:10, scales = list(col = black, tck = c(1, 0)), par.settings = list(axis.line = list(col = transparent)), axis = function(side, ...) { if (side == left) grid.lines(x = c(0, 0), y = c(0, 1), default.units = npc) else if (side == bottom) grid.lines(x = c(0, 1), y = c(0, 0), default.units = npc) axis.default(side = side, ...) }) -Deepayan An example: --8---cut here---start-8--- rm(list=c(ls())) library(lattice) y - 1:10 x - y ## Traditional plot: plot(x, y, bty = l) ## Lattice plot 1: xyplot(y ~ x, panel = function(x, y, ...){ panel.xyplot(x, y, ...) panel.axis(side = c(left, bottom)) } ) ## Lattice plot 2: xyplot(y ~ x, ## This just tries to remove all y axes: scales = list(y = list(draw = FALSE)) ) --8---cut here---end---8--- Thankful for any pointers, Patrick __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: panel superpose with groups
On 9/4/07, Folkes, Michael [EMAIL PROTECTED] wrote: The example code below allows the plotting of three different groups per panel. I can't fathom how to write the panel function to add an additional line for each group, which in this case is just the mean Y value for each group within each panel. (i.e. there'd be six lines per panel.) Spent all day working on it and searching the archives to no avail! Yikes. Any help would be greatly appreciated! xyplot(yvar~year|week2,data=df,layout = c(4, 5), as.table=TRUE, type='l', groups = temp , panel = panel.superpose, panel.groups = function(x, y, ..., lty) { panel.xyplot(x, y, ..., lty = lty) #panel.lines(x, rep(mean(y),length(x)), lty=3, ...) # or panel.abline(h = mean(y), lty=3, ...) }) (see ?panel.superpose for explanation) -Deepayan Michael Folkes # #This builds fake dataset years-2000:2006 weeks-1:20 yr-rep(years,rep(length(weeks)*6,length(years))) wk-rep(weeks,rep(6,length(weeks))) temp-rep(4:9,length(years)*length(weeks)) yvar-round(rnorm(length(years)*length(weeks)*6,mean=30,sd=4),0) xvar-(rnorm(length(years)*length(weeks)*6)+5)/10 df-data.frame(year=yr,week=wk,temp=temp, yvar=yvar, xvar=xvar) # library(lattice) df$year2-as.factor(df$year) df$week2-as.factor(df$week) df-df[df$temp %in% c(5,7,9),] xyplot(yvar~year|week2,data=df,layout = c(4, 5), as.table=TRUE, type='l', groups=temp , panel = function(x, y,groups, ...) { panel.superpose(x,y,groups,...) panel.xyplot(x,rep(mean(y),length(x)),type='l',lty=3) #- only generates the panel mean } ) ___ Michael Folkes Salmon Stock Assessment Canadian Dept. of Fisheries Oceans Pacific Biological Station 3190 Hammond Bay Rd. Nanaimo, B.C., Canada V9T-6N7 Ph (250) 756-7264 Fax (250) 756-7053 [EMAIL PROTECTED] [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: panel superpose with groups
On 9/4/07, Folkes, Michael [EMAIL PROTECTED] wrote: Thank you again Deepayan. I was failing to grasp that I could use panel.groups as a function. But additionally it's still not intuitive to me where and when I should use ... to pass arguments on. In most cases, it's used to pass on graphical parameters, so I tend to have the ... by default (which means you can supply graphical parameters directly as arguments to xyplot). This is particularly non-trivial in panel.groups, because panel.superpose does a lot of work to replicate the parameters and pass on a different one for each call to panel.groups. Additionally, as to why the panel.group function needs to pass the 'lty' argument isn't terribly clear to me! Try not having it and you will find out. Basically, if you don't have it, the explicit lty=3 in the call to panel.abline will conflict with the lty passed on through ... -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] plotting predicted curves with log scale in lattice
On 9/3/07, Ken Knoblauch [EMAIL PROTECTED] wrote: Hi, I was taken off guard by the following behavior in a lattice plot. I frequently want to add a predicted curve defined at more points than in the formula expression of xyplot. There have been numerous examples of how to do this on r-help, but I still often struggle to make this work. I just realized that specifying one of the axes on a log scale does not guarantee that the added data for a curve will automatically take that into account. I don't know if this should be called a bug, More like a possibly desirable feature that's missing. I haven't picked up an indication that would lead me to expect this in the documentation. Yes, the documentation is a bit vague. I've changed it to the following, which is hopefully clearer. 'log' Controls whether the corresponding variable ('x' or 'y') will be log transformed before being passed to the panel function. Defaults to 'FALSE', in which case the data are not transformed. Other possible values are any number that works as a base for taking logarithm, 'TRUE' (which is equivalent to 10), and 'e' (for the natural logarithm). As a side effect, the corresponding axis is labeled differently. Note that this is a transformation of the data, not the axes. Other than the axis labeling, using this feature is no different than transforming the data in the formula; e.g., 'scales=list(x = list(log = 2))' is equivalent to 'y ~ log2(x)'. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Bug?
On 8/31/07, Uwe Ligges [EMAIL PROTECTED] wrote: Johanna Hasmats wrote: Hi! How can I get around in R 2.5.1 in Windows: Error in strsplit(linebuffer, ) : object linebuffer not found Why should this be a bug in R, if you have no object named linebuffer in the environments that are on the search path. This sounds like it could have something to do with the command completion mechanism through package rcompgen (which does use an internal variable called 'linebuffer'). Since this doesn't seem to be a common problem, you need to give us instructions on how to reproduce it. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Additions to xyplot (lattice)? - legend, ticks, axis label size, text
On 8/31/07, Dave Hewitt [EMAIL PROTECTED] wrote: At 12:47 PM 8/30/2007 -0700, [EMAIL PROTECTED] wrote: On 8/30/07, Dave Hewitt [EMAIL PROTECTED] wrote: Thank you very much... simple and easy fixes. Threefinal queries: (1) I need to make a little more room on the left for the larger axis label. I tried 'mex' in the list for ylab but that was ignored. It's possible, but shouldn't be necessary. Can you provide a reproducible example which shows why you need this? I definitely can if needed, but... it's not that the axis label doesn't fit, it's just that I'd like a little more white space. So it may not match up with standard rules, but it's for a specific purpose. Please let me know how to adjust it. Add something like par.settings = list(layout.widths = list(ylab = 2)) Also, might this interact with my choice of plot size prior to production, with: win.graph(width = 10, height = 7) Do you want it to interact? I don't think width and height matter, but pointsize (or whatever its equivalent for win.graph) might. (2) Is it possible to place a tick at each year on the x-axis, but only label every other year? Not in those terms, but you can explicitly specify labels as scales = list(x = list(at = c(1, 2, 3), labels = c(a, , c))) etc. So no ticks without labels, as specified by the user? The above generates only ticks with labels. Yes, but the second label is blank (). Why is that not enough? For finer control of tick marks and labels, you can specify a custom function that computes these. See ?xscale.components.default, but this is not very well documented and you will have to look into the sources a bit. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Need help putting histograms on the diagonal of a splom plot
On 8/30/07, Marc Paterno [EMAIL PROTECTED] wrote: Hello, I am in need of help in putting histograms on the diagonal of a plot produced with splom(). The plot matrix I am trying to produce is to have standard scatterplots in the upper-left triangle, contour plots in the lower-right triangle, and histograms on the diagonal. I have a function that does the first two, but the histograms on the diagonal has been beyond my ability. Here is my function: require(lattice) require(MASS) my.plot = function(data) { splom( ~data , lower.panel=function(x,y, ...) { xy=kde2d(x,y) xy.tr=con2tr(xy) panel.contourplot( xy.tr$x , xy.tr$y , xy.tr$z , subscripts=seq(nrow(xy.tr)) , contour=TRUE , region=TRUE , labels = FALSE , col.regions = terrain.colors ) } , upper.panel=function(x,y, ...) { panel.grid(-1,-1) panel.xyplot(x,y, cex=0.5) } #, diag.panel=function(x, ...) # { #panel.histogram(x, ...) # } ) } It can be called, for example, with: my.plot(subset(iris, select = Sepal.Length:Petal.Width)) (the subset is necessary to get rid of a variable that is a factor; my function can not deal with factors). I have commented out my best guess at the code needed to produce the histograms along the diagonal, which fails. Well, basically the y-axis range of the diagonal panels are not right. What you want is simpler if you are happy with a density estimate: my.plot = function(data) { splom( ~data #, lower.panel=... #, upper.panel=... , diag.panel = function(x, ...) { yrng - current.panel.limits()$ylim d - density(x) d$y - with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y) ) panel.lines(d) }) } my.plot(iris[1:4]) For a histogram, things are a bit more complicated, but still easy enough: my.plot = function(data) { splom( ~data #, lower.panel=... #, upper.panel=... , diag.panel = function(x, ...) { yrng - current.panel.limits()$ylim d - density(x) d$y - with(d, yrng[1] + 0.95 * diff(yrng) * y / max(y) ) panel.lines(d) }) } -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice:can't subset in panel function using other variables
On 8/30/07, Folkes, Michael [EMAIL PROTECTED] wrote: I've succeeded doing a subset within the panel function of xyplot - if I'm subsetting based on either the value of 'x' or 'y' (e.g. below). However, I wish to subset based on the value of another variable and colour that one plotted point. It's not working. Either it doesn't plot the coloured data point, or if I sort the data differently it colours one datapoint, but the wrong one. I assume this means it's not getting the right subscripts? Finally I can sort of see the light as if I remove the conditioning variable (week) and subset before the xyplot (e.g. week==1) to get just one panel, it plots the correct data including the correct single red point. Where am I erring? ___ print(xyplot(yval~xval|week,data=mydata, panel=function(x,y,subscripts){ #panel.xyplot(x,y,type='p',col=1,cex=.5) panel.xyplot(x[y=40],y[y=40],type='p',col=2,cex=.5) # -this works panel.xyplot(x[mydata$yr==2005],y[mydata$yr==2005],type='p',pch=16,col=2,cex=.5) # -sometimes this won't work or it colours wrong datapoint })) ___ Why not xyplot(yval~xval|week,data=mydata, subset = yval 40) or xyplot(yval~xval|week,data=mydata, subset = yr==2005) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Sweave rendering of simple character
On 8/31/07, Ken Knoblauch [EMAIL PROTECTED] wrote: Hi, When I compile the construction \begin{Scode}{eval=FALSE} ?HSP \end{Scode} with Sweave and latex, it outputs in the pdf as, `?` (HSP) which is not incorrect but a bit more formal than I wanted for demonstrating the use of the help shortcut. I would like the output to look like, ?HSP but I can't seem to make this work. I'm not familiar with the Scode environment, but eval=FALSE,keep.source=TRUE= ?HSP @ does what you want. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
On 8/31/07, Christof Bigler [EMAIL PROTECTED] wrote: The suggestions by Deepayan Sarkar and Hadley Wickham work for that case, but I get into troubles when I try to draw e.g. a panel for A and B: xyplot(y ~ x | f , groups=g, data=tmp,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp$f),points=FALSE,lines=TRUE)) Similarly for xyplot(y ~ x | f , groups=f, ...) Let's assume that we use a slightly more complex dataframe, where we have different individuals a, ..., f (grouping variable h) with 1 or 2 series of measurements per individual (grouping variable g). The individuals are grouped as A or B (grouping variable f) according to the distribution of the values in the variable y. set.seed(1) tmp2 - data.frame( y=c(rnorm(10,0,1),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1)), x=1:10, f=c(rep(A,10),rep(B,10),rep(B,10),rep(B,10),rep(A,10),rep(A,10),rep(A,10),rep(B,10),rep(A,10)), g=c(rep(1,10),rep(2,10),rep(3,10),rep(4,10),rep(5,10),rep(6,10),rep(7,10),rep(8,10),rep(9,10)), h=c(rep(a,10),rep(b,10),rep(b,10),rep(c,10),rep(d,10),rep(d,10),rep(e,10),rep(f,10),rep(e,10))) Again, the line colors in the following plot are correct: xyplot(y ~ x | g , groups=f, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, points=FALSE,lines=TRUE)) But, if I want to show e.g. one panel per individual, the assignment of the colors is not correct: xyplot(y ~ x | h , groups=g, data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Any suggestions? This works (only the 'groups' argument is different from your call): xyplot(y ~ x | h , groups=interaction(f, g), data=tmp2,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp2$f), points=FALSE,lines=TRUE)) Note that unlike ggplot, you cannot (easily) use a different variable for grouping and coloring. Fortunately, the levels of interaction(f, g) are such that your colors (red, blue) are repeated in the right order, so you don't need to do much extra work. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice:can't subset in panel function using other variables
On 8/31/07, Folkes, Michael [EMAIL PROTECTED] wrote: Thanks Deepayan for your response. The first subset you suggest was just a test for me and not what I wanted. I can't do your second suggested subset action as I wish to plot all the panel data, but then add a coloured datapoint for just one year (see example code). I think I have found my problem but don't know how to solve it. The subscripts of data going into each panel are almost always the same length, except maybe one or two panels have 1 less datapoint. I've attached a script that builds a quick dataset and plots what I was aiming for. It works great. If you then remove one line of data from the DF (using df-df[-1,] in the script), the plotting goes awry. Any suggestions about dealing with unequal data lengths for panel function subsetting? If your goal is to highlight one particular year, why not use something like xyplot(yvar~xvar|week2,data=df,layout = c(4, 5), as.table=TRUE, groups = (year == 2005), col = 1, pch = c(1, 16)) ? Your code doesn't work because you don't seem to understand what 'subscripts' is supposed to be (either that, or you are confusing yourself with multiple indices). Here's a version with the correct usage: xyplot(yvar~xvar|week2,data=df,layout = c(4, 5), as.table=TRUE, panel = function(x, y, subscripts, ...) { highlight - (df$year == 2005) highlight.panel - highlight[subscripts] panel.xyplot(x, y, type='p', col=1, cex=.5) panel.xyplot(x[highlight.panel], y[highlight.panel], type='p', pch=1, col=3, cex=1.5) }) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] xyplot() groups scope issue
On 8/30/07, Mike Lawrence [EMAIL PROTECTED] wrote: Hi all, Tinkering with a wrapper for xyplot that will help me plot a bunch of plots in a data analysis I'm doing and I ran into an odd error that I'm guessing is a scope issue. Here's a very simple version of the code: It's indeed a scoping issue. For writing wrappers, the best solution I've been able to come up with involves match.call() and eval.parent(). See lattice:::dotplot.formula for a template. If you really want to do it your way, try changing the environment of the formula: do.xyplot = function( plot.formula, data.frame, plot.groups){ print(plot.groups) environment(plot.formula) - environment() # new xyplot( plot.formula ,data = data.frame ,groups = eval(plot.groups) ) } -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Assigning line colors in xyplot
On 8/30/07, Christof Bigler [EMAIL PROTECTED] wrote: Hi, I have a dataframe containing data from individuals 1, ..., 12 (grouping variable g in the data frame below), which belong either to A or B (grouping variable f): set.seed(1) tmp - data.frame( y=c(rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2),rnorm(10,0,1),rnorm(10,4,2)), x=1:10, f=c(rep(A,10),rep(B,10),rep(A,10),rep(B,10),rep(A,10),rep(B,10),rep(A,10),rep(B,10),rep(A,10),rep(B,10),rep(A,10),rep(B,10)), g=c(rep(3,10),rep(2,10),rep(1,10),rep(4,10),rep(5,10),rep(6,10),rep(8,10),rep(7,10),rep(9,10),rep(11,10),rep(12,10),rep(10,10))) I would like to draw line plots using the function xyplot: library(lattice) xyplot(y ~ x | g , groups=g, data=tmp,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, text=levels(tmp$f),points=FALSE,lines=TRUE)) As it is, the colors are recycled alternately in the order the individuals appear in the plot (1, 10, 11, 12, 2, ..., 9). How can I assign the red color to all individuals of group A and the blue color to all individuals of group B? Why not simply use f as the grouping variable rather than g: xyplot(y ~ x | g , groups=f, data=tmp,type=l, par.settings=list(superpose.line=list(col=c(red,blue))), auto.key=list(space=top, points=FALSE,lines=TRUE)) ? Or am I missing something? -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Additions to xyplot (lattice)? - legend, ticks, axis label size, text
On 8/30/07, Dave Hewitt [EMAIL PROTECTED] wrote: I have created an xyplot of a time series with the following code... win.graph(width = 10, height = 7) panel1 = function(x, y) { panel.loess(x, y, lwd=2.5, span=0.5, col=gray) panel.xyplot(x, y, pch=19, col=blue, cex=1.25) } xyplot(oneplusdensity ~ year, data=figdata, aspect=fill, cex=1.5, xlab=NULL, ylab=expression(Crabs per 1,000 m^2), xlim=c(1964, 2007), ylim=c(-2, 62), scales=list (x=list(tick.number=41, cex=0.8, rot=90), y= list(tick.number=7, cex=1.2)), panel=panel1) I want to do the following things: (1) Remove the ticks on the top and right boundaries scales = list(tck = c(1, 0), x = ..., y = ...) (2) Increase the size of the y-axis label to be larger than the tick labels ylab = list( expression(...), cex = 2) (3) Add a legend with the points and lowess line See the entry for 'key' in ?xyplot. As a starting point, key = list(text = list(loess), lines = list(lwd=2.5, span=0.5, col=gray), text = list(points), points = list(pch=19, col=blue, cex=1.25)) (4) Add some annotation (text) in the lower left corner ('text' for plot() did not work) Use panel.text() instead inside your panel function. -Deepayan I'm relatively new to lattice and spent a few hours with the manual and other help pages. I've begun to wonder if a regular old plot() for this would be better. Appreciate any redirection or suggestions. __ R-help@stat.math.ethz.ch 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.
Re: [R] Additions to xyplot (lattice)? - legend, ticks, axis label size, text
On 8/30/07, Dave Hewitt [EMAIL PROTECTED] wrote: Thank you very much... simple and easy fixes. Threefinal queries: (1) I need to make a little more room on the left for the larger axis label. I tried 'mex' in the list for ylab but that was ignored. It's possible, but shouldn't be necessary. Can you provide a reproducible example which shows why you need this? (2) Is it possible to place a tick at each year on the x-axis, but only label every other year? Not in those terms, but you can explicitly specify labels as scales = list(x = list(at = c(1, 2, 3), labels = c(a, , c))) etc. (3) Can the legend pieces be broken into two lines (one for loess and one for points)? Again, not in those terms, but you can fake it using key = list(text = list(c(loess, points)), lines = list(lwd = 2.5, col = c(gray, blue), cex = 1.25, pch = 19, type = c(p, l))) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] customizing the color and point shape for each line drawn using lattice's xyplot
On 8/29/07, Gen [EMAIL PROTECTED] wrote: Description of what I am trying to do: I am using the xyplot code below to plot the variable MeanBxg against the variable PercentVarExplained for all 9 possible combinations of variables bdg and bdx. Within each of these 9 scenarios I am plotting a separate line for each of up to 9 different methods that I used to estimate the variable MeanBxg. These methods are identified by the numeric variable called Method. Giving me one plot with 9 figures and each of the figures contains 9 lines. My problem arises because I would like to repeat the creation of this plot 8 times, in each instance only a subset (eg 6) of the 9 methods are used (a different subset each time). What I can't figure out: I would like to learn how to specify the exact line color that corresponds to each method such that Method==1 will always be represented by the same color (in every plot that it appears in). Where two methods that I used were of the same family of methods (say method==1 and method==2 made the same assumptions about the data) I would like to, if possible, represent the two methods using the same color and distinguish them by the symbol used to represent points on the line. My code as it currently stands: xyplot(MeanBxg ~ PercentVarExplained | bdg.f * bdx.f, data=common, groups=common$Method.f, type=l, subset= Method!=4, auto.key=T) As the code is, the default colors assigned are repeated causing different methods to be represented by the same color with no way to distinguish them (I have not succeeded in plotting lines and points simultaneously). Side question: When I subset the data to particular methods, is there a way to remove the excluded methods from the key as well? (in my code Method is a numeric variable, and Method.f corresponds to the lengthy descriptions of each method for the purpose of the key) It's not clear what you actually want, and you haven't given us a reproducible example. Some general comments: 1. try type = o for both points and lines, and auto.key = list(points = TRUE, lines = TRUE) if you meant this for the legend. 2. The number of distinct colors is 7 by default, after which they get recycled. You can set the colors by changing the superpose.line$col setting (see ?trellis.par.set and the entry for 'par.settings' in ?xyplot) 3. The number of colors used is related to the number of levels of 'groups'. If you want fewer colors (or not have unused colors in the legend), you need to make sure your 'groups' variable has a suitably reduced set of levels. There is no cute way to do this. The long way is subdata - subset(common, Method != 4) subdata$Method.f - subdata$Method.f[, drop = TRUE] -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] FAQ 7.x when 7 does not exist. Useability question
On 8/23/07, Duncan Murdoch [EMAIL PROTECTED] wrote: On 8/23/2007 11:28 AM, Prof Brian Ripley wrote: On Thu, 23 Aug 2007, John Kane wrote: The FAQ Section 7 is a very useful place for new users to find out any number of R idiosycracies. However there is no numbering on the FAQ Table of Content or on the Sections Tables of Contents. Hmm, doc/FAQ does have a numbered table of contents and numbered sections and doc/manual/R-FAQ.html does have numbered sections and my browser's search finds 7.10 straight away. I think the suggestion is to change the contents lists in HTML from ul lists to ol lists. Then one would see 1. Introduction 2. R Basics 3. R and S 4. R Web Interfaces 5. R Add-On Packages 6. R and Emacs 7. R Miscellanea 8. R Programming 9. R Bugs 10. Acknowledgments instead of * Introduction * R Basics * R and S * R Web Interfaces * R Add-On Packages * R and Emacs * R Miscellanea * R Programming * R Bugs * Acknowledgments in a browser, and I agree that would be preferable (assuming the numbering is consistent with what we get in the other formats). However, I don't see how to tell makeinfo --html to do this. Adding --number-sections isn't enough. A simple CSS hack is to have ul{ list-style-type: decimal; } in the style. The result can be seen in http://dsarkar.fhcrc.org/R/RFAQ-1.png A more sophisticated hack is to have something like --- body{ counter-reset: chapter; counter-reset: section; } h2.chapter { counter-increment: chapter; counter-reset: section; } ul { list-style-type: none; } li:before { counter-increment: section; content: counter(chapter) . counter(section) ; } - which results in http://dsarkar.fhcrc.org/R/RFAQ-2.png The only problem here is that there is no way to distinguish between the chapter listing and the section listings (both are ul class=menu). If that could be made to have a different class, the chapter listing could be improved. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] small issue with densityplot
On 8/21/07, Horace Tso [EMAIL PROTECTED] wrote: Hi folks, This is really minor but to someone not familiar with the various tentacles of the lmer package it could be really annoying. I was trying to plot the posterior density of the fixed effect parameters of a lmer model, hr.mcmc = mcmcsamp(hr.lmer, n=5) densityplot(hr.mcmc, plot.points=F) There is this error, Error in densityplot(hr.mcmc, plot.points = F) : no applicable method for densityplot It kind of smells like something I've come across before. So I checked the mcmcsamp help page, and alas, the example suggests that the package coda is needed. From the help page of densityplot alone, there is no way one could figure out this dependency. It says, together with histogram, it is part of lattice. Could the function author *please* make clarification in future editions of lattice. There is nothing to clarify. densityplot() is a generic function, and it is not possible for the author of the generic function to anticipate and document all possible methods, especially those in other packages. I would say that since you are using mcmcsamp(), it's perfectly reasonable to expect you to look at its help page to figure out what you can do with the results. What gave you the idea that densityplot would work on the result of mcmcsamp in the first place? -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Stacked Bar
On 8/21/07, Stephen Tucker [EMAIL PROTECTED] wrote: I think you want to use the 'density' argument. For example: barplot(1:5,col=1) legend(topleft,fill=1,legend=text,cex=1.2) par(new=TRUE) barplot(1:5,density=5,col=2) legend(topleft,fill=2,density=20,legend=text,bty=n,cex=1.2) (if you wanted to overlay solid colors with hatching) Here's the lattice alternative of the bar graph, though the help page says 'density' is currently unimplemented (Package lattice version 0.16-2). Yes, and that's unlikely to change unless grid begins to support it. To get the legend into columns, I followed the suggestion described here: http://tolstoy.newcastle.edu.au/R/help/05/04/2529.html Essentially I use mapply() and the line following to create a list with alternating 'text' and 'rect' arguments (3 times to get 3 columns). === x - matrix(1:75, ncol= 5) dimnames(x)[[2]] - paste(Method, 1:5, sep=) dimnames(x)[[1]] - paste(Row, 1:15, sep=) u - mapply(function(x,y) list(text=list(lab=x),rect=list(col=y)), x = as.data.frame(matrix(levels(as.data.frame.table(x)$Var1), ncol=3)), y = as.data.frame(matrix(rainbow(nrow(x)), ncol=3)), SIMPLIFY=FALSE) key - c(rep=FALSE,space=bottom,unlist(names-(u,NULL),rec=FALSE)) barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, col=rainbow(nrow(x)),density=5, key = key ) === A more transparent solution (IMO) is something like barchart(Freq ~ Var2, data = as.data.frame.table(x), groups = Var1, stack = TRUE, par.settings = list(superpose.polygon = list(col=rainbow(nrow(x, auto.key = list(space = right, columns = 2) ) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] small issue with densityplot
On 8/21/07, Douglas Bates [EMAIL PROTECTED] wrote: On 8/21/07, Horace Tso [EMAIL PROTECTED] wrote: Deepayan, you're right. Now I realize anyone could write a densityplot function to apply on a different class of objects. I guess I should write to the author of lme4 which from what I could see does not describe a densityplot. By the way, it is used in Baayen, Davidson, and Bates (2006). You're right. It's my bad for not documenting that the coda package is required for plots of the results of mcmcsamp. I was trying to piggy-back on the work that the authors of the coda package had done on diagnostics, etc. It was actually Deepayan and I who added the lattice-based plots to the coda package for exactly this purpose (did you remember that, Deepayan?). I do remember that, but my point remains: (1) densityplot is a generic in lattice and documented as such. (2) mcmcsamp produces an object of class mcmc defined in coda, and this is documented in help(mcmcsamp), which even uses densityplot for its examples after attaching coda. (3) densityplot.mcmc is documented in coda. which all seems to be as it should be (except maybe ?densityplot.mcmc should additionally have an alias for densityplot). The only confusing aspect I see is that lme4 does not require or import coda, even though mcmcsamp produces objects of a class that is supposedly from coda. I realize that ownership of S3 classes is not well defined, but wouldn't that be the logical thing to do? The original problem wouldn't have come up if this were true. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] to combine bwplot + srt option?
On 8/16/07, KOITA Lassana - STAC/ACE [EMAIL PROTECTED] wrote: Thank you for you quite and useful explanation. And do know how to sort them by median? See ?reorder.factor Note that traditional practice with bwplot() is to have the categorical variable on the y-axis, in which case the default string rotation is optimal. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] xyplot() with segments() superposed?
On 8/11/07, Yuelin Li [EMAIL PROTECTED] wrote: In the hypothetical example below, how do I add two segments() into the two panels, respectively? Say segments(x0=5, y0=10, x1=5, y1=20) on the left and segments(x0=15, y0=-10, x1=15, y1=-2) on the right? Many thanks in advance, Yuelin Li. ps. part of the code came from a solution given by Deepayan Sarkar. --- library(lattice) set.seed(12345) x - 0:20 y.male.obs - - 1.2 * x + 22 + rnorm(length(x), sd = 3) y.male.prd - - 1.2 * x + 22 y.fema.obs - - 2.2 * x + 30 + rnorm(length(x), sd = 2) y.fema.prd - - 2.2 * x + 30 tdat - data.frame(x = rep(x, 8), y = rep(c(y.male.obs, y.male.prd, y.fema.obs, y.fema.prd), 2), sex = rep(rep(c(m, f), each = 2*length(x)), 2), cohort = rep(c(1970, 1980), each = 4*length(x)), source = rep(rep(c(obs, prd), each = length(x)), 4) ) xyplot(y ~ x | as.factor(cohort), data = tdat, groups = interaction(sex, source), type = c(p, p, l, l), distribute.type = TRUE) If this is a one-off requirement, the simplest solution is: xyplot(y ~ x | as.factor(cohort), data = tdat, groups = interaction(sex, source), type = c(p, p, l, l), distribute.type = TRUE, panel = function(...) { panel.xyplot(...) switch(panel.number(), panel.segments(x0=5, y0=10, x1=5, y1=20), panel.segments(x0=15, y0=-10, x1=15, y1=-2)) }) This is not generalizable, but you haven't told us your general use case. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] xyplot() with segments() superposed?
On 8/13/07, Yuelin Li [EMAIL PROTECTED] wrote: Thanks for the quick response. The general use is like the following. Suppose I have another variable, tkmark, which for cohort 1970 is coded 1.0 at x = c(7, 19) and 0 otherwise. For cohort 1980, tkmark is coded 1.0 at x = c(2, 11, 12, 19) and 0 otherwise. For each cohort, I want to mark with a short vertical bar the observed Y at (x[tkmark == 1 source == obs], y[tkmark == 1 source == obs]). I may have up to 65 different cohorts. Different cohorts have different numbers of tkmark positions (some have none). The real data come from a computerized cigarette smoking reduction program. I want to plot each smoker's tapering of cigarettes (y) over days (x), and mark the quit attempts. Some participants try to quit on days 3, 7, and 17 but resume smoking the next day, some follow the programmed tapering exactly with no interim quit attempts, and some ignored the tapering program. I want to plot each participant's **scheduled** and **observed** smoking pattern over days and mark the quit attempts. Hope this is clear. Sounds like you need to pass your 'tkmarks' variable on to the panel function and then use 'subscripts' inside your panel function. Read the entry on 'panel' in ?xyplot. Also, there's a fairly long thread on a similar problem which you might find useful. It starts with https://stat.ethz.ch/pipermail/r-help/2007-July/136188.html -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] 'From' and 'to' arguments in panel.abline
On 8/13/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, The help does not give much details on the use of the arguments 'from' and 'to' in the panel.abline function. Not surprising, since panel.abline doesn't actually have arguments called 'from' and 'to'. I have looked in the archives but did not find how to implement them. My different tries failed miserably. E.g, the following code doesn't seem to work, in a sense that the line is not limited to the (0,10) range. Do these arguments really apply to panel.abline? No, they do not. It is common to have many functions documented in one help page, and you need to look at the usage section (near the top) to figure out which arguments are releant for which functions. The help page has: Usage: panel.abline(a = NULL, b = 0, h = NULL, v = NULL, reg = NULL, coef = NULL, col, col.line, lty, lwd, type, ...) panel.curve(expr, from, to, n = 101, curve.type = l, col, lty, lwd, type, ...) etc. If you want to limit the range, use panel.curve, e.g. panel.curve(0 + 1 * x,from=0,to=10) -Deepayan If so, how should they be specified? xy-data.frame(x-0.1:10,y-0.1:10) xyplot(y~x,data=xy, panel = function(x, y, ...){ panel.abline(a=0,b=1,from=0,to=10) panel.xyplot(x,y) }) Thanks you in advance for your help. Sebastien __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Question regarding QT device
On 8/5/07, Saptarshi Guha [EMAIL PROTECTED] wrote: Hi, After a few modifications in the makefiles, I successfully compiled the Qt device (written by Deepayan Sirkar) for OS X 10.4.9 on a Powerbook. Cool, can you send me the modifications? I haven't managed to compile qtutils on OS X yet (not that I've tried too hard). However when loading into R If i remove this line from zzz.R in qtutils/R grDevices::deviceIsInteractive(QT) and then install library(qtutils) loads fine and the QT() calls returns a QT window, however, if i switch to another application and then switch back to the R GUI, the menubar has disappeared. I can't test this, but Qt is designed to run as the main application, so it's possible that it is overriding whatever the GUI is doing. If I do not remove the line grDevices::deviceIsInteractive(QT) the following error appears an qtutils does not load Error : 'deviceIsInteractive' is not an exported object from 'namespace:grDevices' Error : .onLoad failed in 'loadNamespace' for 'qtutils' Error: package/namespace load failed for 'qtutils' Could anyone provide some pointers to get that deviceIsInteractive to work? What's your R version? Do you see this in 2.5.1? -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] using loops to create multiple images
On 8/4/07, Donatas G. [EMAIL PROTECTED] wrote: I have a data.frame with ~100 columns and I need a barplot for each column produced and saved in some directory. I am not sure it is possible - so please help me. this is my loop that does not work... vars - list (substitute (G01_01), substitute (G01_02), substitute (G01_03), substitute (G01_04)) results - data.frame ('Variable Name'=rep (NA, length (vars)), check.names=FALSE) for (i in 1:length (vars)) { barplot(table(i),xlab=i,ylab=Nuomonės) dev.copy(png, filename=/my/dir/barplot.i.png, height=600, width=600) dev.off() } questions: Is it possible to use the i somewhere _within_ a file name? (like it is possible in other programming or scripting languages?) (Yes, but) Why are you using dev.copy? See ?png, in particular how the ``page number'' can be encoded in the 'filename' argument for multi-page output. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Background color of symbols in xyplot
On 8/2/07, Sébastien [EMAIL PROTECTED] wrote: Dear R-users, I am using plot symbols given by pch=21:25 in a xyplot. The background color of these symbols can be defined by 'fill' in the panel argument, however I have a hard time to find how to define the same background color in the key. I tried different options like using the auto.key argument and modifying the par.settings, or using normal key argument plus 'fill', 'bg' or 'background'... nothing seems to do I want. What should I change in the following code to make it work ? key=list(space=bottom, points = list(pch = 21:25, col = 32, bg=15), text=list(mylegend)), For starters, upgrade to the latest version of R and lattice. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Xyplot - adding model lines to plotted points
On 8/2/07, Sandeman, L. R. [EMAIL PROTECTED] wrote: Hello, I have written code to plot an xyplot as follows: library(lattice) xyplot(len~ageJan1|as.factor(cohort),groups=sex,as.table=T,strip=strip.c ustom(bg='white',fg='white'),data=dat, xlab=Age (January 1st),ylab=Length (cm),main=Linear models for male and female cod, by cohort,type='p', lwd=1.5,auto.key=list(text=c(Male,Female),points=F,rectangles=F,line s=T)) I have fitted a linear model to the same data (for each sex in each cohort(year)). I would like to add the fitted models to the existing plot (one line for male and one for female, where each panel is a separate cohort - as in the above code). I also want to do this with non-linear models if possible. I have trawled R help, and it seems that panel.superpose may be one method of attempting this, however, I am unable to produce any working code. My dataset is too large to put in as an example, but it is in the basic form as below (where sex is 1 for male and 5 for female). len age fitted_model cohort sex 1 24 2 30.05771 1977 1 2 31 3 36.64122 1977 1 3 22 2 27.73938 1978 1 4 34 3 36.64122 1977 1 5 22 2 27.73938 1978 1 6 31 3 36.64122 1977 1 7 34 3 36.64122 1977 1 8 28 2 27.73938 1978 1 9 23 2 27.73938 1978 1 10 24 2 27.73938 1978 1 11 25 2 27.73938 1978 1 Etc... You need to rearrange your data first, so that it looks like len age cohort sex source 1 242 1977 1 obs 2 313 1977 1 obs 3 222 1978 1 obs 4 343 1977 1 obs ... 1 30.05771 2 1977 1 fitted 2 36.64122 3 1977 1 fitted 3 27.73938 2 1978 1 fitted 4 36.64122 3 1977 1 fitted There are several ways to do that; options include reshape(), make.groups(), and the reshape package. After that, something like xyplot(len ~ age | as.factor(cohort), data = ..., groups = interaction(sex, source), type = c(p, p, l, l), distribute.type = TRUE) should give you what you want (this does use panel.superpose internally). -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] warning messages in grid or lattice give that I can't debug
On 8/2/07, Richard M. Heiberger [EMAIL PROTECTED] wrote: The messages are visible in 2.6.0dev The same commands in 2.4.1 work without warning messages. I am using the HH_1.18-1 from R_2.4.1 with both R versions and not the current HH_2.1-3 version$version.string installed.packages()[c(grid,lattice, HH),3] ## library(HH) library(lattice, lib.loc=C:/PROGRA~1/R/R-2.4.1/library) hotdog - read.table(hh(datasets/hotdog.dat), header=TRUE) ancova(Sodium ~ Calories + Type, data=hotdog) the messages in R_2.6.0dev are Warning messages: 1: In grid.Call.graphics(L_points, x$x, x$y, x$pch, x$size) : supplied color is not numeric nor character ... 6: In grid.Call.graphics(L_points, x$x, x$y, x$pch, x$size) : supplied color is not numeric nor character My guess is that these warnings are a response to a change in grid or lattice in 2.5.0. I attempted to trace these with options(warn=2) options(error=recover) without success. Debugging suggestions or revised programming suggestions would be appreciated. Well, options(warn=2) traces the problem to the legend drawing code, and going from there, I get str(trellis.last.object()$legend$right$args$key$points) 'data.frame': 3 obs. of 6 variables: $ alpha: num 1 1 1 $ cex : num 0.8 0.8 0.8 $ col : chr #0080ff #ff00ff darkgreen $ font : num 1 1 1 $ pch : num 1 1 1 $ fill : Factor w/ 1 level transparent: 1 1 1 Note that the last component 'fill' is a factor (which is not numeric nor character, at least in a zen kind of way). I presume you are somehow protecting 'col' from becoming a factor, and that does not translate to 'fill'. This behaviour (of HH) is not new, and I believe you will see the same output as above in R 2.4. What's new is that 'fill' used to be ignored by draw.key(), which was a bug in lattice that has now been fixed. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] warning messages in grid or lattice give that I can't debug
On 8/2/07, Richard M. Heiberger [EMAIL PROTECTED] wrote: Thanks Deepayan. Does that translate that I don't have to do anything else, and that I can submit the package in its present state to CRAN? No, you have to make sure that 'fill' is not a factor. My guess is that the simplest way to do that would be to not make key$points a data.frame, but I haven't actually looked at what your code is doing. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] lattice grayscale theme
On 7/30/07, Patrick Drechsler [EMAIL PROTECTED] wrote: Deepayan Sarkar [EMAIL PROTECTED] writes: On 7/30/07, Patrick Drechsler [EMAIL PROTECTED] wrote: The Gmane interface seems to have some lag at the moment... Deepayan Sarkar [EMAIL PROTECTED] writes: On 7/28/07, Patrick Drechsler [EMAIL PROTECTED] wrote: is there a grayscale setting for lattice plots? [...] Is there a simple way of achieving something in between these settings (using grayscales for the default colors)? Possibly, but you would have to define what you mean by 'simple' and 'something in between' more precisely. Here is an example of 'in between': --8---cut here---start-8--- ## Set background color of strips to grayscales: strip.background - trellis.par.get(strip.background) trellis.par.set(strip.background = list(col = grey(7:1/8))) ## Set color of plot symbols to grayscale: plot.symbol - trellis.par.get(plot.symbol) trellis.par.set(plot.symbol = list(col = grey(5/8))) --8---cut here---end---8--- Well, there you go. You have a new theme. Thanks for confirming that I am on the right track! I think it would be nice to have a few default themes: A single switch between default color, grayscale and black and white. I'm still not sure what qualities you are looking for in the new theme. Goal: Lattice should have good default settings for color, grayscale and bw. The user should not be bothered with the details (similar to using LaTeX: Many users do not have any idea of DEK's typesetting in the background). Lattice functions should provide reasonable default settings. The default colors for the lattice suite were chosen for a reason (very good choice for color display). IMO the default grayscale colors should be wrapped into a similar default-suite after discussion of which grayscales are best suited for printing with different common resolutions using b/w printers. After this has been decided upon, it might be nice to have something along the lines of: lattice.options(default.theme = [color|gray|bw]) Have I described my concern elaborately enough? Yes, and I agree that in principle it would be a good thing to have. Personally, I don't believe such a theme can be made (at least not one that's generally useful) and will not spend time trying to create one. I'm happy to add one to lattice if anyone contributes it. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] add custom strip to lattice plot
On 7/30/07, Patrick Drechsler [EMAIL PROTECTED] wrote: Hi, what is the recommended way of adding a strip to a lattice plot? See ?strip.default. In the example below I would like to add the value of mean(y) to a new strip.: --8---cut here---start-8--- library(lattice) ## Small sample data set: p0 - xyplot(uptake ~ Type | Treatment, data = CO2) p1 - update(p0, panel = function(x, y, ...) { panel.xyplot(x, y) panel.abline(h = mean(y), col = red ) } ) plot(p1) --8---cut here---end---8--- That's not what strips are for. Strips are used for showing information about the levels of conditioning variables. You can instead do something like this: update(p0, panel = function(x, y, ...) { require(grid) panel.xyplot(x, y, ...) panel.abline(h = mean(y), col = red) grid.text(round(mean(y), 3), 0.5, 1, vjust = 1.2) }) The text can be put into a rectangle, to make them look like strips, and you could use a prepanel function to make sure there's enough space for it. Look up grid documentation for details. There are other workarounds, e.g., using 'trellis.focus' and 'lattice.panelArgs'. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Splom custom superpanels
On 8/1/07, Jonathan Williams [EMAIL PROTECTED] wrote: I thought one nice addition to a splom figure would be to have the scatterplots in the upper triangle and a color-coordinated correlation matrix on the bottom. So I tried my hand at customizing panel.pairs(), and was rebuffed. Many times. Four hours of fruitless debugging later, I turn to you for help: panel.pairs(z=teststatfull[,6:12], pscales=0, panel.subscripts=FALSE, subscripts=, upper.panel=lattice.getOption(panel.splom), lower.panel=function(x1=panel.args$x, y1=panel.args$y, panel.args=trellis.panelArgs(), subscripts=1:dim(teststatfull)[1],...){ panel.fill(col=brewer.pal(9,RdBu)[round(cor(x1,y1)*4 + 5)]) panel.text(mean(x1), mean(y1), round(cor(x1,y1),2), font=2)}) This code is a bit above my level; I stole some tricks from examples I saw elsewhere, and while it looks over-clunky, it works. Works, at least, in creating the superpanel: you can try it yourself by replacing z with a data frame or matrix of your choice and installing the lattice and RColorBrewer packages. However, when I try to insert this into the splom function, it all goes to pot. R scolds me for either missing subscripts, improper subscripts when I try to provide them, or missing data in the panel function, no matter how I define one (or don't). Can anyone recommend a solution or show me how to make my superpanel function more palatable to splom? I haven't figured out how to run your code, even after replacing 'z', but I think what you are making things more complicated than they have to be: library(lattice) library(RColorBrewer) foo - mtcars[c(1:6)] splom(foo, upper.panel = panel.splom, lower.panel = function(x, y, ...) { panel.fill(col = brewer.pal(9, RdBu)[ round(cor(x, y) * 4 + 5)]) panel.text(mean(x), mean(y), round(cor(x, y),2), font=2) }) Of course, you can, if you really want to, make a custom superpanel function to do this: my.panel.pairs - function(..., lower.panel) { my.lower.panel - function(x, y, ...) { panel.fill(col = brewer.pal(9, RdBu)[ round(cor(x, y) * 4 + 5)]) panel.text(mean(x), mean(y), round(cor(x, y),2), font=2) } panel.pairs(..., lower.panel = my.lower.panel) } splom(foo, superpanel = my.panel.pairs) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] line widths of plotting symbols in the lattice
On 7/31/07, Uwe Ligges [EMAIL PROTECTED] wrote: Stephen Tucker wrote: Dear List, Sorry, this is very simple but I can't seem to find any information regarding line widths of plotting symbols in the lattice package. For instance, in traditional graphics: plot(1:10,lwd=3) points(10:1,lwd=2,col=3) 'lwd' allows control of plotting symbol line widths. 'lwd' is documented in ?gpar (the help page does not show up for me, I'll take a closer look why) and works for me: xyplot(1:10 ~ 1:10, type = l, lwd = 5) I think the point is that lwd doesn't work for _points_, and that is a bug (lplot.xy doesn't pass on lwd to grid.points). I'll fix it, meanwhile a workaround is to use grid.points directly, e.g. library(grid) xyplot(1:10 ~ 1:10, cex = 2, lwd = 3, panel = function(x, y, ...) grid.points(x, y, gp = gpar(...))) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Overlaying a single contour from a new data array in levelplot
On 7/30/07, Jenny Barnes [EMAIL PROTECTED] wrote: Dear Deepayan Thank you for your response - it has proved very very helpful, I can't thank you enough! I have another question for you if you have time to reply. I know you have been asked about the colour of the polygon outline before (27 April 2007) and you replied that is a bug and the border can only be black or transparent... I was wondering if you have found a way to change the colour of the outline since this correspondence? If not please can you tell me how to get around this myself? You mentioned writing a replacement to lpolygon - I do not know how to do this - would it be possible for you to guide me further? That should have been fixed by now. Is there anything that's not working as you expect? My code had: lapply(add.cl, panel.polygon, border = 'red') which should have made the borders red. If it doesn't, you probably need to upgrade to a recent version of R/lattice. If it does, changing it to border='white' should suffice. If that doesn't work, please provide a reproducible example. -Deepayan I would really benefit from having the border of the polygon in white as it goes over the sea which is also white and would therefore only be seen over the land, much neater! Many thanks, Jenny On 7/24/07, Jenny Barnes jmb_at_mssl.ucl.ac.uk wrote: Dear R-Help community, I am trying to overlay a single contour line over a correlation plot using levelplot in the lattice package. These are the two arrays: 1) a correlation plot over Africa - so each grid square is a different colour dependent on correlation - this is in an array: result_cor with dim[465,465] 2) a single contour line from a ***different data source*** - this is from data related to the p-values for the above correlation plot - I want to overlay only the 95% confidence contour. The p-values are stored in an array: result.p.values with same dimensions as above. I have read about using panel.levelplot and panel.contourplot in the R-help mailing list but I don't know the right way to call two different data arrays, can anybody help me please? I appreciate your time and help with this question. I can think of a couple of different ways, but the simplest will probably be to compute the single contour beforehand and add it after the standard levelplot using a panel function. E.g., using the 'volcano' data for both matrices: ## you need the explicit x and y arguments because ## the default is different from levelplot. vcl - contourLines(x = seq_len(nrow(volcano)), y = seq_len(ncol(volcano)), z = volcano, levels = c(172, 182)) levelplot(volcano, add.cl = vcl, panel = function(..., add.cl) { panel.levelplot(...) lapply(add.cl, panel.polygon, border = 'red') }) -Deepayan ~ Jennifer Barnes PhD student: long range drought prediction Climate Extremes Group Department of Space and Climate Physics University College London Holmbury St Mary Dorking, Surrey, RH5 6NT Web: http://climate.mssl.ucl.ac.uk __ R-help@stat.math.ethz.ch 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.
Re: [R] lattice grayscale theme
On 7/28/07, Patrick Drechsler [EMAIL PROTECTED] wrote: Hi, is there a grayscale setting for lattice plots? I like the default color settings. I also like the settings that are available for setting black and white with something like this: --8---cut here---start-8--- ltheme - canonical.theme(color = FALSE) ## in-built BW theme ltheme$strip.background$col - transparent ## change strip bg lattice.options(default.theme = ltheme) ## set as default --8---cut here---end---8--- Is there a simple way of achieving something in between these settings (using grayscales for the default colors)? Possibly, but you would have to define what you mean by 'simple' and 'something in between' more precisely. help(trellis.par.set) tells you how to change settings, and you can use it to change all the components you want (and the colors can of course be greyscale). -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] lattice grayscale theme
On 7/30/07, Patrick Drechsler [EMAIL PROTECTED] wrote: The Gmane interface seems to have some lag at the moment... Deepayan Sarkar [EMAIL PROTECTED] writes: On 7/28/07, Patrick Drechsler [EMAIL PROTECTED] wrote: is there a grayscale setting for lattice plots? I like the default color settings. I also like the settings that are available for setting black and white with something like this: --8---cut here---start-8--- ltheme - canonical.theme(color = FALSE) ## in-built BW theme ltheme$strip.background$col - transparent ## change strip bg lattice.options(default.theme = ltheme) ## set as default --8---cut here---end---8--- Is there a simple way of achieving something in between these settings (using grayscales for the default colors)? Possibly, but you would have to define what you mean by 'simple' and 'something in between' more precisely. Here is an example of 'in between': --8---cut here---start-8--- ## Set background color of strips to grayscales: strip.background - trellis.par.get(strip.background) trellis.par.set(strip.background = list(col = grey(7:1/8))) ## Set color of plot symbols to grayscale: plot.symbol - trellis.par.get(plot.symbol) trellis.par.set(plot.symbol = list(col = grey(5/8))) --8---cut here---end---8--- Well, there you go. You have a new theme. I think it would be nice to have a few default themes: A single switch between default color, grayscale and black and white. I'm still not sure what qualities you are looking for in the new theme. An upcoming version of the latticeExtra package will have a function that can be used to create a theme based on some given colors; you might find it useful. It uses colors from RColorBrewer by default, and looks like: brewer.theme - function(symbol = brewer.pal(n = 8, name = Dark2), fill = brewer.pal(n = 12, name = Set3), region = brewer.pal(n = 11, name = Spectral), reference = #e8e8e8, bg = transparent, fg = black) { theme - list(plot.polygon = list(col = fill[1], border = fg[1]), box.rectangle = list(col= symbol[1]), box.umbrella = list(col= symbol[1]), dot.line = list(col = reference), dot.symbol= list(col = symbol[1]), plot.line = list(col = symbol[1]), plot.symbol = list(col= symbol[1]), regions = list(col = colorRampPalette(region)(100)), reference.line= list(col = reference), superpose.line= list(col = symbol), superpose.symbol = list(col = symbol), superpose.polygon = list(col = fill, border = fg), background= list(col = bg), add.line = list(col = fg), add.text = list(col = fg), box.dot = list(col = fg), axis.line = list(col = fg), axis.text = list(col = fg), strip.border = list(col = fg), box.3d= list(col = fg), par.xlab.text = list(col = fg), par.ylab.text = list(col = fg), par.zlab.text = list(col = fg), par.main.text = list(col = fg), par.sub.text = list(col = fg)) modifyList(standard.theme(pdf), theme) } -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] duplicate DATE at in lattice scale handled differently from base graphics OR lattice numeric scales
On 7/30/07, Alex Brown [EMAIL PROTECTED] wrote: When at, label chains contain duplicate at values, axis ticks are dropped. I believe this is handled incorrectly for Date ats in lattice 0.15-4 when compared to how it is handled for numeric, or for dates in base plot. This can result in mis-labelled axes in some circumstances. I have included executable examples. R: 2.5.0 numeric base plot: plot(1:10, axes=F); axis(1, at=c(2,4,4,6), labels=letters[1:4]) shows labels a, b, d date scale in base plot: plot((as.Date(2007-01-01) + 1:10), 1:10, axes=F); axis(1, at= (as.Date(2007-01-01)+c(2,4,4,6)), labels=letters[1:4]) shows labels a, b, d However, foo = as.Date(2007-01-01) plot(foo + 1:10, 1:10, axes = FALSE) axis.Date(1, at = foo + c(2,4,4,6), labels = letters[1:4]) Error in axis(side, at = z, labels = labels, ...) : 'at' and 'labels' lengths differ, 3 != 4 And this code is used almost unchanged in lattice, leading to str(lattice:::formattedTicksAndLabels.Date(range(foo + 1:10), at = foo + c(2, 4, 4, 6), labels = letters[1:4])) List of 4 $ at : num [1:3] 13516 13518 13520 $ labels : chr [1:4] a b c d $ check.overlap: logi FALSE $ num.limit: num [1:2] 13515 13524 That is, 'at' is now of length 3 (and unlike axis.Date, this does not subsequently cause an error, which probably would have been preferable). I will add a fix. -Deepayan numeric scale in lattice plot xyplot(1:10 ~ 1:10, scales=list(x=list(relation=free, at=c (2,4,4,6), labels=list(letters[1:4] shows labels a, b, d date scale in lattice plot xyplot(1:10 ~ (as.Date(2007-01-01) + 1:10) , scales=list(x=list (relation=free, at=list(as.Date(2007-01-01)+c(2,4,4,6)), labels=list(letters[1:4] shows labels a, b, c Since this results in label c being placed at position 6, not 4, I feel this is a bug. -Alex Brown __ R-help@stat.math.ethz.ch 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.
Re: [R] How to auto-scale cex of y-axis labels in lattice dotplot?
On 7/25/07, Kevin Wright [EMAIL PROTECTED] wrote: When I create a dotplot in lattice, I frequently observe overplotting of the labels along the vertical axis. On my screen, this illustrates overplotting of the letters: windows() reps=6 dat=data.frame(let=rep(letters,each=reps), grp=rep(1:reps, 26), y=runif(26*reps)) dotplot(let~y|grp, dat) Is there a way to automatically scale the labels so that they are not over-plotted? Not that I can think of. I currently do something like this: Calculate or guess the number of panel rows: NumPanelRows cexLab - min(1, .9*par()$pin[2]/ (nlevels(dat$let)*NumPanelRows*strheight(A,units=in))) dotplot(..., scales=list(y=list(cex=cexLab)) Is there an easier way? Is there a function that I can call which calculates the layout of the panels that will be used in the dotplot? Not really. The eventual layout is calculated inside print.trellis as follows (where 'x' is the trellis object being plotted): panel.layout - compute.layout(x$layout, dim(x), skip = x$skip) [...] if (panel.layout[1] == 0) { ddim - par(din) device.aspect - ddim[2] / ddim[1] panel.aspect - panel.height[[1]] / panel.width[[1]] plots.per.page - panel.layout[2] m - max (1, round(sqrt(panel.layout[2] * device.aspect / panel.aspect))) n - ceiling(plots.per.page/m) m - ceiling(plots.per.page/n) panel.layout[1] - n panel.layout[2] - m } -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Overlaying a single contour from a new data array in levelplot
On 7/24/07, Jenny Barnes [EMAIL PROTECTED] wrote: Dear R-Help community, I am trying to overlay a single contour line over a correlation plot using levelplot in the lattice package. These are the two arrays: 1) a correlation plot over Africa - so each grid square is a different colour dependent on correlation - this is in an array: result_cor with dim[465,465] 2) a single contour line from a ***different data source*** - this is from data related to the p-values for the above correlation plot - I want to overlay only the 95% confidence contour. The p-values are stored in an array: result.p.values with same dimensions as above. I have read about using panel.levelplot and panel.contourplot in the R-help mailing list but I don't know the right way to call two different data arrays, can anybody help me please? I appreciate your time and help with this question. I can think of a couple of different ways, but the simplest will probably be to compute the single contour beforehand and add it after the standard levelplot using a panel function. E.g., using the 'volcano' data for both matrices: ## you need the explicit x and y arguments because ## the default is different from levelplot. vcl - contourLines(x = seq_len(nrow(volcano)), y = seq_len(ncol(volcano)), z = volcano, levels = c(172, 182)) levelplot(volcano, add.cl = vcl, panel = function(..., add.cl) { panel.levelplot(...) lapply(add.cl, panel.polygon, border = 'red') }) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] lattice plot axis scaling
On 7/18/07, Alan S Barnett [EMAIL PROTECTED] wrote: I want to generate a lattice plot of a multiple linear regression. I'm using the code: xyplot(y ~ x1 + x2 | status, data=datam, xlab=Peak separation,ylab=G/W,main=G/W vs Fuzzy peak separation: Threshold=1.8, groups=Fuzzy.gw.t.score1.8, subset=(status %in% c(control,patient,sibling)), panel=function(x,y,groups,subscripts,...){ panel.xyplot(x,y,groups=groups,subscripts=subscripts,...) panel.abline(tmp-lm(y~x),col = light blue,lwd=2) panel.abline(tmp-rlm(y~x),col = blue,lwd=2) good.id-groups[subscripts] fm - rlm(y[good.id] ~ x[good.id]) sm - summary(fm) panel.abline(reg = fm) panel.abline(tmp1-lm(y[good.id]~x[good.id]),col = pink,lwd=2) panel.abline(tmp1-rlm(y[good.id]~x[good.id]),col = red,lwd=2) slope - round(coef(fm)[2], 3) err - round(sm$coefficients[4], 3) message(Click on desired location) panel.text(pos-grid.locator(native), lab = paste(slope =,slope,+-,err),col=red) }) ___ The problem is that x1 varies from 1-3, while x2 varies from 20-60. The output scales both independent variables the same, so all the data in the y vs x1 plot are up against the left edge of the plot. How do I scale the x-axes separately? Add scales = list(x = free) to your call. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] creating a world map of eco-climatic zones
On 7/18/07, Tanja Srebotnjak [EMAIL PROTECTED] wrote: Hello R users: I would like to produce a world map with countries colored according to whether they fall into one of 7 eco-climatic zones. For simplicity, each country is allocated to exactly 1 eco-climatic zone. For this purpose I have looked at the map and mapdata packages, which contain world maps composed of polygons (1 for each country, it seems). Each of the polygons can be referred to by a region name in the map package. I am thinking, I would do something like map(region=c('Germany', 'Switzerland',...), fill=TRUE, col='blue') for each of the 7 eco-climatic zones. My question is, what are the names used in the world map dataset for each country and how can I get them? library(maps) wmap - map('world', plot = FALSE, fill = TRUE) str(wmap) List of 4 $ x: num [1:27636] -133 -132 -132 -132 -130 ... $ y: num [1:27636] 58.4 57.2 57.0 56.7 56.1 ... $ range: num [1:4] -180.0 190.3 -85.4 83.6 $ names: chr [1:2284] Canada South Africa Denmark Great Lakes:Superior, Huron, Michigan ... - attr(*, class)= chr map So wmap$names will give you the names you want. Perhaps there's also a smarter way to produce this map, in which case, your advise is greatly appreciated as well. If you construct a suitable color vector (with the same order as $names), you should be able to do the whole thing in one call. Lastly, some countries (mostly large ones such as USA, Russia, Canada, China) fall into multiple eco-climatic zones. Assume I could get geographical coordinates for polygons describing the sub-country portion falling into a single eco-climatic zone, is there a way to improve the above map to show portions of countries belonging to a specific eco-climatic zone? wmap$x and wmap$y are exactly that (coordinates of polygons, with NA's separating polygons). You should be able to add a few more, add suitable names, and supply the result as the 'database' argument to another call to map(). -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] xyplot for longitudinal data
On 7/17/07, Osman Al-Radi [EMAIL PROTECTED] wrote: Dear R-help subscribers, I use xyplot to plot longitudinal data as follows: score-runif(100,-4,5) group-sample(1:4,100,rep=T) subject-rep(1:25,4) age-rep(runif(4,1,40),25) df-data.frame(score,group,age,subject) xyplot(score~age|group, group=subject, panel=function(...){ panel.loess(...,lwd=4) panel.superpose(...)} ,data=df) this produced a plot with four panels one for each group, with unique plotting parameters for each subject. How can I create a create a plot with a single panel where all four groups are superimposed using different line colors and symbols for each group, but preserving the longitudinal nature of the data (i.e. one line per subject). Create an interaction and specify the colors and symbols you want explicitly. E.g., spcolors - trellis.par.get(superpose.symbol)$col df$subject - factor(df$subject) df$group - factor(df$group) with(df, xyplot(score~age, groups = interaction(subject, group), type = l, col = rep(spcolors[1:nlevels(group)], each = nlevels(subject Look at ?Rows, which might help in getting code that is a bit more general (for instance, the code above may not work when groups has more than 7 levels). -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Restructuring data
On 7/15/07, Daniel Malter [EMAIL PROTECTED] wrote: Hi folks, I am new to the list and relatively new to R. I am trying to unstack data arraywise and could not find a convenient solution yet. I tried to find a solution for the problem on help archives. I also tried to use the reshape command in R and the reshape package but could not get result. I will illustrate the case below, but the real dataset is quite large so that I would appreciate an easy solution if there is any. The current data structure (variable names): ID, TIME, BUY-A, BUY-B, SELL-A, SELL-B Achieved structure (with the reshape command or the reshape package) ID, TIME, BUY-A ID, TIME, BUY-B ID, TIME, SELL-A ID, TIME, SELL-B This is regular unstacking with two identifier variables. Nothing special though. What I am looking for and did not manage is the following structure: ID, TIME, BUY-A, SELL-A ID, TIME, BUY-B, SELL-B I am quite sure it's pretty easy, but I could not find how to do this. This seems to work: foo - data.frame(ID = 1:4, TIME=1:4, + BUY-A = rnorm(4), + BUY-B = rnorm(4), + SELL-A = rnorm(4), + SELL-B = rnorm(4), check.names = FALSE) foo ID TIME BUY-A BUY-B SELL-A SELL-B 1 11 0.47022807 1.09573107 0.1977035 -0.08333043 2 22 -0.20672870 0.07397772 1.4959044 -0.98555020 3 33 0.05533779 0.25821758 1.3531913 0.16808307 4 44 -0.11471772 1.27798740 -0.1101390 -0.36937994 reshape(foo, direction=long, + varying = list(c(BUY-A, BUY-B), c(SELL-A, SELL-B)), + v.names=c(BUY, SELL), idvar=ID, + times = c(A, B), timevar=which) ID TIME which BUYSELL 1.A 11 A 0.47022807 0.19770349 2.A 22 A -0.20672870 1.49590443 3.A 33 A 0.05533779 1.35319133 4.A 44 A -0.11471772 -0.11013896 1.B 11 B 1.09573107 -0.08333043 2.B 22 B 0.07397772 -0.98555020 3.B 33 B 0.25821758 0.16808307 4.B 44 B 1.27798740 -0.36937994 -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] change default alphabetic order for bwplot
On 7/13/07, gallon li [EMAIL PROTECTED] wrote: when producing boxplot from bwplot, I have five groups: Nitrogen, Duration, Pressure, A, Z. I wish the graphical display is according to the original order. But the R-function bwplot seems to automatically adjust the groups according to the alphabetic oder and thus creat a graph for A, Duration, Nitrogen, Pressure and Z. How can I specify the original order in bwplot? This also happens to the older function boxplot. This has to do with how levels for a factor are computed by default. help(factor) tells you how to specify your own levels. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Drawing rectangles in multiple panels
On 7/14/07, Stephen Tucker [EMAIL PROTECTED] wrote: I wonder what kind of objects? Are there large advantages for allowing lattice functions to operate on objects other than data frames - I couldn't find any screenshots of flowViz but I imagine those objects would probably be list of arrays and such? I tend to think of mapply() [and more recently melt()], etc. could always be applied beforehand, but I suppose that would undermine the case for having generic functions to support the rich collection of object classes in R... There's a copy of a presentation at http://www.ficcs.org/meetings/ficcs3/presentations/DeepayanSarkar-flowviz.pdf and a (largish - 37M) vignette linked from http://bioconductor.org/packages/2.1/bioc/html/flowViz.html Neither of these really talk about the challenge posed by the size of the data. The data structure, as with most microarray-type experiments, is like a data frame, except that the response for every experimental unit is itself a large matrix. If we represented the GvHD data set (the one used in the examples) as a long format data frame that lattice would understand, it would have 585644 rows and 12 columns (8 measurements that are different for each row, and 4 phenotypic variables that are the same for all rows coming from a single sample). And this is for a smallish subset of the actual experiment. In practice, the data are stored in an environment to prevent unnecessary copying, and panel functions only access one data matrix at a time. -Deepayan --- Deepayan Sarkar [EMAIL PROTECTED] wrote: On 7/11/07, hadley wickham [EMAIL PROTECTED] wrote: A question/comment: I have usually found that the subscripts argument is what I need when passing *external* information into the panel function, for example, when I wish to add results from a fit done external to the trellis call. Fits[subscripts] gives me the fits (or whatever) I want to plot for each panel. It is not clear to me how the panel layout information from panel.number(), etc. would be helpful here instead. Am I correct? -- or is there a smarter way to do this that I've missed? This is one of things that I think ggplot does better - it's much easier to plot multiple data sources. I don't have many examples of this yet, but the final example on http://had.co.nz/ggplot2/geom_abline.html illustrates the basic idea. That's probably true. The Trellis approach is to define a plot by data source + type of plot, whereas the ggplot approach (if I understand correctly) is to create a specification for the display (incrementally?) and then render it. Since the specification can be very general, the approach is very flexible. The downside is that you need to learn the language. On a philosophical note, I think the apparent limitations of Trellis in some (not all) cases is just due to the artificial importance given to data frames as the one true container for data. Now that we have proper multiple dispatch in S4, we can write methods that behave like traditional Trellis calls but work with more complex data structures. We have tried this in one bioconductor package (flowViz) with encouraging results. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Direction of panel plots in trellis graphics
On 7/13/07, Richard M. Heiberger [EMAIL PROTECTED] wrote: You can control the panel sequence with subscripting and transpose. Here are several examples. I think tmp.tr3 is the one you asked for. library(lattice) tmp - data.frame(x=rnorm(24), y=rnorm(24), a=rep(letters[1:6],4), b=rep(LETTERS[1:4],each=6)) tmp.tr - xyplot(y ~ x | a*b, data=tmp) tmp.tr t(tmp.tr) tmp.tr2 - xyplot(y ~ x | a, data=tmp) tmp.tr2 tmp.tr2[c(1,3,5,2,4,6)] tmp.tr3 - xyplot(y ~ x | a, data=tmp, as.table=TRUE) tmp.tr3 tmp.tr3[c(1,3,5,2,4,6)] Another high level option is to change the rule determining how packets are chosen for a given panel in the layout. print(tmp.tr3, packet.panel = function(layout, row, column, ...) { layout - layout[c(2, 1, 3)] packet.panel.default(layout = layout, row = column, column = row, ...) }) This effectively transposes the layout, which (along with as.table=TRUE) is what you want. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Subplot.
On 7/13/07, suman Duvvuru [EMAIL PROTECTED] wrote: Hello All, I wanted to do many plots (in my case, wanted to get 6 histograms) on the same figure. Is there a method in R that analogous to 'subplot' in MATLAB? Here are a few possibilities: data(singer, package = lattice) ## using traditional graphics singer.split - with(singer, split(height, voice.part)) par(mfrow = c(2, 4)) for (i in names(singer.split)) hist(singer.split[[i]], main = i, xlab = height) ## using lattice library(lattice) histogram(~height | voice.part, singer) ## using ggplot2 library(ggplot2) qplot(height, data = singer, geom = histogram, facets = voice.part ~ .) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Drawing rectangles in multiple panels
On 7/11/07, Jonathan Williams [EMAIL PROTECTED] wrote: Hi folks, I'm having some trouble understanding the intricacies of panel functions. I wish to create three side-by-side graphs, each with different data-- so far, so good: I rbind() the data, add a column of subscripts as a conditioning variable, load up the lattice package, specify either a c(3,1) 'layout' or work through 'allow.multiple' and 'outer' and I'm good to go. But now I wish to add three rectangles to each plot, which will be in different places on each panel, and I'm terribly stuck. I can guess this requires defining a panel function on the fly, but none of my attempts are working. Suggestions? You haven't told us what determines the rectangles (only that they are different in each panel). If they are completely driven by panel data, here's an example: panel.qrect - function(x, y, ...) { xq - quantile(x, c(0.1, 0.9)) yq - quantile(y, c(0.1, 0.9)) panel.rect(xq[1], yq[1], xq[2], yq[2], col = grey86, border = NA) panel.xyplot(x, y, ...) } xyplot(Sepal.Length ~ Sepal.Width | Species, iris, panel = panel.qrect) If the rectangles are somehow determined externally, you probably want to use one of the accessor functions described in help(panel.number). There are good and bad (i.e. less robust) ways to use these, but we need to know your use case before recommending one. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Drawing rectangles in multiple panels
On 7/11/07, Bert Gunter [EMAIL PROTECTED] wrote: Deepayan et. al.: A question/comment: I have usually found that the subscripts argument is what I need when passing *external* information into the panel function, for example, when I wish to add results from a fit done external to the trellis call. Fits[subscripts] gives me the fits (or whatever) I want to plot for each panel. It is not clear to me how the panel layout information from panel.number(), etc. would be helpful here instead. Am I correct? -- or is there a smarter way to do this that I've missed? subscripts is absolutely the right thing to use if your auxiliary information is in the form of vectors than have the same length as the rest of your data. Examples would include a color for every point in a xyplot or confidence bounds in a dotplot. However, sometimes your external information might be a summary; say the parameters defining a fitted curve for every combination of your conditioning variables (and the underlying model might have shared information across combinations, so you wouldn't be able to compute them from the panel data alone). In that case, which.packet(), which gives you the levels of the conditioning variables defining the current panel, may be helpful. Of course, there are workarounds using subscripts, or in this example, adding rows to your data frame containing the fitted values. The accessors are a convenience that sometimes make life simpler. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Drawing rectangles in multiple panels
On 7/11/07, Jonathan Williams [EMAIL PROTECTED] wrote: Deepayan, Thanks for the clarification. The rectangles are completely external to the panel data, and correspond to 90% confidence intervals built from training data, to be overlaid on these graphs of the test data. Right. So if you have that information in a single object (say a list), you can specify that as an argument to xyplot (or whatever), and capture that in the panel function to then index it. E.g. rectInfo - list(matrix(runif(4), 2, 2), matrix(runif(4), 2, 2), matrix(runif(4), 2, 2)) panel.qrect - function(x, y, ..., rect.info) { ri - rect.info[[packet.number()]] ## if you have more than one conditioning variable, this might be ## something like ## ri - do.call([, list(rect.info, as.list(which.packet([[1]] panel.rect(ri[1, 1], ri[1, 2], ri[2, 1], ri[2, 2], col = grey86, border = NA) panel.xyplot(x, y, ...) } xyplot(runif(30) ~ runif(30) | gl(3, 10), rect.info = rectInfo, panel = panel.qrect) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Drawing rectangles in multiple panels
On 7/11/07, hadley wickham [EMAIL PROTECTED] wrote: A question/comment: I have usually found that the subscripts argument is what I need when passing *external* information into the panel function, for example, when I wish to add results from a fit done external to the trellis call. Fits[subscripts] gives me the fits (or whatever) I want to plot for each panel. It is not clear to me how the panel layout information from panel.number(), etc. would be helpful here instead. Am I correct? -- or is there a smarter way to do this that I've missed? This is one of things that I think ggplot does better - it's much easier to plot multiple data sources. I don't have many examples of this yet, but the final example on http://had.co.nz/ggplot2/geom_abline.html illustrates the basic idea. That's probably true. The Trellis approach is to define a plot by data source + type of plot, whereas the ggplot approach (if I understand correctly) is to create a specification for the display (incrementally?) and then render it. Since the specification can be very general, the approach is very flexible. The downside is that you need to learn the language. On a philosophical note, I think the apparent limitations of Trellis in some (not all) cases is just due to the artificial importance given to data frames as the one true container for data. Now that we have proper multiple dispatch in S4, we can write methods that behave like traditional Trellis calls but work with more complex data structures. We have tried this in one bioconductor package (flowViz) with encouraging results. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Drawing rectangles in multiple panels
On 7/11/07, Stephen Tucker [EMAIL PROTECTED] wrote: In the Trellis approach, another way (I like) to deal with multiple pieces of external data sources is to 'attach' them to panel functions through lexical closures. For instance... rectInfo - list(matrix(runif(4), 2, 2), matrix(runif(4), 2, 2), matrix(runif(4), 2, 2)) panel.qrect - function(rect.info) { function(x, y, ...) { ri - rect.info[[packet.number()]] panel.rect(ri[1, 1], ri[1, 2], ri[2, 1], ri[2, 2], col = grey86, border = NA) panel.xyplot(x, y, ...) } } xyplot(runif(30) ~ runif(30) | gl(3, 10), panel = panel.qrect(rectInfo)) ...which may or may not be more convenient than passing rectInfo (and perhaps other objects if desired) explicitly as an argument to xyplot(). That's an interesting approach. I think the important thing is to make sure that the data required to reproduce the plot is available as part of the trellis object (e.g. if you save it and load it in another session (not that anyone actually ever does that, but it's the principle of the thing)). This happens transparently if you supply the external data as arguments to xyplot(). It happens in your example too, but the data is hidden inside the environment of the panel function. The two approaches might differ in terms of memory use, but I'm not sure. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: vertical barchart
On 7/10/07, Sundar Dorai-Raj [EMAIL PROTECTED] wrote: Michael Hoffman said the following on 7/10/2007 7:06 AM: barchart(Titanic, stack=F) produces a very nice horizontal barchart. Each panel has four groups of two bars. barchart(Titanic, stack=F, horizontal=F) doesn't produce the results I would have expected, as it produces this warning message: Warning message: y should be numeric in: bwplot.formula(x = as.formula(form), data = list(Class = c(1, And it results in each panel having 22 groups of 0-2 bars. How can I produce something just like the original except with the orientation changed? Thanks in advance. Hi, Michael, It seems that barchart.table doesn't allow the horizontal = FALSE argument. With a slight modification to barchart.table this can be accomplished. Thanks Sundar. This seems like a reasonable feature; I'll add it in the next update. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Formatting panel borders in lattice package
On 7/10/07, Hil Lyons [EMAIL PROTECTED] wrote: Hello all -- I would like to thicken the borders between panels -- or more generally, all borders -- in a plot generated using lattice (specifically, levelplot). Something similar perhaps to box() function in graphics. I haven't been successful in reviewing available documentation. The problem stems from using a grayscale colorscheme that puts black colors near the borders, thus obscuring them. Then, another alternative would be to somehow inject white space between panels. In summary: 1) Does anybody know of a good way to increase the line thickness of borders in lattice, specifically those separating panels? qqmath(~height | voice.part, singer, par.settings = list(axis.line = list(lwd = 3))) (or probably closer to what you want) qqmath(~height | voice.part, singer, par.settings = list(axis.line = list(lwd = 3)), scales = list(lwd = 1)) 2) Does anybody know of a way to separate panels with whitespace? qqmath(~height | voice.part, singer, between = list(x = 0.5, y = 0.5)) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Antwort: Re: pgup/pgdown in R Graphics Window under Linux ['Watchdog': checked]
On 7/8/07, Paul Matthias Diderichsen [EMAIL PROTECTED] wrote: Hi Deepayan, Deepayan Sarkar [EMAIL PROTECTED] schrieb am 06.07.2007 02:05:02: On 7/5/07, Paul Matthias Diderichsen [EMAIL PROTECTED] wrote: library(lattice) xyplot(speed~dist|speed, data=cars, layout=c(3,3)) If this is your use case, you might be interested in http://cran.r-project.org/src/contrib/Descriptions/plotAndPlayGTK.html Thanks a lot for the pointer; this package seems to be very useful when coding your own plots. However, it's not exactly my use case - rather an example to illustrate the the X11 graphics device is apparently not too useful for multi-page plots. The motivation for my question was that I want to use xpose4 ( http://xpose.sourceforge.net/) under linux. Xpose is a program that provides functions for producing diagnostic plots for population PKPD model evaluation. I am not able to rewrite the entire package, wrapping every call to multi-page plot functions with plotAndPlayGTK. That's why I was hoping that there exist some obscure configuration option for X11 (seems not to be the case, cf. Prof Ripley's reply) or an alternative graphic device that runs under linux. The tools seem to be there already (recordPlot and replayPlot), and it seems mostly a matter of capturing the relevant keypresses etc. I have no idea how hard that would be with the X11 device, but I have added some basic functionality to the Qt based device I've been playing with. You are welcome to try it --- details can be found at http://dsarkar.fhcrc.org/R/R-Qt.html I'm sure doing something similar with the Gtk/Cairo devices wouldn't be too hard (for those who know what they are doing). -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Several quick questions
On 7/7/07, Sébastien [EMAIL PROTECTED] wrote: Dear R users, Here is a couple a quick questions, for which I was unable to not find any answer in the list archives and in the help: [...] 2- When a log scale is called in a graph, the label takes a format like 10^n. That's true for lattice, but not traditional graphics, as far as I know. Is there a way to come back to a regular number format like 1, 10, 100... without having to create a custom axis ? Depends on what you mean by custom axis. You don't need to manually choose the tick positions etc, but you still need to define the rules that determine how they are calculated. See example(axis.default) for an example where the tick positions remain the same (as the defaults), but the labels change. The slightly different rule used in traditional graphics is available through the axTicks() function, which basically boils down to this: logTicks - function (lim, loc = c(1, 5)) { ii - floor(log10(range(lim))) + c(-1, 2) main - 10^(ii[1]:ii[2]) r - as.numeric(outer(loc, main, *)) r[lim[1] = r r = lim[2]] } where 'lim' is the limits in the original scale. So we have logTicks(c(1, 100)) [1] 1 5 10 50 100 logTicks(c(1, 100), loc = c(2, 5, 10)) [1] 1 2 5 10 20 50 100 3- In lattice graphics, how does the default value of the axs argument influence the values of limits ? This question should be considered in the following context. The help states that a 4% extension is applied by default to the axis range in base graphics. So, I have tried to apply this 4 % extension to create some custom lattice graphics. I worked on a dataset in which the independent variable ranged from 0 to 120, so I basically customized my axis using limits=c(-4.8,124.8). The results of the graphics with and without the limits command were not identical... The extension is user-settable in lattice, and defaults to 7% (I think this value came from Trellis specs, but I don't remember the exact details). lattice.getOption(axis.padding) $numeric [1] 0.07 $factor [1] 0.6 -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] algebra/moving average question - NOTHING TO DO WITH R
On 7/6/07, Leeds, Mark (IED) [EMAIL PROTECTED] wrote: This has ABSOLUTELY nothing to do with R but I was hoping that someone might know because there are obviously a lot of very bright people on this list. Suppose I had a time series of data and at each point in time t, I was calculating x bar + plus minus sigma where x bar was based on a moving window of size n and so was sigma. So, if I was at time t , then x bar t plus minus sigma_t would be based on the values of x_t-n+1 through x_t. This is the hard part : Is there a way to back out what the next value(s), x_t+1 would have to be in order to for that value to be either greater than x bar_t+1 plus Z*sigma_t+1 or less than x bar_t+1 plus minus Z*sigma_t+1. where Z is whatever constant ? I started to try to figure this out but the data window changes and the x bar is in the formula for sigma so the algebra got overwhelming but I was never an algebra whiz. thanks. Here's one approach: Define T1(t, n) = \sum_{k=t-n+1}^t X_k T2(t, n) = \sum_{k=t-n+1}^t {X_k}^2 Then, dropping the dependence on (t, n), xbar = T1 / n sigma^2 = (T2 - T1^2 / n) / (n-1) The bounds you want are X_{t+1} = xbar(t + 1, n) +/- Z sigma(t + 1, n) Note that T1(t + 1, n) = T1(t, n - 1) + X_{t+1} T2(t + 1, n) = T2(t, n - 1) + {X_{t+1}}^2 This should give you a quadratic equation for X_{t+1}. If my calculations are correct (they may not be, so you should check), this equation is Ax^2 + Bx + C = 0, where A = (n-1)^3 - Z^2 * n * (n-1) B = -2 * (Z^2 * n + (n-1)^2) * T1(t, n-1) C = (Z^2 * n + n - 1) * T1(t, n-1)^2 - Z^2 * n^2 * T2(t, n-1) Hope this helps, -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Changing Tick Mark Values for lattice / wireframe
On 7/6/07, Anthony Pezzola [EMAIL PROTECTED] wrote: How can I change the tick mark values in lattice, specifically wireframe? I have a 11*46 matrix of values that I am plotting using wireframe. Unfortunely, the values range from 0.1-1.1 and 0.5-5. Using the code below the tick marks have are (2,4,6,8,10) and (10,20,30,40). You will need to use the formula method. Here's one possibility: mydata - expand.grid(row = rowvals, column = colvals) mydata$z - as.vector(as.numeric(output.matrix)) wireframe(z ~ x * y, data = mydata, ...) It probably makes sense to allow this for matrices directly, and I'll add something in the next update. -Deepayan Thanks in advance. graphic5 - wireframe(output.matrix, shade= TRUE, scales = list(arrows = FALSE, cex=.6, col=black, font= 3, tck=1), xlab=list(Employees, cex=.65, col=black, rot=30), ylab=list(Political Concentration, cex=.65, col=black, rot=-40), zlab=list(label=Probability of Protection, cex=.65, rot=90, col=black) ) __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] Lookups in R
On 7/5/07, jim holtman [EMAIL PROTECTED] wrote: You are getting two very different results in what you are comparing. system.time(lapply(1:10^4, mean)) user system elapsed 1.310.001.31 is returning a list with 10,000 values in it. It is taking time to allocate the space and such. system.time(for(i in 1:10^4) mean(i)) user system elapsed 0.330.000.32 is just returning a single value (mean(10^4)) and is not having to allocate space and setup the structure for a list. Typically you use 'lapply' not only for 'looping', but more importantly returning the values associated with the processing. The point still holds: system.time(lapply(1:10^4, mean)) user system elapsed 3.748 2.404 6.161 system.time({ a = numeric(10^4); for (i in 1:10^4) a[i] = mean(i) }) user system elapsed 0.716 0.004 0.720 To really get rid of the for loop, you need to move the loop to pure C code, e.g. system.time(rowMeans(matrix(1:10^4, ncol = 1))) user system elapsed 0.004 0.000 0.004 Sometimes you can do this using functions available in R, e.g. using tapply() in your original question and rowMeans() in this example. Sometimes you cannot, and the only way to gain efficiency is to write custom C code (we do not have enough information to decide which is the case in your real example, since we don't know what it is). -Deepayan On 7/5/07, Michael Frumin [EMAIL PROTECTED] wrote: the problem I have is that userid's are not just sequential from 1:n_users. if they were, of course I'd have made a big matrix that was n_users x n_fields and that would be that. but, I think what I cando is just use the hash to store the index into the result matrix, nothing more. then the rest of it will be easy. but please tell me more about eliminating loops. In many cases in R I have used lapply and derivatives to avoid loops, but in this case they seem to give me extra overhead simply by the generation of their result lists: system.time(lapply(1:10^4, mean)) user system elapsed 1.310.001.31 system.time(for(i in 1:10^4) mean(i)) user system elapsed 0.330.000.32 thanks, mike I don't think that's a fair comparison--- much of the overhead comes from the use of data frames and the creation of the indexing vector. I get n_accts - 10^3 n_trans - 10^4 t - list() t$amt - runif(n_trans) t$acct - as.character(round(runif(n_trans, 1, n_accts))) uhash - new.env(hash=TRUE, parent=emptyenv(), size=n_accts) for (acct in as.character(1:n_accts)) uhash[[acct]] - list(amt=0, n=0) system.time(for (i in seq_along(t$amt)) { + acct - t$acct[i] + x - uhash[[acct]] + uhash[[acct]] - list(amt=x$amt + t$amt[i], n=x$n + 1) + }, gcFirst = TRUE) user system elapsed 0.508 0.008 0.517 udf - matrix(0, nrow = n_accts, ncol = 2) rownames(udf) - as.character(1:n_accts) colnames(udf) - c(amt, n) system.time(for (i in seq_along(t$amt)) { + idx - t$acct[i] + udf[idx, ] - udf[idx, ] + c(t$amt[i], 1) + }, gcFirst = TRUE) user system elapsed 1.872 0.008 1.883 The loop is still going to be the problem for realistic examples. -Deepayan __ R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@stat.math.ethz.ch 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.
Re: [R] Adding points to a wireframe with conditioning variable
On 7/5/07, Mark Lyman [EMAIL PROTECTED] wrote: I would like to add points to a wireframe but with a conditioning variable. I found a solution for this without a conditioning variable here, http://finzi.psych.upenn.edu/R/Rhelp02a/archive/65321.html. Does anyone know how to plot a wireframe conditioned on a variable and add the points conditioned on the same variable, similar to the solution at the link above? Depends on what form you have your points in. Inside a panel function, packet.number() will give you the packet number in sequential order (column major order if you think of the conditioning variables as defining a multiway cross-tabulation), and which.packet() will give you a vector with the current level of each conditioning variable. You can use these to extract the appropriate subset of points. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] pgup/pgdown in R Graphics Window under Linux
On 7/5/07, Paul Matthias Diderichsen [EMAIL PROTECTED] wrote: Dear S-users. This should be an easy one: How do I change pages on an X11 graphics device under linux? I thought that the page-up/page-down keys were supposed to do the trick, but the frame (window) seems to be kind of immune to any kind of keyboard input. The only reaction I ever see is that the mouse pointer changes to a + when moved into the frame. I issue these commands: [...] library(lattice) xyplot(speed~dist|speed, data=cars, layout=c(3,3)) If this is your use case, you might be interested in http://cran.r-project.org/src/contrib/Descriptions/plotAndPlayGTK.html -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] retrieving stats from bwplot
On 7/3/07, Héctor Villalobos [EMAIL PROTECTED] wrote: Hi all, I want to retrieve the stats from a 'bwplot' with one factor. I have read the help for 'panel' function and I'm aware of the option 'stats' which defaults to 'boxplot.stats' but I didn't understand it well and therefore I am unable to get what I need. I'm not sure what bwplot has to do with this. Perhaps this will help: foo - with(OrchardSprays, split(decrease, treatment)) str(foo) List of 8 $ A: num [1:8] 2 2 5 4 5 12 4 3 $ B: num [1:8] 8 6 4 10 7 4 8 14 $ C: num [1:8] 15 84 16 9 17 29 13 19 $ D: num [1:8] 57 36 22 51 28 27 20 39 $ E: num [1:8] 95 51 39 114 43 47 61 55 $ F: num [1:8] 90 69 87 20 71 44 57 114 $ G: num [1:8] 92 71 72 24 60 77 72 80 $ H: num [1:8] 69 127 72 130 81 76 81 86 boxplot.stats(foo$A) $stats [1] 2.0 2.5 4.0 5.0 5.0 $n [1] 8 $conf [1] 2.603464 5.396536 $out [1] 12 bxp.stats - lapply(foo, boxplot.stats) str(bxp.stats) List of 8 $ A:List of 4 ..$ stats: num [1:5] 2 2.5 4 5 5 ..$ n: int 8 ..$ conf : num [1:2] 2.60 5.40 ..$ out : num 12 $ B:List of 4 ..$ stats: num [1:5] 4 5 7.5 9 14 ..$ n: int 8 ..$ conf : num [1:2] 5.27 9.73 ..$ out : num(0) $ C:List of 4 ..$ stats: num [1:5] 9 14 16.5 24 29 ..$ n: int 8 ..$ conf : num [1:2] 10.9 22.1 ..$ out : num 84 $ D:List of 4 ..$ stats: num [1:5] 20 24.5 32 45 57 ..$ n: int 8 ..$ conf : num [1:2] 20.5 43.5 ..$ out : num(0) $ E:List of 4 ..$ stats: num [1:5] 39 45 53 78 114 ..$ n: int 8 ..$ conf : num [1:2] 34.6 71.4 ..$ out : num(0) $ F:List of 4 ..$ stats: num [1:5] 20 50.5 70 88.5 114 ..$ n: int 8 ..$ conf : num [1:2] 48.8 91.2 ..$ out : num(0) $ G:List of 4 ..$ stats: num [1:5] 60 65.5 72 78.5 92 ..$ n: int 8 ..$ conf : num [1:2] 64.7 79.3 ..$ out : num 24 $ H:List of 4 ..$ stats: num [1:5] 69 74 81 106 130 ..$ n: int 8 ..$ conf : num [1:2] 62.8 99.2 ..$ out : num(0) If you want combinations defined by more than one factor, you could use something like with(OrchardSprays, split(decrease, interaction(treatment, colpos))) (although this is a bad example, since there is only one observation per combination) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lookups in R
On 7/4/07, Martin Morgan [EMAIL PROTECTED] wrote: Michael, A hash provides constant-time access, though the resulting perl-esque data structures (a hash of lists, e.g.) are not convenient for other manipulations n_accts - 10^3 n_trans - 10^4 t - list() t$amt - runif(n_trans) t$acct - as.character(round(runif(n_trans, 1, n_accts))) uhash - new.env(hash=TRUE, parent=emptyenv(), size=n_accts) ## keys, presumably account ids for (acct in as.character(1:n_accts)) uhash[[acct]] - list(amt=0, n=0) system.time(for (i in seq_along(t$amt)) { + acct - t$acct[i] + x - uhash[[acct]] + uhash[[acct]] - list(amt=x$amt + t$amt[i], n=x$n + 1) + }) user system elapsed 0.264 0.000 0.262 udf - data.frame(amt=0, n=rep(0L, n_accts), + row.names=as.character(1:n_accts)) system.time(for (i in seq_along(t$amt)) { + idx - row.names(udf)==t$acct[i] + udf[idx, ] - c(udf[idx,amt], udf[idx, n]) + c(t$amt[i], 1) + }) user system elapsed 18.398 0.000 18.394 I don't think that's a fair comparison--- much of the overhead comes from the use of data frames and the creation of the indexing vector. I get n_accts - 10^3 n_trans - 10^4 t - list() t$amt - runif(n_trans) t$acct - as.character(round(runif(n_trans, 1, n_accts))) uhash - new.env(hash=TRUE, parent=emptyenv(), size=n_accts) for (acct in as.character(1:n_accts)) uhash[[acct]] - list(amt=0, n=0) system.time(for (i in seq_along(t$amt)) { + acct - t$acct[i] + x - uhash[[acct]] + uhash[[acct]] - list(amt=x$amt + t$amt[i], n=x$n + 1) + }, gcFirst = TRUE) user system elapsed 0.508 0.008 0.517 udf - matrix(0, nrow = n_accts, ncol = 2) rownames(udf) - as.character(1:n_accts) colnames(udf) - c(amt, n) system.time(for (i in seq_along(t$amt)) { + idx - t$acct[i] + udf[idx, ] - udf[idx, ] + c(t$amt[i], 1) + }, gcFirst = TRUE) user system elapsed 1.872 0.008 1.883 The loop is still going to be the problem for realistic examples. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: shifting strips to left of axes
On 7/3/07, Michael Hoffman [EMAIL PROTECTED] wrote: [EMAIL PROTECTED] wrote: On 7/2/07, Michael Hoffman [EMAIL PROTECTED] wrote: Consider this plot: xyplot(mpg ~ disp | cyl, mtcars, strip=F, strip.left=T, layout=c(1, 3), scales=list(relation=free), par.settings=list(strip.background=list(col=transparent))) I want to have the cyl strip labels on the left side of the axis. Is this possible? No. (It's possible to have a legend there, which could be used to put row-specific ylab-s, for example, but it will be hard to make it look like strips) Thanks for the response. Not looking like a real strip is fine. What I want is essentially a secondary ylab for each row, and don't care about niceties such as shingle markings (I should have made the conditional factor(cyl) in the above plot). I thought this might be the case. But it looks like the legend goes to the left of the plot's ylab, and what I really want is for the secondary ylab to be between the primary ylab and the panel. So looks like I would have to eliminate the primary ylab from being drawn automatically and draw it myself in the legend? And I think I would have to manually calculate the panel heights as well, right? I don't see a way for the legend to get this out of the trellis object. It's possible, although it requires some advanced grid features. Luckily, this has come up before (search the r-help archives for myXlabGrob). Basically, you can use the fact that 'ylab' can be a grob to get what you want (I think). Here is a modified version of the original function (adapted to include a 'primary' ylab): library(grid) library(lattice) myYlabGrob - function(..., main.ylab = ) ## ...is lab1, lab2, etc { ## you can add arguments to textGrob for more control ## in the next line labs - lapply(list(...), textGrob, rot=90) main.ylab - textGrob(main.ylab, rot = 90) nlabs - length(labs) lab.heights - lapply(labs, function(lab) unit(1, grobheight, data=list(lab))) unit1 - unit(1.2, grobheight, data = list(main.ylab)) unit2 - do.call(max, lab.heights) lab.layout - grid.layout(ncol = 2, nrow = nlabs, heights = unit(1, null), widths = unit.c(unit1, unit2), respect = TRUE) lab.gf - frameGrob(layout=lab.layout) for (i in seq_len(nlabs)) { lab.gf - placeGrob(lab.gf, labs[[i]], row = i, col = 2) } lab.gf - placeGrob(lab.gf, main.ylab, col = 1) lab.gf } xyplot(mpg ~ disp | cyl, mtcars, strip=F, strip.left=F, layout=c(1, 3), scales=list(relation=free), ylab = myYlabGrob(4, 6, 8, main.ylab = mpg)) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Plots from categorial data
On 7/1/07, Jim Lemon [EMAIL PROTECTED] wrote: Christoph Krammer wrote: Hello everybody, Since my first message was caught by the spam filter, I just try to do it again: I want to use R to generate plots from categorial data. The data contains results from OCR scans over images with are preprocessed by different image filtering techniques. A small sample data set looks as following: data - read.csv(d:/tmp_da/sql_data/filter_d_tool.csv, header=T) data ocrtool filter_setting avg.hit. 1 FineReader2x10.383 2 FineReader2x20.488 3 FineReader3x20.268 4 FineReader3x30.198 5 FineReader4x30.081 6 FineReader4x40.056 7gocr2x10.153 8gocr2x20.102 9gocr3x20.047 10 gocr3x30.052 11 gocr4x30.014 12 gocr4x40.002 13 ocrad2x10.085 14 ocrad2x20.094 15 ocrad3x20.045 16 ocrad3x30.050 17 ocrad4x30.025 18 ocrad4x40.009 I now want to draw a plot with the categories (filter_setting) as X axis, and the avg_hit as Y axis. There should be lines for each ocrtool. But when I draw a plot, the resulting plot always contains bars, even if I specify type=n. plot(data$filter_setting, data$avg.hit., type=n) When I only plot the categories, without data, there appear strange grey (but empty) boxes. plot(data$filter_setting, type=n) Who do I get a clean white box to draw the different lines in? Hi Christoph, How about this? plot(as.numeric(krammer$filter_setting[1:6]),krammer$avg_hit[1:6], type=b,col=2,ylim=c(0,0.5),main=OCR performance, xlab=Filter setting,ylab=Average hits,axes=FALSE) points(as.numeric(krammer$filter_setting[7:12]),krammer$avg_hit[7:12], type=b,col=3) points(as.numeric(krammer$filter_setting[13:18]),krammer$avg_hit[13:18], type=b,col=4) box() axis(1,at=1:6,labels=c(2x1,2x2,3x2,3x3,4x3,4x4)) axis(2) And this is mostly equivalent to with(krammer, interaction.plot(filter_setting, ocrtool, avg_hit)) or (with the original names) with(data, interaction.plot(filter_setting, ocrtool, avg.hit.)) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Adding different output to different lattice panels
On 6/28/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I would like to add a reference line to lattice graphs, with the reference line being different according to the factor level. Example : Draw 3 dotplots for a,b and c factors, and then add an horizontal line at y=10 for panel a, y=8 for panel b and y=6 for panel 4 I tried the code below, but this draw all three reference lines for each panel. How do I index the current panel to chose the right reference vector value ? dat-data.frame(id=rep(c(a,b,c),4),val=1:12,quand=rep(c(t1,t2,t3,t4),each=3)) ref-c(10,8,6) plot.new() datplot-dotplot(val~quand|id,data=dat,panel=function(...){ panel.dotplot(...) panel.abline(h=ref) }) print(datplot) dotplot(val~quand|id,data=dat,panel=function(...){ panel.dotplot(...) panel.abline(h = ref[packet.number()]) }) (Things are more complicated if you have more than one conditioning variable.) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Adding different output to different lattice panels
On 6/28/07, Alexandre Salvador [EMAIL PROTECTED] wrote: Selon [EMAIL PROTECTED]: On 6/28/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: I would like to add a reference line to lattice graphs, with the reference line being different according to the factor level. Example : Draw 3 dotplots for a,b and c factors, and then add an horizontal line at y=10 for panel a, y=8 for panel b and y=6 for panel 4 I tried the code below, but this draw all three reference lines for each panel. How do I index the current panel to chose the right reference vector value ? dat-data.frame(id=rep(c(a,b,c),4),val=1:12,quand=rep(c(t1,t2,t3,t4),each=3)) ref-c(10,8,6) plot.new() datplot-dotplot(val~quand|id,data=dat,panel=function(...){ panel.dotplot(...) panel.abline(h=ref) }) print(datplot) dotplot(val~quand|id,data=dat,panel=function(...){ panel.dotplot(...) panel.abline(h = ref[packet.number()]) }) (Things are more complicated if you have more than one conditioning variable.) -Deepayan I tried you solution, but the following error appears when I print(datplot): Erreur dans as.numeric(h) : impossible de trouver la fonction packet.number I have lattice and grid libraries loaded. The exact code I use is datplot-dotplot(val~quand|id,data=dat,panel=function(...){ panel.dotplot(...) panel.abline(h=ref[packet.number()]) }) print(datplot) What do I do wrong ? You are using a very old version of R (or at least old enough that the packet.number number didn't exist). You haven't told us what version you are using. I would encourage you to upgrade, but this might work for now: dotplot(val~quand|id,data=dat, panel=function(..., panel.number){ panel.dotplot(...) panel.abline(h = ref[panel.number]) }) Note : I usually need to use the indirect datplot-dotplot(... and then print(datplot) when I store my code in a file and use it through source(myfile.R). The direct dotplot(... does not open a device whereas it does if I type directly dotplot(... at the R command line... Is this normal ? Yes, and a FAQ: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-do-lattice_002ftrellis-graphics-not-work_003f -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Changing graphics height when using grid and lattice
On 6/28/07, Jim Price [EMAIL PROTECTED] wrote: Hi, I have recently been playing with the grid package in an attempt to create some pages containing multiple lattice plots on the same page. However, when I specify a grid layout with different widths, such as: pushViewport(viewport(layout = grid.layout(1, 2, unit(c(2, 1), null the individual graphs do not end up as the same height - which is a feature I would prefer to have. The default padding between components of a graph is defined as a proportion of the plot area. In particular, it's unit(0.01, snpc), which makes it the same on both axes. The problem is that when you have two grid viewports, unit(0.01, snpc) may give different physical length. One option is to set all paddings to 0, which can be done with myLatticeSettings - function() list(layout.heights = list(top.padding = 0, main.key.padding = 0, key.axis.padding = 0, axis.xlab.padding = 0, xlab.key.padding = 0, key.sub.padding = 0, bottom.padding = 0), layout.widths = list(left.padding = 0, key.ylab.padding = 0, ylab.axis.padding = 0, axis.key.padding = 0, right.padding = 0) ) trellis.par.set(myLatticeSettings()) This may not be what you want, so another option is to set the paddings to something absolute; e.g. myLatticeOptions - function() list(layout.heights = list(top.padding = list(x = 1, units = mm, data = NULL), main.key.padding = list(x = 1, units = mm, data = NULL), key.axis.padding = list(x = 1, units = mm, data = NULL), axis.xlab.padding = list(x = 1, units = mm, data = NULL), xlab.key.padding = list(x = 1, units = mm, data = NULL), key.sub.padding = list(x = 1, units = mm, data = NULL), bottom.padding = list(x = 1, units = mm, data = NULL)), layout.widths = list(left.padding = list(x = 1, units = mm, data = NULL), key.ylab.padding = list(x = 1, units = mm, data = NULL), ylab.axis.padding = list(x = 1, units = mm, data = NULL), axis.key.padding = list(x = 1, units = mm, data = NULL), right.padding = list(x = 1, units = mm, data = NULL)) ) lattice.options(myLatticeOptions()) I'm not particularly attached to the snpc solution, so I may change them at some point. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] xyplot with par
On 6/27/07, Afshartous, David [EMAIL PROTECTED] wrote: All, Is there are a simple way to plot multiple xyplots on the same page in the code below (it currently overwrites the first plot w/ the second). I searched the archives and saw a similar question but the answer didn't seem to work. thanks dave x1 = rnorm(10) x2 = rnorm (10) y1 = rnorm(10) y2 = rnorm (10) op = par(mfrow = c(2,1)) xyplot(y1 ~ x1) xyplot(y2 ~ x2) See help(print.trellis), which has examples. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] levelplot in lattice
On 6/27/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, I'm new to lattice. So please kindly be patient with me. I'm trying to arrange groups of levelplots into 3 rows as follows: Row1: Probabilities as functions of x and y, and conditioned on an event factor vector factor(a,b,c) Row2: Number of days as functions of x and y, and conditioned on, again the same event factor(a,b,c) Row2: Costs as functions of x and y, and conditioned on, again the same event factor(a,b,c) I tried the following: windows(); par(mfrow=c(3,1)); levelplot(z ~ x*y|events, probDat); levelplot(z ~ x*y|events, daysDat); levelplot(z ~ x*y|events, costDat); It does not do what I want. It replace the previous plot every time I call a new levelplot. I can't put them into the same matrix and plot them all at once because the scale of each data type is very different, i.e.g probability=c(0,1), days=c(0,10), etc. I'm wondering if you could suggest a way to solve this problem. It all depends on the details. Please give a small reproducible example. Also, if I would like to put in mathematical notation on the top strip of the plot instead of using text in my events factor vector, what can I do? Use expressions (see ?plotmath). Again, please give details. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Sweave bug? when writing figures / deleting variable in chunk
On 6/27/07, D G Rossiter [EMAIL PROTECTED] wrote: I have found a quite strange (to me) behaviour in Sweave. It only occurs in the following situation: 1. define a variable in one chunk 2. use it within a subsequent figure-generating chunk 3. delete it at the end of that same chunk Then the Sweave driver chokes, not finding the variable name when generating the figure The reason is that by default, every fig=TRUE chunk is run twice, once to produce postscript, once for pdf. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] levelplot in lattice
On 6/27/07, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Sorry. My email editor from my ISP always screws up the text after sending out. Below is my response to you (in plain text). Thank you Deepayan. Let's do the following exercise to reproduce the problem I'm facing. In the following code, I tried to have plot.rw1 and plot.rw2 shows in two separate rows on my plot windows. However, after the call to the 2nd levelplot, plot of plot.rw2 replace my first plot and the result shows only 1 row on my windows. Any lights on this would be really appreciated. Thank you x - seq(0.1,1,0.1); y - seq(0.1,1,0.1); dat - rnorm(4*length(x)*length(y)); Pa.dat - expand.grid(x,y); Pa.dat$z - dat[1:100]; Pa.dat$cond - Pa; plot.rw1 - Pa.dat; Pb.dat - expand.grid(x,y); Pb.dat$z - dat[101:200]; Pb.dat$cond - Pb; plot.rw1 - rbind(plot.rw1, Pb.dat); names(plot.rw1) - c(x,y,z,cond); Days1.dat - expand.grid(x,y); Days1.dat$z - dat[201:300]; Days1.dat$cond - Day Work; plot.rw2 - Days1.dat; Days2.dat - expand.grid(x,y); Days2.dat$z - dat[301:400]; Days2.dat$cond - Day Rest; plot.rw2 - rbind(plot.rw2, Days2.dat); names(plot.rw2) - c(x,y,z,cond); windows(); par(mfrow=c(2,1)); levelplot(z ~ x*y|cond, plot.rw1); levelplot(z ~ x*y|cond, plot.rw2); ## option 1: levelplot(z ~ x * y | cond, make.groups(plot.rw1, plot.rw2)) ## equivalent in this case levelplot(z ~ x * y | cond, rbind(plot.rw1, plot.rw2)) ## option 2 (not exactly the same) print(levelplot(z ~ x*y|cond, plot.rw1), split = c(1, 2, 1, 2)) print(levelplot(z ~ x*y|cond, plot.rw2), split = c(1, 1, 1, 2), newpage = FALSE) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Overlaying lattice graphs (continued)
On 6/22/07, Sébastien [EMAIL PROTECTED] wrote: Hi Deepayan, The following code creates a dummy dataset which has the same similar as my usual datasets. I did not try to implement the changes proposed by Hadley, hoping that a solution can be found using the original dataset. # My code # Creating dataset nPts-10# number of time points nInd-6 # number of individuals nModel-3 # number of models TimePts-rep(1:nPts,nInd*nModel)# creates the Time column Coef-rep(rnorm(6,0.1,0.01),each=nPts,nModel) # Creates a vector of coefficients for generating the observations Obs-10*exp(-Coef*TimePts) # creates the observations for (i in 1:60){ Pred[i]-jitter(10*exp(-Coef[i]*TimePts[i])) Pred[i+60]-jitter(5) Pred[i+120]-jitter(10-Coef[i+120]*TimePts[i]) } # creates the predicted values colPlot-rep(1,nPts*nInd*nModel) # creates the Plot column colModel-gl(nModel,nPts*nInd,labels=c(A,B,C)) # creates the Model column colID-gl(nInd,nPts,nPts*nInd*nModel) # creates the ID column mydata-data.frame(colPlot,colModel,colID,TimePts,Obs,Pred) # creates the dataset names(mydata)-c(Plot,Model,Individuals,Time,Observed,Predicted) The way you have structured your data makes no sense to me. In particular, your 'Observed' data is the same set of 60 numbers repeated 3 times, and this is not reflected in the data structure at all. What would you want to happen if the numbers were not repeated? Would you always plot the first 60, or would plot all of them? If I understand what you are trying to do, this might be a more transparent approach: nPts-10 # number of time points nInd-6# number of individuals TimePts - rep(1:nPts, nInd) Coef - rep(rnorm(6,0.1,0.01), each = nPts) Obs - 10 * exp(-Coef * TimePts) colID - gl(nInd, nPts) mydata - data.frame(Time = TimePts, Observed = Obs, Individuals = colID) fmA - lm(Observed ~ Time, mydata) fmB - lm(Observed ~ poly(Time, 2), mydata) fmC - lm(Observed ~ poly(Time, 2) * Individuals, mydata) mydata$PredA - predict(fmA) mydata$PredB - predict(fmB) mydata$PredC - predict(fmC) xyplot(Observed + PredA + PredB + PredC ~ Time | Individuals, data = mydata, type = c(p, l, l, l), distribute.type = TRUE) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: hiding only some strips
On 6/22/07, Michael Hoffman [EMAIL PROTECTED] wrote: I am using R 2.4.0 and lattice to produce some xyplots conditioned on a factor and a shingle. The shingle merely chops up the data along the x-axis, so it is easy to identify which part of the shingle a panel is in by looking at the x-axis markings. I only want to have a strip at the top for the factor. Is this possible? I looked into calculateGridLayout() and it seems to me that there isn't an easy way to do it without rewriting that function (and others). It's nowhere near that complicated, you just need to write your own strip function. E.g., mtcars$HP - equal.count(mtcars$hp) xyplot(mpg ~ disp | HP + factor(cyl), mtcars, par.strip.text = list(lines = 0.5), strip = function(which.given, which.panel, ...) { if (which.given == 2) strip.default(which.given = 1, which.panel = which.panel[which.given], ...) }) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Barchart legend position
On 6/22/07, Spilak,Jacqueline [Edm] [EMAIL PROTECTED] wrote: I am using barchart to make charts for some data with a lot more functions and labels and such in the command. barchart(Freq ~ factor(HH), data = dataset1, group= year) So I have my data grouped by year and I get a legend at the top of graph, which is great cause I need the legend for the different years but it is a weird spot. So how can I manipulate the legend, ie. Move it, shrink it, do anything with it. I have searched the help archives and found nothing, and I have looked up the legend section in ?barchart but that has not helped or I am doing something wrong. Any help is greatly appreciated. I can be more specific if you say what exactly you want to do (preferably with a small reproducible example). The relevant documentation is the part under 'key' in help(barchart). I prefer to use 'auto.key' instead (and you haven't told us what you are using), but most components of 'key' can be passed through 'auto.key'. Some examples: barchart(Titanic, auto.key = list(space = right, size = 2, cex = 0.5)) barchart(Titanic, auto.key = list(x = 0.75, y = 0.25, size = 2)) ## choose location interactively: library(grid) barchart(Titanic, page = function(n) { cat(Click on plot to place legend, fill = TRUE) ll - grid.locator(unit = npc) if (!is.null(ll)) draw.key(simpleKey(dimnames(Titanic)$Survived, rect = TRUE, points = FALSE), vp = viewport(x = ll$x, y = ll$y), draw = TRUE) }) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lattice: hiding only some strips
On 6/22/07, Michael Hoffman [EMAIL PROTECTED] wrote: Deepayan Sarkar wrote: On 6/22/07, Michael Hoffman [EMAIL PROTECTED] wrote: I am using R 2.4.0 and lattice to produce some xyplots conditioned on a factor and a shingle. The shingle merely chops up the data along the x-axis, so it is easy to identify which part of the shingle a panel is in by looking at the x-axis markings. I only want to have a strip at the top for the factor. Is this possible? I looked into calculateGridLayout() and it seems to me that there isn't an easy way to do it without rewriting that function (and others). It's nowhere near that complicated, you just need to write your own strip function. E.g., mtcars$HP - equal.count(mtcars$hp) xyplot(mpg ~ disp | HP + factor(cyl), mtcars, par.strip.text = list(lines = 0.5), strip = function(which.given, which.panel, ...) { if (which.given == 2) strip.default(which.given = 1, which.panel = which.panel[which.given], ...) }) Thank you for this response. But it looks like I poorly specified the problem. I only want to have a strip at the very top of the plot, not at the top of each panel. You can probably understand why I want this better if we take your example and swap the givens around: xyplot(mpg ~ disp | factor(cyl) * HP, mtcars, par.strip.text = list(lines = 0.5), strip = function(which.given, which.panel, ...) { if (which.given == 1) strip.default(which.given = 1, which.panel = which.panel[which.given], ...) }) So now I have 4, 6, and 8 at the top of every row of panels as a label for cyl. But I don't need that--really I only need 4, 6, and 8 at the very top (or bottom) of the plot, just like with default settings I only get the axis labels at the top and bottom of the plot, not duplicated for every panel. What I want is to draw strips at the very top of the plot and not to draw strips that are between panels. Can this be done? Sure. xyplot(mpg ~ disp | factor(cyl) * HP, mtcars, par.strip.text = list(lines = 0.5), strip = function(which.given, which.panel, ...) { if (which.given == 1) strip.default(which.given = 1, which.panel = which.panel[which.given], ...) }, par.settings = list(layout.heights = list(strip = rep(c(0, 1), c(5, 1) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Overlaying lattice graphs (continued)
On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Hi Hadley, Hopefully, my dataset won't be too hard to changed. Can I modify the aspect of each group using your code (symbols for observed and lines for predicted)? Sebastien hadley wickham a écrit : Hi Sebastian, I think you need to rearrange your data a bit. Firstly, you need to put observed on the same footing as the different models, so you would have a new column in your data called value (previously observed and predicted) and a new model type (observed). Then you could do: Yes, and ?make.groups (and reshape of course) could help with that. This might not be strictly necessary though. However, I'm finding your pseudo-code confusing. Could you create a small example data set that can be used to try out some real code? Just from your description, I would have suggested something like xyplot(Observed + Predicted ~ Time | Individuals + Model, data = mydata, panel = panel.superpose.2, type = c(p, l), layout = c(0, nlevels(mydata$Individuals)), ...) If all you want is to plot one page at a time, there are easier ways to do that. -Deepayan xyplot(value ~ time | individauls, data=mydata, group=model) Hadley On 6/21/07, Sébastien [EMAIL PROTECTED] wrote: Dear R Users, I recently posted an email on this list about the use of data.frame and overlaying multiple plots. Deepayan kindly indicated to me the panel.superposition command which worked perfectly in the context of the example I gave. I'd like to go a little bit further on this topic using a more complex dataset structure (actually the one I want to work on). mydata PlotModelIndividualsTimeObserved Predicted 11A 1 0.05 1010.2 21A 1 0.10 2019.5 etc... 10 1B 1 0.05 10 9.8 11 1B 1 0.10 20 20.2 etc... There are p levels in mydata$Plot, m in mydata$Model, n in mydata$Individuals and t in mydata$Time (Note that I probably use the word levels improperly as all columns are not factors). Basically, this dataset summarizes the t measurements obtained in n individuals as well as the predicted values from m different modeling approaches (applied to all individuals). Therefore, the observations are repeated m times in the Observed columns, while the predictions appears only once for a given model an a given individual. What I want to write is a R batch file creating a Trellis graph, where each panel corresponds to one individual and contains the observations (as scatterplot) plus the predicted values for all models (as lines of different colors)... $Plot is just a token: it might be used to not overload graphs in case there are too many tested models. The fun part is that the values of p, m, n and t might vary from one dataset to the other, so everything has to be coded dynamically. For the plotting part I was thinking about having a loop in my code containing something like that: for (i in 1:nlevels(mydata$Model)) { subdata-subset(mydata,mydata$Model=level(mydata$Model)[i]) xyplot(subset(Observed + Predicted ~ Time | Individuals, data = subdata) #plus additionnal formatting code } Unfortunately, this code simply creates a new Trellis plot instead of adding the model one by one on the panels. Any idea or link to a useful command will wellcome. Sebastien __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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. __ R-help@stat.math.ethz.ch 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.
Re: [R] barchart in trellis and NA
On 6/21/07, Sigbert Klinke [EMAIL PROTECTED] wrote: Hi, I've a dataset with discrete data and several groups and in one group I have also missing values (NA). When I use table and barchart to visualize the counts I never get a bar for NA in the barchart although it appears in the result of table. Is there a possibility to get this bar too? Please give a reproducible example, because it's not clear to me what exactly is supposed to be NA. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Retrieve part of (top right corner) of a plot.data.frame plot?
On 6/20/07, Dan Bolser [EMAIL PROTECTED] wrote: Hi, I believe this question has been asked before, but I cant find and don't remember the answer. The problem is simple, calling 'plot.data.frame(x)' gives a nice 'matrix of scatterplots' for each pair of columns in x. for example; x - data.frame(a=jitter(01:20), b=jitter(20:01), c=jitter(21:40), d=jitter(rep(01,20)), e=jitter(rep(10,20)), f=jitter(rep(20,20)) ) plot(x) gives a 6 by 6 grid of scatter plots, two (upper right and lower left) for each pair of columns in x. (I am going over these basics so that you can understand what I mean next). I would like to see just part of the above result, namely the nine plots in the top right of the given plot, or; a vs. d | a vs. e | a vs. f b vs. d | b vs. e | b vs. f c vs. d | c vs. e | c vs. f I tried a number of ways to do this, but I can't find either the right formula or the right function to get what I want. Any suggestions you can give (especially any not involving the source code of 'pairs') are most welcome. Lattice gets you close: xyplot(a + b + c ~ d + e + f, data = x, outer = TRUE, scales = free, layout = c(3, 3), aspect = 1) The rest may or may not be simple, depending on what you want exactly. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Controlling text and strip arrangement in xyplot
On 6/19/07, Juan Pablo Lewinger [EMAIL PROTECTED] wrote: I've searched the archives and read the xyplot help but can't figure out the 2 lattice questions below? Consider: library(lattice) DF - data.frame(x=rnorm(20), y=rnorm(20), g1=rep(letters[1:2], 10), g2=rep(LETTERS[1:2], each=10), g3=rep(rep(letters[3:4],each=5),2)) xyplot(y ~ x | g1 + g2, groups=g3, data=DF) 1) Is there a way to get one strip per row and column of panels as below instead of the default? _|__a__|__b__| | B | -- | A | This has been discussed on the list before (if I remember correctly), and I have been meaning to add something to the latticeExtra package. An implementation would look something like this (beware of line wrapping): useOuterStrips - function(x, strip = strip.default, strip.left = strip.custom(horizontal = FALSE)) { dimx - dim(x) stopifnot(inherits(x, trellis)) stopifnot(length(dimx) == 2) opar - if (is.null(x$par.settings)) list() else x$par.settings par.settings - modifyList(opar, list(layout.heights = list(strip = c(rep(0, dimx[2]-1), 1)), layout.widths = list(strip.left = c(1, rep(0, dimx[1]-1) update(x, par.settings = par.settings, strip = function(which.given, which.panel, ...) { if (which.given == 1) strip(which.given = 1, which.panel = which.panel[1], ...) }, strip.left = function(which.given, which.panel, ...) { if (which.given == 2) strip.left(which.given = 1, which.panel = which.panel[2], ...) }, par.strip.text = list(lines = 0.5), layout = dimx) } The function acts on a trellis object and returns an updated one, so for your example, it would work like: useOuterStrips(xyplot(y ~ x | g1 + g2, groups=g3, data=DF)) 2) How do I control the text of the strips so that for instance instead of a and b it readsg1=alpha, g1=beta where alpha and beta stand for the corresponding greek symbols? (my difficulty here is not with the plotmath symbols but with controlling the text of the strips directly from the call to xyplot and not by renaming the levels of g1) Generally speaking, you need to write your own strip function. The default (strip.default) has some useful arguments that modify its behaviour, and in particular 'factor.levels' might do what you want. If you are going to do this in conjunction with (1), life will actually be simpler and you can get away with using strip.custom(): useOuterStrips(xyplot(y ~ x | g1 + g2, groups=g3, data=DF), strip = strip.custom(factor.levels = expression(g[1]==alpha, g[1]==beta)), strip.left = strip.custom(horizontal = FALSE, factor.levels = expression(g[2]==gamma, g[2]==delta))) Otherwise, you will really have to write a proper strip function that calls strip.default() with different values of 'factor.levels' depending on the value of 'which.given'. Note also the 'strip.names' and 'sep' argument of strip.default(), which might be more in line with what you want to do. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Histograms with strings, grouped by repeat count (w/ data)
On 6/18/07, Matthew Trunnell [EMAIL PROTECTED] wrote: Aha! So to expand that from the original expression, table(table(d$filename, d$email_addr)) 0 1 2 3 253 20 8 9 I think that is exactly what I'm looking for. I knew it must be simple!!! What does the 0 column represent? Number of unique filename:email_addr combinations that don't occur in the data. Also, does this tell me the same thing, filtered by Japan? table(table(d$filename, d$email_addr, d$country_residence)[d$country_residence==Japan]) 0 1 2 3 958 5 2 1 No it doesn't. length(table(d$filename, d$email_addr, d$country_residence)) [1] 4350 length(d$country_residence) [1] 63 You are using an index that is meaningless. There's an alternative tabulation function that uses a formula interface similar to that used in modeling functions; this might be more transparent for your case: count - + xtabs(~filename + email_addr, data = d, + subset = country_residence == Japan) xtabs(~count) count 0 1 3 284 2 4 How does that differ logically from this? table(table(d$filename, d$email_addr)[d$country_residence==Japan]) 0 1 2 3 51 4 2 1 This is also using meaningless indexing. Note, incidentally, that you are indexing a matrix of dimension 10x29 as if it were a vector of length 290, which is probably not what you meant to do anyway: str(table(d$filename, d$email_addr)) 'table' int [1:10, 1:29] 1 0 0 0 0 0 0 0 0 0 ... - attr(*, dimnames)=List of 2 ..$ : chr [1:10] file1 file10 file2 file3 ... ..$ : chr [1:29] email1 email10 email11 email12 ... You need to read help(Extract) carefully and play around with some simple examples. I don't understand why that produces different results. The first one adds a third dimension to the table, but limits that third dimension to a single element, Japan. Shouldn't it be the same? And again, what's that zero column? As before, they are the empty combinations. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] plotting order of lines in xyplot panels while using conditioning variable and groups
On 6/19/07, RICHARD PITMAN [EMAIL PROTECTED] wrote: I am using the following code: library(lattice) data-read.csv(data.csv) attach(data) fig-xyplot(S_t~month|event, key= list(text=list(lab=c(Time to first CV event - Data, Survival post first CV event - Model, Survival post first MIA/CA event - Data, Survival post first CVA event - Data, Survival post first TIA event - Data, Survival post first CVA/TIA event - Model), font=2, cex=0.55), lines=list(col=c(red, magenta, blue, brown), type=l, cex=0.55, lwd=1.5, pch=0:1), corner=c(0,0),x=0.75,y=0.75), group = group, index.cond = list(c(4,5,6,7,1,2,3)), type = l, lwd= 3, ylim = c(0,1.5), layout = c(4,2), col= c(red,magenta,blue,brown), pch= 0:3, cex= 0.5, ylab = cumulative probability, xlab = months, ) print(fig) However, in each panel, the order in which the lines are printed is suboptimal as some shorter lines are obscured under longer lines. I am having some trouble finding a method to change the order in which curves are plotted in each panel. I have tried reordering the levels in data$group: data$group-factor(data$group, levels=c(CV_model_event_1,CV model event 2,CV event 1,CV_event_2,CV_event_2_CVA,CV_event_2_TIA)) but this changed nothing. Any suggestions gratefully received. It's hard to see the problem without a reproducible example, but if all you want is to order the levels of groups in decreasing order of frequency, you could use groups = reorder(group, group, function(x) { -length(x) }), -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Second y-axis in xyplot (lattice) where y1 and y2 have different ranges
On 6/18/07, Andy Bunn [EMAIL PROTECTED] wrote: Hi all, I realize this is asking a lot of lattice, but I want to add a second y axis inside a xyplot and have y1 and y2 have different ranges. Given dat below, I can add a second y axis by overlaying a new plot with par(new=T) and label axis 4 with standard graphics. I've seen an example for doing something similar in xyplot even though Deepayan has indicated that lattice isn't the right tool for the job. However, is there a way to gracefully add a second y-axis to a xyplot where y1 and y2 have different scales as in the example below? I've seen the experimental tools to focus and modify lattice graphics but do not know if these are applicable. You could use those, but one drawback there is that you don't get the usual benefit of automatic allocation of space. Here is a ``better'' solution (as long as you realize that this is still a hack): [Note: this won't work if scales=free or sliced] [...] dat2 - data.frame(Year = rep(1751:2000,2), Stuff = rep(rnorm(250),2), Samples = rep(floor(seq(5,30,length.out=250)+ rnorm(250,5)),2), Grp = c(rep('SiteOne',250), rep('SiteTwo',250))) scale.pars - function(x) { c(mx = min(x), dx = diff(range(x))) } rescale - function(x, pars = scale.pars(x)) { (x - pars[mx]) / pars[dx] } pars.Stuff - scale.pars(dat2$Stuff) pars.Samples - scale.pars(dat2$Samples) rng.Stuff - range(dat2$Stuff) rng.Samples - range(dat2$Samples) my.yscale.components - function(lim, ...) { ## template we will modify ans - yscale.components.default(lim, ...) ## labels for Stuff in original scale Stuff - yscale.components.default(rng.Stuff, ...) Stuff$left$ticks$at - rescale(Stuff$left$ticks$at, pars.Stuff) Stuff$left$labels$at - rescale(Stuff$left$labels$at, pars.Stuff) ## labels for Samples in original scale Samples - yscale.components.default(rng.Samples, ...) Samples$left$ticks$at - rescale(Samples$left$ticks$at, pars.Samples) Samples$left$labels$at - rescale(Samples$left$labels$at, pars.Samples) ## modified 'components' ans$left - Stuff$left ans$right - Samples$left ans } xyplot(rescale(Stuff, pars.Stuff) + rescale(Samples, pars.Samples) ~ Year | Grp, data=dat2, panel = panel.superpose.2, ## newlay added: yscale.components = my.yscale.components, scales = list(alternating = 3), ylab = Stuff, legend = list(right = list(fun = grid::textGrob(Samples, rot = 90))), type = c('l', 'l')) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] [OT] 'gv' and fractional points
On 6/15/07, Ted Harding [EMAIL PROTECTED] wrote: Hi Folks, This is off-topic R-wise, but it may be close to the heart of many R-users, so I think it may be the best place to ask! Users of 'gv' (the front end to ghostscript) will be aware of the little window which gives you the x-y coordinates (in points = 1/72 inch) of the position of the cross-hair mouse cursor. These coordinates are those of the corresponding position on the printed page, relative to some origin. I have often used this to extract numerical values for data from graphs in Postscript files (also PDF files, after you have converted them to PS). Then (veering back on topic ... ) you can submit the numerical data to R and try your own analyses on these data, and compare with what the article does. However, this little window only gives the numbers in whole points. Say a smallish graphic may print out 3 inches wide or high. Then you get precision of 1/216 per 3 inches or 0.4% of full scale. This can be adequate on many occasions, but can be on the coarse side on other occasions. If you are mostly concerned about small figures, one possibility is 1. zoom out to a level where you're happy with the pixel resolution 2. do a screen capture using 'import' 3. use gimp (which has the same feature, with more units) gimp can also load PS files directly, with a user supplied zoom factor at load time, but only one page at a time, AFAICT. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] model.frame: how does one use it?
On 6/15/07, Dirk Eddelbuettel [EMAIL PROTECTED] wrote: Philipp Benner reported a Debian bug report against r-cran-rpart aka rpart. In short, the issue has to do with how rpart evaluates a formula and supporting arguments, in particular 'weights'. A simple contrived example is - library(rpart) ## using data from help(rpart), set up simple example myformula - formula(Kyphosis ~ Age + Number + Start) mydata - kyphosis myweight - abs(rnorm(nrow(mydata))) goodFunction - function(mydata, myformula, myweight) { hyp - rpart(myformula, data=mydata, weights=myweight, method=class) prev - hyp } goodFunction(mydata, myformula, myweight) cat(Ok\n) ## now remove myweight and try to compute it inside a function rm(myweight) badFunction - function(mydata, myformula) { myweight - abs(rnorm(nrow(mydata))) mf - model.frame(myformula, mydata, myweight) print(head(df)) hyp - rpart(myformula, data=mf, weights=myweight, method=class) prev - hyp } badFunction(mydata, myformula) cat(Done\n) - Here goodFunction works, but only because myweight (with useless random weights, but that is not the point here) is found from the calling environment. badFunction fails after we remove myweight from there: :~ cat /tmp/philipp.R | R --slave Ok Error in eval(expr, envir, enclos) : object myweight not found Execution halted :~ As I was able to replicate it, I reported this to the package maintainer. It turns out that seemingly all is well as this is supposed to work this way, and I got a friendly pointer to study model.frame and its help page. Now I am stuck as I can't make sense of model.frame -- see badFunction above. I would greatly appreciate any help in making rpart work with a local argument weights so that I can tell Philipp that there is no bug. :) I don't know if ?model.frame is the best place page to look. There's a more detailed description at http://developer.r-project.org/nonstandard-eval.pdf but here are the non-standard evaluation rules as I understand them: given a name in either (1) the formula or (2) ``special'' arguments like 'weights' in this case, or 'subset', try to find the name 1. in 'data' 2. failing that, in environment(formula) 3. failing that, in the enclosing environment, and so on. By 'name', I mean a symbol, such as 'Age' or 'myweight'. So basically, everything is as you would expect if the name is visible in data, but if not, the search starts in the environment of the formula, not the environment where the function call is being made (which is the standard evaulation behaviour). This is a feature, not a bug (things would be a lot more confusing if it were the other way round). With this in mind, either of the following might do what you want: badFunction - function(mydata, myformula) { mydata$myweight - abs(rnorm(nrow(mydata))) hyp - rpart(myformula, data=mydata, weights=myweight, method=class) prev - hyp } badFunction - function(mydata, myformula) { myweight - abs(rnorm(nrow(mydata))) environment(myformula) - environment() hyp - rpart(myformula, data=mydata, weights=myweight, method=class) prev - hyp } -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] model.frame: how does one use it?
On 6/15/07, hadley wickham [EMAIL PROTECTED] wrote: On 6/15/07, Deepayan Sarkar [EMAIL PROTECTED] wrote: [...] By 'name', I mean a symbol, such as 'Age' or 'myweight'. So basically, everything is as you would expect if the name is visible in data, but if not, the search starts in the environment of the formula, not the environment where the function call is being made (which is the standard evaulation behaviour). This is a feature, not a bug (things would be a lot more confusing if it were the other way round). Could you give an example? It's always seemed confusing to me and I don't see why looking in the environment of the formula helps. Good question. I remember being convinced that it was a good idea, but no longer remember why. This is the best I can come up with right now: ## stupid function that fits a model on a random subsample lmsub - function(formula, data, p = 0.7) { n - nrow(data) newdata - data[sample(round(n * p)), ] lm(formula, newdata) } mydata - data.frame(x = 1:100, y = rnorm(100)) n - 2 lmsub(y ~ poly(x, n), data = mydata) I don't think the use of the name 'n' is unusual in either case, and we definitely wouldn't want the one inside 'lmsub' to be used for the formula. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] model.frame: how does one use it?
On 6/15/07, Philipp Benner [EMAIL PROTECTED] wrote: Thanks for your explanation! With this in mind, either of the following might do what you want: badFunction - function(mydata, myformula) { mydata$myweight - abs(rnorm(nrow(mydata))) hyp - rpart(myformula, data=mydata, weights=myweight, method=class) prev - hyp } badFunction - function(mydata, myformula) { myweight - abs(rnorm(nrow(mydata))) environment(myformula) - environment() hyp - rpart(myformula, data=mydata, weights=myweight, method=class) prev - hyp } OK, this is what I have now: adaboostBad - function(formula, data) { ## local definition of the weight vector (won't work because pima.formula is not defined within this function) w - abs(rnorm(nrow(data))) rpart(formula, data=data, weights=w) } adaboostGood - function(formula, data) { ## create weight vector in the data object data$w - abs(rnorm(nrow(data))) rpart(formula, data=data, weights=w) } adaboostBest - function(formula, data) { ## associate the current environment (this function's one) with the object `formula' environment(formula) - environment() w - abs(rnorm(nrow(data))) rpart(formula, data=data, weights=w) } As far as I understand this non-standard evaluation stuff, adaboostGood() and adaboostBest() are the only two possibilities to call rpart() with weight vectors. Now suppose that I don't know what `data' contains and suppose further that it already contains a column called `w'. adaboostGood() would overwrite that column with new data which is then used as weight vector and as training data for rpart(). adaboostBest() would just use the wrong data as weight vector as it finds data$w before the real weight vector. So, in both cases I have to check for `names(data) == w` and stop if TRUE? Or is there a better way? Well, that depends on what you want to happen when there is a column called 'w' in data. I don't see a situation where it makes sense to use data$w as weights ('w' is just a name you happen to choose inside adaboostBest), so I would just go with adaboostGood. In case you are worried about overwriting the original data, that may not be happening in the sense you are thinking. When you say data$w - abs(rnorm(nrow(data))) inside adaboostGood, that modifies a local copy of the data argument, not the original (R argument semantics are call by value, not call by reference). You are losing data$w in the local copy in your function, but why would you care if you are not using it anyway. Of course, if your formula contains a reference to 'w' then you will get wrong results, so checking for a unique name is always safer. In addition, use an obfuscated name like '.__myWeights' instead of 'w', and the check will be almost always irrelevant. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] plot via xyplot not being saved
On 6/15/07, Benilton Carvalho [EMAIL PROTECTED] wrote: Hi everyone, it's been a while I've been trying to save a plot created via lattice:::xyplot if I have a file tst.R with the following code: y - rnorm(100) x - rnorm(100) z - sample(letters[1:4], 100, rep=T) library(lattice) bitmap(tst.png) xyplot(y~x|z) dev.off() and I source it, I get the tst.png file, which is a blank page. If I copy and paste instead, I get the correct plot. Any suggestion? Use source(..., echo = TRUE) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] plot via xyplot not being saved
On 6/15/07, Benilton Carvalho [EMAIL PROTECTED] wrote: So, if those statements are inside a function, I have to make my function to have an 'echo' argument/functionality? eg.: ## begin test.R test - function(n){ y - rnorm(n) x - rnorm(n) z - sample(letters[1:4], n, rep=T) library(lattice) bitmap(tst.png) xyplot(y~x|z) dev.off() } test(100) ## end test.R source(test.R, echo=T) also fails in this case... Yes. The following will produce some output (the values of x + y and x - y) if you type it out at the R prompt: x - rnorm(10) y - rnorm(10) x + y x - y If you put that in a file and source it, nothing will get printed, unless you have echo=TRUE. If you define test - function(){ x - rnorm(10) y - rnorm(10) x + y x - y } calling test() at the R prompt will only print x - y and not x + y, and so on. This is all standard R behaviour. If you want something to be printed irrespective of context, use print(), e.g. print(x + y) or print(xyplot(y~x|z)) This is also mentioned in the R FAQ. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Annotating trellis graphics
On 6/13/07, Alan S Barnett [EMAIL PROTECTED] wrote: I'm using xyplot to generate a trellis plot with each panel containing a scatterplot and a best fit line. Is it possible to write the slope of the best fit line in each panel? Sure. The only question is, where (inside the panel) do you want it written? Here are a couple of possibilities: ## writes the slope at a location that happens to be empty in both ## panels in this example xyplot(len ~ dose | supp, ToothGrowth, panel = function(x, y, ...) { panel.xyplot(x, y, ...) fm - lm(y ~ x) panel.abline(reg = fm) slope - round(coef(fm)[2], 3) panel.text(1.5, 5, lab = slope) }) ## needs the user to click on a suitable position for each panel library(grid) xyplot(len ~ dose | supp, ToothGrowth, panel = function(x, y, ...) { panel.xyplot(x, y, ...) fm - lm(y ~ x) panel.abline(reg = fm) slope - round(coef(fm)[2], 3) message(Click on desired location) panel.text(grid.locator(native), lab = slope) }) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] format.dates, chron and Hmisc
On 6/10/07, R.H. Koning [EMAIL PROTECTED] wrote: Hello, I have some problems in using chron, Hmisc, and lattice. First, using both chron and Hmisc, I get an error message when describing data: df$Date - chron(df$Date,format=c(d/m/y)) ll - latex(describe(df),file=..//text//df.tex) Error in formatDateTime(dd, atx, !timeUsed) : could not find function format.dates Then, using a chron object and lattice, I get plot.a - xyplot(theta~Date|team,data=op.df.long, + strip = function(bg, ...) strip.default(bg = 'transparent', ...), + panel=function(x,y,...){ + panel.xyplot(x,y,cex=0.4,col=black,...) + panel.loess(x,y,span=0.3,col=black,...) + panel.abline(h=0) + }) print(plot.a) Error in pretty(rng, ...) : unused argument(s) (format.posixt = NULL) This one is a bug in lattice (there is some code to support chron objects, but evidently not many people use it). I will include a fix in the next update. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Stacked barchart color
On 6/12/07, Dieter Menne [EMAIL PROTECTED] wrote: Dear Latticer, I want to give individual colors to all elements in a simple stacked barchart. I know why the example below does not work (and it is a excellent default), but is there any workaround for this? Dieter # This only colors red and green, but I want blue and gray for Peatland. barchart(yield ~ variety , groups=year, data = barley, stack = TRUE, subset=site==Grand Rapids variety %in% c(Velvet,Peatland), col=c(red,green,blue,gray)) The easiest way is to create a new factor with suitable levels: replace groups=year by groups=year:variety or the more verbose groups=interaction(year, variety) -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] 2 Trellis graphics question
On 6/12/07, Alan S Barnett [EMAIL PROTECTED] wrote: 1) I have a data that includes some bad data. I want to make a trellis plot where each panel contains a) A scatter plot of the good data b) A scatter plot of the bad data in a different color c) A best fit line of all the data, and d) A best fit line of the good data. I tried using xyplot and setting the group argument, but I'm having trouble. Here is my code: xyplot(y ~ x | status, data=data,groups=good, + panel=function(x,y,...){ + panel.xyplot(x,y,...) + panel.lmline(x,y,col = red) + panel.lmline(x[good],y[good],col = blue) + } + ) You are close, except the last panel.lmline call is not meaningful because the 'x' and 'y' in the panel function are not the same length as 'good'. You need to use 'subscripts' for that. To fix ideas, here's a concrete example (I've changed the names 'x' and 'y' to 'xx' and 'yy' to avoid any confusion): mydata - data.frame(xx = sample(100), yy = rnorm(100) + rep(c(5, 10), c(80, 20)), status = gl(5, 1, 100), good = rep(c(TRUE, FALSE), c(80, 20))) Then, what you want can be achieved with: xyplot(yy ~ xx | status, mydata, groups = good, panel = function(x, y, groups, subscripts, ...) { panel.xyplot(x, y, groups = groups, subscripts = subscripts, ...) panel.lmline(x, y, col = red) good.id - groups[subscripts] ## good.id: subset of 'good' relevant for this panel panel.lmline(x[good.id], y[good.id], col = blue) }) This also works if 'good' is globally visible and not in 'mydata'. [...] 2) There are 5 different values of status, but I only want to plot three of them. Can I do this without copying only the desired elements into a new data frame? Sure, just use the additional argument subset = (status %in% c(1, 3, 5)) or whatever the appropriate subset is. -Deepayan __ R-help@stat.math.ethz.ch 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.
Re: [R] Lines in dotchart dotplot ?
On 6/11/07, John Kane [EMAIL PROTECTED] wrote: --- [EMAIL PROTECTED] wrote: On 6/9/07, John Kane [EMAIL PROTECTED] wrote: Is it possible to use dotchart or dotplot and set the lines in such a way that they only extend from the left y-axis to the data point? Yes (sort of) in dotplot at least. E.g., dotplot(VADeaths, groups = FALSE, type = c(p, h)) dotplot(VADeaths, groups = FALSE, type = c(p, h), origin = 0) -Deepayan Ah, that is quite nice, not exactly what I remember from Cleveland but it should do quite nicely. You can do that too if you really want to: dotplot(VADeaths, groups = FALSE, type = c(p, h), origin = 0, panel = panel.xyplot, pch = 16, lty = 3, col = black) -Deepayan __ R-help@stat.math.ethz.ch 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.