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? Erich ________________________________________________ Erich Studerus Lic. Phil. Klinische Psychologie Psychiatric University Hospital Zurich Division of Clinical Research Lenggstr. 31 CH-8008 Zurich Switzerland Mail: erich.studerus@bli.uzh.ch Office: +41 44 384 26 66 Mobile: +41 76 563 31 54 ________________________________________________ [[alternative HTML version deleted]]
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
Dear Erich, Have a look at ggplot2 library(ggplot2) dataset <- expand.grid(x = 1:20, y = factor(LETTERS[1:4]), value = 1:10) dataset$value <- rnorm(nrow(dataset), sd = 0.5) + as.numeric(dataset$y) plotdata <- aggregate(dataset$value, list(x = dataset$x, y = dataset$y), mean) plotdata <- merge(plotdata, aggregate(dataset$value, list(x = dataset$x, y = dataset$y), sd)) plotdata$min <- plotdata$x.x - plotdata$x.y plotdata$max <- plotdata$x.x + plotdata$x.y ggplot(plotdata, aes(x = x, y = x.x, colour = y, min = min, max = max)) + geom_pointrange() + geom_line() + geom_point() HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Erich Studerus Verzonden: dinsdag 9 september 2008 12:49 Aan: r-help op r-project.org Onderwerp: [R] plotting group means 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? Erich ________________________________________________ Erich Studerus Lic. Phil. Klinische Psychologie Psychiatric University Hospital Zurich Division of Clinical Research Lenggstr. 31 CH-8008 Zurich Switzerland Mail: erich.studerus op bli.uzh.ch Office: +41 44 384 26 66 Mobile: +41 76 563 31 54 ________________________________________________ [[alternative HTML version deleted]] ______________________________________________ R-help op 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
Hi Erich, Have a look at brkdn.plot in the plotrix package. Jim
On Tue, Sep 9, 2008 at 6:56 AM, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> wrote:> Dear Erich, > > Have a look at ggplot2 > > library(ggplot2) > dataset <- expand.grid(x = 1:20, y = factor(LETTERS[1:4]), value = 1:10) > dataset$value <- rnorm(nrow(dataset), sd = 0.5) + as.numeric(dataset$y)Or with stat_summary: qplot(x, value, data=dataset, colour=y, group = y) + stat_summary(geom="line", fun="mean",size=2) -- http://had.co.nz/
Thanks for all the suggestions, but it seems, that all these functions need a rearrangement of my data, since in my case, the dependent variables are in different columns. The error.bars.by-function seems to be the only plotting function, that does not need a rearrangement. Are there other functions, which can do that or is there an easy way to rearrange the columns into one? Thanks Erich -----Urspr?ngliche Nachricht----- Von: hadley wickham [mailto:h.wickham at gmail.com] Gesendet: Dienstag, 9. September 2008 15:02 An: ONKELINX, Thierry Cc: Erich Studerus; r-help at r-project.org Betreff: Re: [R] plotting group means On Tue, Sep 9, 2008 at 6:56 AM, ONKELINX, Thierry <Thierry.ONKELINX at inbo.be> wrote:> Dear Erich, > > Have a look at ggplot2 > > library(ggplot2) > dataset <- expand.grid(x = 1:20, y = factor(LETTERS[1:4]), value = 1:10) > dataset$value <- rnorm(nrow(dataset), sd = 0.5) + as.numeric(dataset$y)Or with stat_summary: qplot(x, value, data=dataset, colour=y, group = y) + stat_summary(geom="line", fun="mean",size=2) -- http://had.co.nz/
On Tue, Sep 9, 2008 at 8:38 AM, Erich Studerus <erich.studerus at bli.uzh.ch> wrote:> Thanks for all the suggestions, but it seems, that all these functions need > a rearrangement of my data, since in my case, the dependent variables are in > different columns. The error.bars.by-function seems to be the only plotting > function, that does not need a rearrangement. Are there other functions, > which can do that or is there an easy way to rearrange the columns into one?Try: library(reshape) melt(x) Hadley -- http://had.co.nz/