[R] plotting group means

Chuck Cleland ccleland at optonline.net
Tue Sep 9 13:54:02 CEST 2008


On 9/9/2008 6:49 AM, Erich Studerus wrote:
> Hi all,
> 
>  
> 
> I want to plot the grouped means of some variables. The dependent variables
> and the grouping factor are stored in different columns. I want to draw a
> simple line-plot of means, in which the x-axis represents the variables and
> y-axis represents the means. The means of the groups should be connected by
> lines. So far, the only function that I could find comes closest to what I'm
> looking for, is the error.bars.by-function in the psych-package. To know,
> what I'm looking for, just type:
> 
>  
> 
> library(psych)
> x <- matrix(rnorm(500),ncol=20)
> y <- sample(4,25 ,replace=TRUE)
> x <- x+y
> error.bars.by(x,y,ci=0)
> 
>  
> 
> Now, I want to put a legend for the grouping factor of this graph. I also
> would like to manipulate the linetypes and colors of the lines. I've read
> the documentation, but it was not clear to me, how to do this. Are there
> other plotting functions in R, which can do the same?

  Here is an approach which uses xyplot() in the lattice package and
shows how to control line types and colors:

mydf <- data.frame(x=rep(paste("Group", 1:4, sep=""), 6),
                   v=rep(paste("Variable", 1:6, sep=""), each=4),
                   y=runif(24))

library(lattice)

xyplot(y ~ v, groups = x, data = mydf, type="b",
              xlab="Dependent Variables", ylab="Mean",
              auto.key=list(lines=TRUE, points=TRUE, space="right"),
              par.settings = list(superpose.symbol =
                                   list(pch=c(16,8,1,5),
                                        col=c("black","red","green","blue"),
                                        lty=c(1,2,3,4)),
                                  superpose.line =
                                   list(col=c("black","red","green","blue"),
                                        lty=c(1,2,3,4))))

> Erich
> 
>  
> 
> ________________________________________________
> 
> Erich Studerus
> Lic. Phil. Klinische Psychologie
> Psychiatric University Hospital Zurich
> Division of Clinical Research
> Lenggstr. 31
> CH-8008 Zurich
> Switzerland
> Mail: erich.studerus at bli.uzh.ch
> Office: +41 44 384 26 66
> Mobile: +41 76 563 31 54
> ________________________________________________
> 
>  
> 
> 
> 	[[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. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list