paladini at rz.uni-potsdam.de
2006-Mar-21 11:05 UTC
[R] How to use: library lattice: barchart
Dear ladies and gentlemen! In the help text for the xyplot (library(lattice), help(xyplot)) is an example given how one can use barchart: barchart(yield ~ variety | site, data = barley, groups = year, layout = c(1,6), ylab = "Barley Yield (bushels/acre)", scales = list(x = list(abbreviate = TRUE, minlength = 5))) I want my data to be represented just in the same way. But when I try it like this: ayield = c(2,3,5,6,3,4,7,8,9,2,3,5,6,1,2,3,4,2,6,8) avariety = c(rep("A",5),rep("B",5),rep("C",5),rep("D",5)) ayear = (c(rep(1931,10),rep(1932,10))) asite = c(rep(c("iu","gt","jt","jhzt","tr"),4)) abarley = data.frame(cbind(ayield,avariety,ayear,asite)) barchart(ayield ~ avariety | asite, data = abarley,groups = ayear, layout c(1,5) ) it looks totaly different and I get the error message: "x should be numeric in: bwplot.formula(x = ayield ~ avariety | asite, data list(ayield = c(2," What did I do wrong? Can anybody help me? Best regards, thank you very much Claudia Paladini
Hello, You could get something similar to the example by adding "horizontal=FALSE". ############## barchart(ayield ~ avariety | asite, data = abarley, groups = ayear, horizontal=FALSE, layout = c(1,5) ) ############### With respect to the error message you got. Check carefully "barley" data and compare it with yours. In your case, "avariety" has four levels A,B,C and D but only A and B appears in 1931 and (C,D) in 1932 while in barley data, the all the varieties appear in all the years. That lack of data in some years is the cause of the error. Regards, Carlos Ortega. On 3/21/06, paladini@rz.uni-potsdam.de <paladini@rz.uni-potsdam.de> wrote:> > Dear ladies and gentlemen! > > In the help text for the xyplot (library(lattice), help(xyplot)) is an > example > given how one can use barchart: > > barchart(yield ~ variety | site, data = barley, > groups = year, layout = c(1,6), > ylab = "Barley Yield (bushels/acre)", > scales = list(x = list(abbreviate = TRUE, > minlength = 5))) > > I want my data to be represented just in the same way. But when I try it > like > this: > > > ayield = c(2,3,5,6,3,4,7,8,9,2,3,5,6,1,2,3,4,2,6,8) > avariety = c(rep("A",5),rep("B",5),rep("C",5),rep("D",5)) > ayear = (c(rep(1931,10),rep(1932,10))) > asite = c(rep(c("iu","gt","jt","jhzt","tr"),4)) > abarley = data.frame(cbind(ayield,avariety,ayear,asite)) > > barchart(ayield ~ avariety | asite, data = abarley,groups = ayear, layout > > c(1,5) ) > > it looks totaly different and I get the error message: > "x should be numeric in: bwplot.formula(x = ayield ~ avariety | asite, > data > list(ayield = c(2," > > What did I do wrong? > Can anybody help me? > > Best regards, thank you very much > > > Claudia Paladini > > ______________________________________________ > 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 >[[alternative HTML version deleted]]
I think your problem is the definition of abarley. You're making ayield into a factor. Have a look at str(abarley). Peter Ehlers paladini at rz.uni-potsdam.de wrote:> Dear ladies and gentlemen! > > In the help text for the xyplot (library(lattice), help(xyplot)) is an example > given how one can use barchart: > > barchart(yield ~ variety | site, data = barley, > groups = year, layout = c(1,6), > ylab = "Barley Yield (bushels/acre)", > scales = list(x = list(abbreviate = TRUE, > minlength = 5))) > > I want my data to be represented just in the same way. But when I try it like > this: > > > ayield = c(2,3,5,6,3,4,7,8,9,2,3,5,6,1,2,3,4,2,6,8) > avariety = c(rep("A",5),rep("B",5),rep("C",5),rep("D",5)) > ayear = (c(rep(1931,10),rep(1932,10))) > asite = c(rep(c("iu","gt","jt","jhzt","tr"),4)) > abarley = data.frame(cbind(ayield,avariety,ayear,asite)) > > barchart(ayield ~ avariety | asite, data = abarley,groups = ayear, layout > c(1,5) ) > > it looks totaly different and I get the error message: > "x should be numeric in: bwplot.formula(x = ayield ~ avariety | asite, data > list(ayield = c(2," > > What did I do wrong? > Can anybody help me? > > Best regards, thank you very much > > > Claudia Paladini > > ______________________________________________ > R-help at 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
Hi cbind(some numeric and not numeric columns) gives you all columns to be character and when you make data.frame from it it is converted to factors. so> abarley = data.frame(ayield,avariety,ayear,asite)brings you close but than you need ayear to be factor. Either convert it in data frame or on fly barchart(ayield ~ avariety | asite, data = abarley, groups = factor(ayear), layout = c(1,5) ) HTH Petr BTW. If you encounter error other than "Error: syntax error in: .... it's time to look at your data by ?str, class, typeoff, ... and any other structure and type revealing tools. Cheers. On 21 Mar 2006 at 12:05, paladini at rz.uni-potsdam.de wrote: Date sent: Tue, 21 Mar 2006 12:05:27 +0100 From: paladini at rz.uni-potsdam.de To: r-help at stat.math.ethz.ch Subject: [R] How to use: library lattice: barchart> Dear ladies and gentlemen! > > In the help text for the xyplot (library(lattice), help(xyplot)) is an > example given how one can use barchart: > > barchart(yield ~ variety | site, data = barley, > groups = year, layout = c(1,6), > ylab = "Barley Yield (bushels/acre)", > scales = list(x = list(abbreviate = TRUE, > minlength = 5))) > > I want my data to be represented just in the same way. But when I try > it like this: > > > ayield = c(2,3,5,6,3,4,7,8,9,2,3,5,6,1,2,3,4,2,6,8) > avariety = c(rep("A",5),rep("B",5),rep("C",5),rep("D",5)) > ayear = (c(rep(1931,10),rep(1932,10))) > asite = c(rep(c("iu","gt","jt","jhzt","tr"),4)) > abarley = data.frame(cbind(ayield,avariety,ayear,asite)) > > barchart(ayield ~ avariety | asite, data = abarley,groups = ayear, > layout = c(1,5) ) > > it looks totaly different and I get the error message: > "x should be numeric in: bwplot.formula(x = ayield ~ avariety | asite, > data = list(ayield = c(2," > > What did I do wrong? > Can anybody help me? > > Best regards, thank you very much > > > Claudia Paladini > > ______________________________________________ > R-help at 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.htmlPetr Pikal petr.pikal at precheza.cz
> it looks totaly different and I get the error message: > "x should be numeric in: bwplot.formula(x = ayield ~ avariety | asite, > data = list(ayield = c(2,"-------------------> cbind(some numeric and not numeric columns) > > gives you all columns to be character and when you make data.frame > from it it is converted to factors. > > so > > > abarley = data.frame(ayield,avariety,ayear,asite) > > brings you close but than you need ayear to be factor. Either convert > it in data frame or on flyActually, as the warning suggests, you have all factors in the dataframe but you need to convert ayield to a numeric. Something like, abarley = data.frame(cbind(as.numeric(ayield),avariety,ayear,asite))> > barchart(ayield ~ avariety | asite, data = abarley, groups > factor(ayear), layout = c(1,5) ) > > HTH > Petr____________________________ Robert W. Baer, Ph.D. Associate Professor Department of Physiology A. T. Still University of Health Science 800 W. Jefferson St. Kirksville, MO 63501-1497 USA
paladini at rz.uni-potsdam.de
2006-Mar-21 16:36 UTC
[R] How to use: library lattice: barchart
Thank you both very much. It has to be "abarley data.frame(cbind(as.numeric(ayield),avariety,ayear,asite))". Than the plot looks really fine! Best regards clauida Zitat von Robert Baer <rbaer at atsu.edu>:> > it looks totaly different and I get the error message: > > "x should be numeric in: bwplot.formula(x = ayield ~ avariety | asite, > > data = list(ayield = c(2," > > ------------------- > > > cbind(some numeric and not numeric columns) > > > > gives you all columns to be character and when you make data.frame > > from it it is converted to factors. > > > > so > > > > > abarley = data.frame(ayield,avariety,ayear,asite) > > > > brings you close but than you need ayear to be factor. Either convert > > it in data frame or on fly > Actually, as the warning suggests, you have all factors in the dataframe but > you need to convert ayield to a numeric. Something like, > > abarley = data.frame(cbind(as.numeric(ayield),avariety,ayear,asite)) > > > > > barchart(ayield ~ avariety | asite, data = abarley, groups > > factor(ayear), layout = c(1,5) ) > > > > HTH > > Petr > > > ____________________________ > Robert W. Baer, Ph.D. > Associate Professor > Department of Physiology > A. T. Still University of Health Science > 800 W. Jefferson St. > Kirksville, MO 63501-1497 USA > > >