[R] Time series plot

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

[R] Time series plot

Arun.stat
Dear all R users,

Suppose I have a data set like this:

date              price

1-Jan-02     4.8803747
2-Jan-02     4.8798430
3-Jan-02     4.8840133
4-Jan-02     4.8803747
5-Jan-02     4.8749683
6-Jan-02     4.8754263
7-Jan-02     4.8746628
8-Jan-02     4.8753500
9-Jan-02     4.8882416
10-Jan-02     4.8895217
11-Jan-02     4.8871108

I want to get a time series plot of that dataset. But in x-axis I want to
see the first day, and last day, and other day in between them  i.e.
1-Jan-02,  6-Jan-02, and  11-Jan-02 only. Can anyone tell me how to do that?

My second question is that is there any way to define a secondary axis like
Microsoft Excel in the same plot window?

Thanks and regards,
Arun

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R] Time series plot

Gabor Grothendieck
You can use read.zoo in the zoo package to read in the data
and then see:

https://www.stat.math.ethz.ch/pipermail/r-help/2006-December/122742.html

See ?axis for creating additional axes with classic graphics and

library(lattice)
?panel.axis

in lattice graphics.  Search the archives for examples.

On 1/4/07, Arun Kumar Saha <[hidden email]> wrote:

> Dear all R users,
>
> Suppose I have a data set like this:
>
> date              price
>
> 1-Jan-02     4.8803747
> 2-Jan-02     4.8798430
> 3-Jan-02     4.8840133
> 4-Jan-02     4.8803747
> 5-Jan-02     4.8749683
> 6-Jan-02     4.8754263
> 7-Jan-02     4.8746628
> 8-Jan-02     4.8753500
> 9-Jan-02     4.8882416
> 10-Jan-02     4.8895217
> 11-Jan-02     4.8871108
>
> I want to get a time series plot of that dataset. But in x-axis I want to
> see the first day, and last day, and other day in between them  i.e.
> 1-Jan-02,  6-Jan-02, and  11-Jan-02 only. Can anyone tell me how to do that?
>
> My second question is that is there any way to define a secondary axis like
> Microsoft Excel in the same plot window?
>
> Thanks and regards,
> Arun
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R] Time series plot

Arun.stat
Dear Gabor,

Thank you very much for your letter. Actually I got partial solution from
your suggestion. Still I am fighting with defining a secondary axis. More
pecisely, suppose I have following two dataset:

x = c(1:10)
y = x*10

To plot x I can simply write plot(x, type='l'), here the"y-axis" takes value
from 1:10. Now I want to plot y on a Secondary "Y-axis" on same graphics
window. Secondary y-axis will take value from 1:100 and plot y accordingly,
just like Microsoft Excel. Is there any solution?

Thanks and regards,





On 1/4/07, Gabor Grothendieck <[hidden email]> wrote:

>
> You can use read.zoo in the zoo package to read in the data
> and then see:
>
> https://www.stat.math.ethz.ch/pipermail/r-help/2006-December/122742.html
>
> See ?axis for creating additional axes with classic graphics and
>
> library(lattice)
> ?panel.axis
>
> in lattice graphics.  Search the archives for examples.
>
> On 1/4/07, Arun Kumar Saha <[hidden email]> wrote:
> > Dear all R users,
> >
> > Suppose I have a data set like this:
> >
> > date              price
> >
> > 1-Jan-02     4.8803747
> > 2-Jan-02     4.8798430
> > 3-Jan-02     4.8840133
> > 4-Jan-02     4.8803747
> > 5-Jan-02     4.8749683
> > 6-Jan-02     4.8754263
> > 7-Jan-02     4.8746628
> > 8-Jan-02     4.8753500
> > 9-Jan-02     4.8882416
> > 10-Jan-02     4.8895217
> > 11-Jan-02     4.8871108
> >
> > I want to get a time series plot of that dataset. But in x-axis I want
> to
> > see the first day, and last day, and other day in between them  i.e.
> > 1-Jan-02,  6-Jan-02, and  11-Jan-02 only. Can anyone tell me how to do
> that?
> >
> > My second question is that is there any way to define a secondary axis
> like
> > Microsoft Excel in the same plot window?
> >
> > Thanks and regards,
> > Arun
> >
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > [hidden email] 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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R] Time series plot

Gabor Grothendieck
Check out #2 in:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/85801.html

and RSiteSearch("axis(4") to find additional examples.

On 1/4/07, Arun Kumar Saha <[hidden email]> wrote:

> Dear Gabor,
>
> Thank you very much for your letter. Actually I got partial solution from
> your suggestion. Still I am fighting with defining a secondary axis. More
> pecisely, suppose I have following two dataset:
>
> x = c(1:10)
> y = x*10
>
> To plot x I can simply write plot(x, type='l'), here the"y-axis" takes value
> from 1:10. Now I want to plot y on a Secondary "Y-axis" on same graphics
> window. Secondary y-axis will take value from 1:100 and plot y accordingly,
> just like Microsoft Excel. Is there any solution?
>
> Thanks and regards,
>
>
>
>
>
> On 1/4/07, Gabor Grothendieck <[hidden email]> wrote:
> > You can use read.zoo in the zoo package to read in the data
> > and then see:
> >
> >
> https://www.stat.math.ethz.ch/pipermail/r-help/2006-December/122742.html
> >
> > See ?axis for creating additional axes with classic graphics and
> >
> > library(lattice)
> > ?panel.axis
> >
> > in lattice graphics.  Search the archives for examples.
> >
> > On 1/4/07, Arun Kumar Saha <[hidden email]> wrote:
> > > Dear all R users,
> > >
> > > Suppose I have a data set like this:
> > >
> > > date              price
> > >
> > > 1-Jan-02     4.8803747
> > > 2-Jan-02     4.8798430
> > > 3-Jan-02     4.8840133
> > > 4-Jan-02     4.8803747
> > > 5-Jan-02     4.8749683
> > > 6-Jan-02     4.8754263
> > > 7-Jan-02     4.8746628
> > > 8-Jan-02     4.8753500
> > > 9-Jan-02     4.8882416
> > > 10-Jan-02     4.8895217
> > > 11-Jan-02     4.8871108
> > >
> > > I want to get a time series plot of that dataset. But in x-axis I want
> to
> > > see the first day, and last day, and other day in between them   i.e.
> > > 1-Jan-02,  6-Jan-02, and  11-Jan-02 only. Can anyone tell me how to do
> that?
> > >
> > > My second question is that is there any way to define a secondary axis
> like
> > > Microsoft Excel in the same plot window?
> > >
> > > Thanks and regards,
> > > Arun
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] 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.
> > >
> >
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R] Time series plot

PIKAL Petr
In reply to this post by Arun.stat
Hi

On 4 Jan 2007 at 14:18, Arun Kumar Saha wrote:

Date sent:       Thu, 4 Jan 2007 14:18:11 +0530
From:           "Arun Kumar Saha" <[hidden email]>
To:             "Gabor Grothendieck" <[hidden email]>
Copies to:       "[hidden email]" <[hidden email]>
Subject:         Re: [R] Time series plot

> Dear Gabor,
>
> Thank you very much for your letter. Actually I got partial solution
> from your suggestion. Still I am fighting with defining a secondary
> axis. More pecisely, suppose I have following two dataset:
>
> x = c(1:10)
> y = x*10
>
> To plot x I can simply write plot(x, type='l'), here the"y-axis" takes
> value from 1:10. Now I want to plot y on a Secondary "Y-axis" on same

I think you are lost a bit in plotting
let's
x be
x<-rnorm(10)
and
y<-x*10

then
gives you x values on y axis and x axis is 1:10. You can put another
values y on the same plot but only if you give them space before

plot(x, type="l", ylim=range(y))
points(y)
but it is probably not what you want.

If you want to plot time series to get time labels on x axis then
Gabor's solution would be perfectly valid.

Or you can convert your date (which is probably factor) to real time.

> tab=read.table("clipboard", header=T)
> tab
        date    price
1   1-Jan-02 4.880375
2   2-Jan-02 4.879843
3   3-Jan-02 4.884013
4   4-Jan-02 4.880375
5   5-Jan-02 4.874968
6   6-Jan-02 4.875426
7   7-Jan-02 4.874663
8   8-Jan-02 4.875350
9   9-Jan-02 4.888242
10 10-Jan-02 4.889522
11 11-Jan-02 4.887111

tab$newdate <- as.POSIXct(strptime(tab$date, format="%d-%b-%y"))

> str(tab)
'data.frame':   11 obs. of  3 variables:
 $ date   : Factor w/ 11 levels "1-Jan-02","10-Jan-02",..: 1 4 5 6 7
8 9 10 11 2 ...
 $ price  : num  4.88 4.88 4.88 4.88 4.87 ...
 $ newdate:'POSIXct', format: chr  "2002-01-01" "2002-01-02" "2002-01-
03" "2002-01-04" .

plot(tab$newdate, tab$price)

what is what you may want without explicit need for secondary y axis.

HTH
Petr

> graphics window. Secondary y-axis will take value from 1:100 and plot
> y accordingly, just like Microsoft Excel. Is there any solution?
>
> Thanks and regards,
>
>
>
>
>
> On 1/4/07, Gabor Grothendieck <[hidden email]> wrote:
> >
> > You can use read.zoo in the zoo package to read in the data
> > and then see:
> >
> > https://www.stat.math.ethz.ch/pipermail/r-help/2006-December/122742.
> > html
> >
> > See ?axis for creating additional axes with classic graphics and
> >
> > library(lattice)
> > ?panel.axis
> >
> > in lattice graphics.  Search the archives for examples.
> >
> > On 1/4/07, Arun Kumar Saha <[hidden email]> wrote:
> > > Dear all R users,
> > >
> > > Suppose I have a data set like this:
> > >
> > > date              price
> > >
> > > 1-Jan-02     4.8803747
> > > 2-Jan-02     4.8798430
> > > 3-Jan-02     4.8840133
> > > 4-Jan-02     4.8803747
> > > 5-Jan-02     4.8749683
> > > 6-Jan-02     4.8754263
> > > 7-Jan-02     4.8746628
> > > 8-Jan-02     4.8753500
> > > 9-Jan-02     4.8882416
> > > 10-Jan-02     4.8895217
> > > 11-Jan-02     4.8871108
> > >
> > > I want to get a time series plot of that dataset. But in x-axis I
> > > want
> > to
> > > see the first day, and last day, and other day in between them
> > > i.e. 1-Jan-02,  6-Jan-02, and  11-Jan-02 only. Can anyone tell me
> > > how to do
> > that?
> > >
> > > My second question is that is there any way to define a secondary
> > > axis
> > like
> > > Microsoft Excel in the same plot window?
> > >
> > > Thanks and regards,
> > > Arun
> > >
> > >        [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > [hidden email] 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]]
>
> ______________________________________________
> [hidden email] 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.

Petr Pikal
[hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: [R] Time series plot

Don MacQueen
In reply to this post by Arun.stat
Here's an example illustrating a way to get a second y axis that has
a different range:

x <- 1:10
y1 <- 2*x
y2 <- 100-3*x+rnorm(10)

par(mar=c(5.1,4.1,4.1,4.1))

plot(x,y1)
par(new=TRUE)
plot(x,y2,xaxt='n',yaxt='n',xlab='',ylab='',pch=3)
axis(4)
mtext('y2',side=4,line=2.5)

-Don

At 2:18 PM +0530 1/4/07, Arun Kumar Saha wrote:

>Dear Gabor,
>
>Thank you very much for your letter. Actually I got partial solution from
>your suggestion. Still I am fighting with defining a secondary axis. More
>pecisely, suppose I have following two dataset:
>
>x = c(1:10)
>y = x*10
>
>To plot x I can simply write plot(x, type='l'), here the"y-axis" takes value
>from 1:10. Now I want to plot y on a Secondary "Y-axis" on same graphics
>window. Secondary y-axis will take value from 1:100 and plot y accordingly,
>just like Microsoft Excel. Is there any solution?
>
>Thanks and regards,
>
>
>
>
>
>On 1/4/07, Gabor Grothendieck <[hidden email]> wrote:
>>
>>  You can use read.zoo in the zoo package to read in the data
>>  and then see:
>>
>>  https://www.stat.math.ethz.ch/pipermail/r-help/2006-December/122742.html
>>
>>  See ?axis for creating additional axes with classic graphics and
>>
>>  library(lattice)
>>  ?panel.axis
>>
>>  in lattice graphics.  Search the archives for examples.
>>
>>  On 1/4/07, Arun Kumar Saha <[hidden email]> wrote:
>>  > Dear all R users,
>>  >
>>  > Suppose I have a data set like this:
>>  >
>>  > date              price
>>  >
>>  > 1-Jan-02     4.8803747
>>  > 2-Jan-02     4.8798430
>>  > 3-Jan-02     4.8840133
>>  > 4-Jan-02     4.8803747
>>  > 5-Jan-02     4.8749683
>>  > 6-Jan-02     4.8754263
>>  > 7-Jan-02     4.8746628
>>  > 8-Jan-02     4.8753500
>>  > 9-Jan-02     4.8882416
>>  > 10-Jan-02     4.8895217
>>  > 11-Jan-02     4.8871108
>>  >
>>  > I want to get a time series plot of that dataset. But in x-axis I want
>>  to
>>  > see the first day, and last day, and other day in between them  i.e.
>>  > 1-Jan-02,  6-Jan-02, and  11-Jan-02 only. Can anyone tell me how to do
>>  that?
>>  >
>>  > My second question is that is there any way to define a secondary axis
>>  like
>>  > Microsoft Excel in the same plot window?
>>  >
>>  > Thanks and regards,
>>  > Arun
>>  >
>>  >        [[alternative HTML version deleted]]
>>  >
>>  > ______________________________________________
>>  > [hidden email] 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]]
>
>______________________________________________
>[hidden email] 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.


--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

______________________________________________
[hidden email] 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.