Re: [R] Lattice:can't subset in panel function using other variables
Thankyou very much Deepayan for pointing me in the correct direction. Your examples work perfectly for me. Much appreciated. Michael From: Deepayan Sarkar [mailto:[EMAIL PROTECTED] Sent: Fri 31/08/2007 5:18 PM To: Folkes, Michael Cc: r-help@stat.math.ethz.ch Subject: 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 [[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.
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] Lattice:can't subset in panel function using other variables
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? Thanks so much Michael ***Start of code # #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-df[df$temp==4 ,] df$year2-as.factor(df$year) df$week2-as.factor(df$week) #! #df-df[-1,] #-run this to see problem if panel data are of unequal length #! print(xyplot(yvar~xvar|week2,data=df,layout = c(4, 5), scales=list(cex=0.7,x=list(rot=45)), par.strip=list(cex=.7), as.table=T, strip = strip.custom(strip.names = F, strip.levels = TRUE), panel=function(x,y,subscripts){ panel.xyplot(x,y,type='p',col=1,cex=.5) panel.xyplot(df$xvar[df$year==2005][subscripts],df$yvar[df$year==2005][s ubscripts],type='p',pch=1,col=3,cex=1.5) }, )) ***End of code -Original Message- From: Deepayan Sarkar [mailto:[EMAIL PROTECTED] Sent: August 31, 2007 2:04 PM To: Folkes, Michael Cc: r-help@stat.math.ethz.ch Subject: 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] 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.
[R] Lattice:can't subset in panel function using other variables
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 })) ___ Thanks very much! Michael Folkes ___ 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.