df=data.frame(country=c("A","A","A","B","B","B"), food=rep(c("Apples","Pears","Bananas"),2), X2000=c(4,5,6,7,6,8), X2001=c(4,5,6,7,6,8), X2002=c(4,5,6,7,6,8), X2003=c(4,5,6,7,6,8)); I have data in the above form trying to get a plot of each fruit over time year conditioned on country and food. I tried, xyplot(X2000+X2001+X2002+X2003~2000:2003|country*food,df) But think I need to transform the data, just not sure how. Any help gratefully received. Cheers, Geoff. [[alternative HTML version deleted]]
Geoff - I think this will get you closer to a solution: newdf = reshape(df,varying=names(df)[-c(1,2)],direction='long', times=2000:2003,idvar=c('country','food'),v.names='X', timevar='year') xyplot(X~year|country*food,data=newdf) - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Fri, 18 Feb 2011, Geoff Russell wrote:> df=data.frame(country=c("A","A","A","B","B","B"), > food=rep(c("Apples","Pears","Bananas"),2), > X2000=c(4,5,6,7,6,8), > X2001=c(4,5,6,7,6,8), > X2002=c(4,5,6,7,6,8), > X2003=c(4,5,6,7,6,8)); > > I have data in the above form trying to get a plot of each fruit over time > year conditioned on country and food. > > I tried, > > xyplot(X2000+X2001+X2002+X2003~2000:2003|country*food,df) > > But think I need to transform the data, just not sure > how. Any help gratefully received. > > Cheers, > Geoff. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Hi: Here's another approach using the reshape package: library(reshape) # id defines the grouping variables; the others are stacked, creating # new variables 'variable' (containing the variable names as factor levels) and # 'value' (which holds the corresponding values) dfm <- melt(df, id = c('country', 'food')) # Strip off the leading 'X' and redefine as a factor with new levels dfm$year <- factor(gsub('X', '', dfm$variable), levels = 2000:2003) # Using foods as a groups = argument in xyplot(): xyplot(value ~ year | country, groups = food, data = dfm, type = c('p', 'l')) # Using country and food as conditioning variables xyplot(value ~ variable | country * food, data = dfm) HTH, Dennis On Thu, Feb 17, 2011 at 10:41 PM, Geoff Russell <geoffrey.russell@gmail.com>wrote:> df=data.frame(country=c("A","A","A","B","B","B"), > food=rep(c("Apples","Pears","Bananas"),2), > X2000=c(4,5,6,7,6,8), > X2001=c(4,5,6,7,6,8), > X2002=c(4,5,6,7,6,8), > X2003=c(4,5,6,7,6,8)); > > I have data in the above form trying to get a plot of each fruit over time > year conditioned on country and food. > > I tried, > > xyplot(X2000+X2001+X2002+X2003~2000:2003|country*food,df) > > But think I need to transform the data, just not sure > how. Any help gratefully received. > > Cheers, > Geoff. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]