[R] Lattice:can't subset in panel function using other variables
Folkes, Michael
FolkesM at pac.dfo-mpo.gc.ca
Sat Sep 1 01:51:07 CEST 2007
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:deepayan.sarkar at gmail.com]
Sent: August 31, 2007 2:04 PM
To: Folkes, Michael
Cc: r-help at stat.math.ethz.ch
Subject: Re: [R] Lattice:can't subset in panel function using other
variables
On 8/30/07, Folkes, Michael <FolkesM at pac.dfo-mpo.gc.ca> 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
More information about the R-help
mailing list