Skip to content

efficient overlapping average curve on original curves

7 messages · Eliza Botto, Rui Barradas, David Winsemius +1 more

#
Hello,

Something like this?


# Make up some data
mat <- matrix(rnorm(100*37), ncol = 37)
mat <- apply(mat, 2, cumsum)
avg <- rowMeans(mat)

# matplot - matrix plot
matplot(mat, type = "l")
lines(avg, lwd = 2)


I've also seen some very nice graphics for ploting many lines in ggplot2 
using transparency in order to give a visual picture of where there are 
more lines.

Hope this helps,

Rui Barradas
Em 22-09-2012 00:11, eliza botto escreveu:
#
HI,

Similar graph in xyplot:
set.seed(1)

mat <- matrix(rnorm(100*37), ncol = 37)
mat <- apply(mat, 2, cumsum)
mat1<-melt(mat)


library(latticeExtra)
p0<-xyplot(value~X1,group=X2,data=mat1,type="l",ylab="mat1")
p1<-xyplot(aggregate(mat1,list(mat1$X1),function(x) mean(x))[,4]~X1,data=mat1,type="l",col="black",lwd=2)
p0+p1

A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: eliza botto <eliza_botto at hotmail.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Friday, September 21, 2012 7:33 PM
Subject: Re: [R] efficient overlapping average curve on original curves

Hello,

Something like this?


# Make up some data
mat <- matrix(rnorm(100*37), ncol = 37)
mat <- apply(mat, 2, cumsum)
avg <- rowMeans(mat)

# matplot - matrix plot
matplot(mat, type = "l")
lines(avg, lwd = 2)


I've also seen some very nice graphics for ploting many lines in ggplot2 
using transparency in order to give a visual picture of where there are 
more lines.

Hope this helps,

Rui Barradas
Em 22-09-2012 00:11, eliza botto escreveu:
______________________________________________
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.
#
On Sep 21, 2012, at 4:11 PM, eliza botto wrote:

            
If you are going to use par(new=TRUE) you must make sure your 'xlim' and 'ylim' are the same in the second plot as in the original plot. In your misadventure, the ylim for the means was probably much more narrow than in the original data. Or to do it easily, follow Rui Barradas' advice.

Or you could `rbind` the `rowMeans` to the original data before using `matplot`.
Perhaps:

matplot(x=mat1, y= rbind(mat2, rowMeans(mat2) ) )
#
And with ggplot2.


library(ggplot2)
library(scales)

dat <- data.frame(id = seq_len(nrow(mat)), mat)
dm <- reshape2::melt(dat, id = "id")
dm$variable <- as.ordered(dm$variable)
dm$avg <- rowMeans(mat)

p <- ggplot(dm, aes(x = id, y = value, group = variable))
p + geom_line(data = dm, colour = alpha("blue", 1/5)) +
     geom_line(data = dm, aes(y = avg), colour = "darkblue")

Rui Barradas
Em 22-09-2012 02:02, arun escreveu:
#
HI,

Modified version of ggplot()
library(ggplot2)
set.seed(1)
mat1<-melt(mat)
new1<-aggregate(mat1,list(mat1$X1),function(x) mean(x))[,4]

mat2<-within(mat1,{X2<-as.factor(X2)})

ggplot(data=mat2,aes(x=X1,y=value,group=X2))+geom_line(aes(colour=X2))+geom_line(data=mat2,aes(y=new1),colour="darkred")+opts(legend.position="none")


A.K.



----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: arun <smartpink111 at yahoo.com>
Cc: eliza botto <eliza_botto at hotmail.com>; R help <r-help at r-project.org>
Sent: Friday, September 21, 2012 9:18 PM
Subject: Re: [R] efficient overlapping average curve on original curves

And with ggplot2.


library(ggplot2)
library(scales)

dat <- data.frame(id = seq_len(nrow(mat)), mat)
dm <- reshape2::melt(dat, id = "id")
dm$variable <- as.ordered(dm$variable)
dm$avg <- rowMeans(mat)

p <- ggplot(dm, aes(x = id, y = value, group = variable))
p + geom_line(data = dm, colour = alpha("blue", 1/5)) +
? ?  geom_line(data = dm, aes(y = avg), colour = "darkblue")

Rui Barradas
Em 22-09-2012 02:02, arun escreveu: