[R] efficient overlapping average curve on original curves

Rui Barradas ruipbarradas at sapo.pt
Sat Sep 22 03:18:05 CEST 2012


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,
>
> 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:
>> Dear useRs,
>>
>> my question could be very basic for which i apologize in advance.
>> Each column of a matrix with dimensions 365 rows and 37 columns was drawn against another matrix of dimensions 365 rows and 1 column. with that i was able to draw 37 curves on the same axis.
>> now i want to draw an average curve of these 37 curves on the same axis in such a way that all the curves (average and 37 curves) should appear on the same axis at the same time.
>> i used
>>
>>> par(new=TRUE)
>> But it has 2 limitations
>>
>> 1- it completly distorted y-axis values
>> 2- it was not very efficient as average curve in no overlapped the original curves.
>>
>> can any1 advise me what to do?
>> thanks in advance for you time.
>>
>> eliza botto
>>
>>                             
>>      [[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.
> ______________________________________________
> 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.
>




More information about the R-help mailing list