Hello I have a data set summarized like this: File name= Height Group Ind Age Trait 1 1 1 20 1 1 2 21 1 2 1 22 1 2 2 21 1 3 1 24 1 3 2 45 1 4 1 23 1 4 2 26 2 1 1 45 2 1 2 12 2 2 1 25 2 2 2 26 2 3 1 45 2 3 2 43 2 4 1 23 2 4 2 47 . . . I would like to plot Trait ~ Age but a different plot for each Group. I tried:> plot(Height$Trait ~ Height$Age | Group)But does not work. Any suggestion? Thanks Rosario
Hi, The best I've found (but definitely not the best!): x <- read.table(textConnection("Group Ind Age Trait 1 1 2 21 1 2 1 22 1 2 2 21 1 3 1 24 1 3 2 45 1 4 1 23 1 4 2 26 2 1 1 45 2 1 2 12 2 2 1 25 2 2 2 26 2 3 1 45 2 3 2 43 2 4 1 23 2 4 2 47 "), header=T) str(x) 'data.frame': 15 obs. of 4 variables: $ Group: int 1 1 1 1 1 1 1 2 2 2 ... $ Ind : int 1 2 2 3 3 4 4 1 1 2 ... $ Age : int 2 1 2 1 2 1 2 1 2 1 ... $ Trait: int 21 22 21 24 45 23 26 45 12 25 ... x_grp <- split(x, x$Group) for (i in 1:length(x_grp)){ plot(x_grp[[i]]$Trait~x_grp[[i]]$Age) } There are probably better approaches using lattice. HTH, Ivan Le 10/27/2010 11:21, Rosario Garcia Gil a ?crit :> Hello > > I have a data set summarized like this: > > File name= Height > > Group Ind Age Trait > 1 1 1 20 > 1 1 2 21 > 1 2 1 22 > 1 2 2 21 > 1 3 1 24 > 1 3 2 45 > 1 4 1 23 > 1 4 2 26 > 2 1 1 45 > 2 1 2 12 > 2 2 1 25 > 2 2 2 26 > 2 3 1 45 > 2 3 2 43 > 2 4 1 23 > 2 4 2 47 > . > . > . > > > I would like to plot Trait ~ Age but a different plot for each Group. > I tried: >> plot(Height$Trait ~ Height$Age | Group) > But does not work. Any suggestion? > > Thanks > Rosario > > ______________________________________________ > 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. >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Hi: Here's another take with ggplot2: library(ggplot2) # Define the basic plot elements. First argument is the data frame. # aes() refers to the plot's aesthetics, which refer to the variables # mapped to specific 'roles' in the plot 'geoms' g <- ggplot(x, aes(x = Age, y = Trait)) g + geom_point(size = 2.5) + facet_wrap(~ Group) + theme_bw() geom_point() produces a scatterplot; it picks up its x and y from the aesthetics in g. The default point size in geom_point() is 2, which I upped a bit. facet_wrap() produces individual plots by group; it serves the same purpose as the conditioning variable in lattice. The default theme in ggplot2 is to produce a grey background; theme_bw() changes the background to white and changes a few other defaults. HTH, Dennis On Wed, Oct 27, 2010 at 2:21 AM, Rosario Garcia Gil < M.Rosario.Garcia@genfys.slu.se> wrote:> Hello > > I have a data set summarized like this: > > File name= Height > > Group Ind Age Trait > 1 1 1 20 > 1 1 2 21 > 1 2 1 22 > 1 2 2 21 > 1 3 1 24 > 1 3 2 45 > 1 4 1 23 > 1 4 2 26 > 2 1 1 45 > 2 1 2 12 > 2 2 1 25 > 2 2 2 26 > 2 3 1 45 > 2 3 2 43 > 2 4 1 23 > 2 4 2 47 > . > . > . > > > I would like to plot Trait ~ Age but a different plot for each Group. > I tried: > > plot(Height$Trait ~ Height$Age | Group) > > But does not work. Any suggestion? > > Thanks > Rosario > > ______________________________________________ > 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]]
Reasonably Related Threads
- (no subject)
- ggpliot2: reordering of factors in facets facet.grid(). Reordering of factor on x-axis no problem.
- Ggplot2: Moving legend, change fill and removal of space between plots when using grid.arrange() possible use of facet_grid?
- point size
- color de fondo de un gráfico creado con ggplot