Hi R community, I'm just starting out in R and have a basic question about xyplot and tables. Suppose I had a table of data with the following names: Height, Age_group, City. I'd like to plot mean Height vs Age_group for each City. When I try to do the following:> library(lattice) > xyplot(mean(Height) ~ Age_group | City)of course I get just one data point, the mean Height for all individuals. I also tried constructing a new array with mean Height for each Age_group, factored by City, but couldn't find any way of doing this without using loops. I'm sure there must be an elegant way of plotting this in R without resorting to loops, but I can't find any hints in the online manuals. Thanks for any tips~ -- View this message in context: http://www.nabble.com/table%2C-xyplot%2C-names%2C---loops-tp25138552p25138552.html Sent from the R help mailing list archive at Nabble.com.
Just plotting the mean is not very informative (at least not more informative than providing the same information in a table). If you want to show some of the distributional properties of height for each Age_group x City pair, try box-and-whisker plots ?bwplot Imagine you have four age categories and five cities. Age=rep(0:3,each=25) City=rep(0:4,20) e=rnorm(100) #Create a dependent variable Height=(Age-1)+(City-1)+(Age-1)*(City-1)+e #Box whisker plot of Height by Age category for each City bwplot(Height~factor(Age)|factor(City)) Daniel ------------------------- cuncta stricte discussurus ------------------------- -----Urspr?ngliche Nachricht----- Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im Auftrag von w_poet Gesendet: Tuesday, August 25, 2009 1:17 PM An: r-help at r-project.org Betreff: [R] table, xyplot, names, & loops Hi R community, I'm just starting out in R and have a basic question about xyplot and tables. Suppose I had a table of data with the following names: Height, Age_group, City. I'd like to plot mean Height vs Age_group for each City. When I try to do the following:> library(lattice) > xyplot(mean(Height) ~ Age_group | City)of course I get just one data point, the mean Height for all individuals. I also tried constructing a new array with mean Height for each Age_group, factored by City, but couldn't find any way of doing this without using loops. I'm sure there must be an elegant way of plotting this in R without resorting to loops, but I can't find any hints in the online manuals. Thanks for any tips~ -- View this message in context: http://www.nabble.com/table%2C-xyplot%2C-names%2C---loops-tp25138552p2513855 2.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ 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.
Hello, "I'm just starting out in R and have a basic question about xyplot and tables. Suppose I had a table of data with the following names: Height, Age_group, City. I'd like to plot mean Height vs Age_group for each City" You did not provide a sample data.frame, so I generated one. This example is basically borrowed directly from Figure 4.3 in Sarkar's excellent book, "Lattice". Personally, I do feel that a plot such as the one below is a better display choice than a simple table of the means, but some may disagree. Please note that my random data do not contain effects for either age.group or city, so my guess is that your resulting plot will look cleaner (i.e., contain some visual signal.) ## BEGIN SAMPLE R CODE ## create sample data.frame df <- data.frame(height <- rnorm(1000, 10), age.group <- sample(gl(10,100, labels = paste("Age Group", 1:10))), city <- sample(gl(4, 250, labels = paste("City", 1:4)))) ## tabulate the data in matrix form h.tab <- with(df, tapply(height, list(age.group, city), mean)) ## use dotplot with the matrix object dotplot(h.tab, type = "o", auto.key = list(lines = TRUE, space = "right"), xlab = "height") ## END SAMPLE R CODE Best, Erik Iverson
And of course I did not test this :). Within the data.frame argument list, please change the <- operators to = signs. Then it should work. Erik -----Original Message----- From: Erik Iverson Sent: Tuesday, August 25, 2009 1:17 PM To: 'w_poet'; r-help at r-project.org Subject: RE: [R] table, xyplot, names, & loops Hello, "I'm just starting out in R and have a basic question about xyplot and tables. Suppose I had a table of data with the following names: Height, Age_group, City. I'd like to plot mean Height vs Age_group for each City" You did not provide a sample data.frame, so I generated one. This example is basically borrowed directly from Figure 4.3 in Sarkar's excellent book, "Lattice". Personally, I do feel that a plot such as the one below is a better display choice than a simple table of the means, but some may disagree. Please note that my random data do not contain effects for either age.group or city, so my guess is that your resulting plot will look cleaner (i.e., contain some visual signal.) ## BEGIN SAMPLE R CODE ## create sample data.frame df <- data.frame(height <- rnorm(1000, 10), age.group <- sample(gl(10,100, labels = paste("Age Group", 1:10))), city <- sample(gl(4, 250, labels = paste("City", 1:4)))) ## tabulate the data in matrix form h.tab <- with(df, tapply(height, list(age.group, city), mean)) ## use dotplot with the matrix object dotplot(h.tab, type = "o", auto.key = list(lines = TRUE, space = "right"), xlab = "height") ## END SAMPLE R CODE Best, Erik Iverson
On Tue, Aug 25, 2009 at 10:16 AM, w_poet<stephen.le at ucla.edu> wrote:> > Hi R community, > > I'm just starting out in R and have a basic question about xyplot and > tables. Suppose I had a table of data with the following names: Height, > Age_group, City. I'd like to plot mean Height vs Age_group for each City. > > When I try to do the following: > >> library(lattice) >> xyplot(mean(Height) ~ Age_group | City) > > of course I get just one data point, the mean Height for all individuals.One additional pointer: panel.average will do this for you: xyplot(Height ~ Age_group | City, panel = panel.average) or xyplot(Height ~ Age_group | City, type = "a") As others have pointed out, this is not usually a good idea, but with a 'groups' argument, this is a cheap way to get an interaction plot. -Deepayan