Datetime in doubly y-axis plot

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

Datetime in doubly y-axis plot

Ogbos
Dear Members,
I have some hourly data. Usin:
dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
I converted the hour to time format and stored in Year.
The data consists of two different observations and I wish to plot
same on one graph (two y-axis and common x-axis).

Part of my script is of the form:
library(plotrix)
plot(Year,Li,pch=16,axes=FALSE,xlab="",ylab="",type="l",col="black",ylim=c(1,22909))
axis(2, ylim=c(1,22909),col="black",las=1)
mtext("Lightning count/day", side=2, line=4)
box()
par(new=TRUE)
plot(Year,CR, pch=15,
xlab="",ylab="",ylim=c(7302,9983),axes=FALSE,type="l",col="red")
mtext("GCR count/day",side=4,col="red",line=3)
axis(4, ylim=c(7302,9983), col="red",col.axis="red",las=1)
The two y-axis worked and looked fine.

But I have difficulties with x-axis.
I tried things like in an attempt to draw the x-axis.
## Draw the time axis
#axis(1,pretty(range(Year),10))
axis(side=1,at=c(as.datetime("2005-01-01 01:00:00
GMT"),as.datetime("2007-01-01 01:00:00 GMT"),as.datetime("2009-01-01
01:00:00 GMT"),labels=c("2005","2007","2009"))
#axis(side=1, at =Year)
#axis(1,Year)

But none worked.

However, when I plotted the two graphs separately, the x-axis was
correctly labelled from 2005 to 2010 (the time range of my data).

Small portion of the data is:
04 12 31 10  8637 4992 0.310676228913214 49.7113438132607
04 12 31 11  8634 4183 0.275834035016405 25.4492290005748
04 12 31 12  8652 4160 0.484887198397259 24.7594531777172
04 12 31 13  8626 3411 0.182921517958247 2.2967535550946
04 12 31 15  8618 3904 0.09000900090009 17.0819483667808
04 12 31 16  8620 3428 0.113237130164629 2.80658785894585
04 12 31 17  8603 3041 -0.0842019685839552 -8.79964011696198
04 12 31 18  8579 2340 -0.362939519758427 -29.8228075875341
04 12 31 19  8588 3006 -0.258412938068 -9.8492989778322
04 12 31 20  8604 3280 -0.0725879039516855 -1.63196960987679
04 12 31 21  8568 3696 -0.490694230713394 10.8439757078949
04 12 31 22  8567 2307 -0.502308295345664 -30.8124859420688
04 12 31 23  8570 2726 -0.467466101448855 -18.2465698647939
05 01 01 00  8579 2879 -0.568878758318289 8.46755203918244
05 01 01 01  8581 1586 -0.545698639133843 -40.2467740416313
05 01 01 02  8562 1183 -0.765909771386082 -55.4299708015447
05 01 01 03  8602 1491 -0.302307387697159 -43.8259395309409
05 01 01 04  8576 1742 -0.603648937094958 -34.369407553923
05 01 01 05  8622 2001 -0.0705061958526974 -24.6114721672789
05 01 01 06  8613 2675 -0.174816732182705 0.781765093717623
05 01 01 07  8622 3293 -0.0705061958526974 24.0651784873316
05 01 01 08  8628 4333 -0.00096583829935895 63.2476217387209
05 01 01 09  8669 16 0.474226604981787 -99.3971931807479
05 01 01 10  8698 3671 0.810338333156256 38.3064895921635
05 01 01 11  8671 3856 0.497406724166233 45.2764434397664
05 01 01 12  8679 3411 0.590127200904018 28.5108787793162
05 01 01 13  8647 3428 0.219245293952879 29.1513610247716
05 01 01 14  8651 4596 0.265605532321772 73.156258830178
05 01 01 15  8638 3154 0.114934757622872 18.8282942450786
05 01 01 16  8635 2365 0.0801645788462025 -10.8976170292926
05 01 01 17  8628 2697 -0.00096583829935895 1.61062447018932
05 01 01 18  8653 3231 0.288785651506218 21.7293020627296
05 01 01 19  8657 3548 0.33514588987511 33.6724121691627
05 01 01 20  8650 2645 0.254015472729549 -0.348497692380145
05 01 01 21  8605 2153 -0.26753720892049 -18.8848073843835
05 01 01 22  8600 1759 -0.325487506881605 -33.7289253084676
Where the first four columns are year, month, day and hour. The last
four columns are the parameters of interest which I am trying to plot,
taking two at a time.
I will remain indebted for your assistance.
Best Regards
Ogbos

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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: Datetime in doubly y-axis plot

PIKAL Petr
Hi

Why you do not use twoord.plot from plotrix which should be designed for
such issue.

Something like

twoord.plot(lx = Year, ly = Li, rx = Year, ry= CR, data=yourdata)

Cheers
Petr

> -----Original Message-----
> From: R-help <[hidden email]> On Behalf Of Ogbos Okike
> Sent: Thursday, November 21, 2019 9:49 AM
> To: r-help <[hidden email]>
> Subject: [R] Datetime in doubly y-axis plot
>
> Dear Members,
> I have some hourly data. Usin:
> dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year,
> month,day,hour,0,0)))
> I converted the hour to time format and stored in Year.
> The data consists of two different observations and I wish to plot
> same on one graph (two y-axis and common x-axis).
>
> Part of my script is of the form:
> library(plotrix)
>
plot(Year,Li,pch=16,axes=FALSE,xlab="",ylab="",type="l",col="black",ylim=c(

> 1,22909))
> axis(2, ylim=c(1,22909),col="black",las=1)
> mtext("Lightning count/day", side=2, line=4)
> box()
> par(new=TRUE)
> plot(Year,CR, pch=15,
> xlab="",ylab="",ylim=c(7302,9983),axes=FALSE,type="l",col="red")
> mtext("GCR count/day",side=4,col="red",line=3)
> axis(4, ylim=c(7302,9983), col="red",col.axis="red",las=1)
> The two y-axis worked and looked fine.
>
> But I have difficulties with x-axis.
> I tried things like in an attempt to draw the x-axis.
> ## Draw the time axis
> #axis(1,pretty(range(Year),10))
> axis(side=1,at=c(as.datetime("2005-01-01 01:00:00
> GMT"),as.datetime("2007-01-01 01:00:00 GMT"),as.datetime("2009-01-01
> 01:00:00 GMT"),labels=c("2005","2007","2009"))
> #axis(side=1, at =Year)
> #axis(1,Year)
>
> But none worked.
>
> However, when I plotted the two graphs separately, the x-axis was
> correctly labelled from 2005 to 2010 (the time range of my data).
>
> Small portion of the data is:
> 04 12 31 10  8637 4992 0.310676228913214 49.7113438132607
> 04 12 31 11  8634 4183 0.275834035016405 25.4492290005748
> 04 12 31 12  8652 4160 0.484887198397259 24.7594531777172
> 04 12 31 13  8626 3411 0.182921517958247 2.2967535550946
> 04 12 31 15  8618 3904 0.09000900090009 17.0819483667808
> 04 12 31 16  8620 3428 0.113237130164629 2.80658785894585
> 04 12 31 17  8603 3041 -0.0842019685839552 -8.79964011696198
> 04 12 31 18  8579 2340 -0.362939519758427 -29.8228075875341
> 04 12 31 19  8588 3006 -0.258412938068 -9.8492989778322
> 04 12 31 20  8604 3280 -0.0725879039516855 -1.63196960987679
> 04 12 31 21  8568 3696 -0.490694230713394 10.8439757078949
> 04 12 31 22  8567 2307 -0.502308295345664 -30.8124859420688
> 04 12 31 23  8570 2726 -0.467466101448855 -18.2465698647939
> 05 01 01 00  8579 2879 -0.568878758318289 8.46755203918244
> 05 01 01 01  8581 1586 -0.545698639133843 -40.2467740416313
> 05 01 01 02  8562 1183 -0.765909771386082 -55.4299708015447
> 05 01 01 03  8602 1491 -0.302307387697159 -43.8259395309409
> 05 01 01 04  8576 1742 -0.603648937094958 -34.369407553923
> 05 01 01 05  8622 2001 -0.0705061958526974 -24.6114721672789
> 05 01 01 06  8613 2675 -0.174816732182705 0.781765093717623
> 05 01 01 07  8622 3293 -0.0705061958526974 24.0651784873316
> 05 01 01 08  8628 4333 -0.00096583829935895 63.2476217387209
> 05 01 01 09  8669 16 0.474226604981787 -99.3971931807479
> 05 01 01 10  8698 3671 0.810338333156256 38.3064895921635
> 05 01 01 11  8671 3856 0.497406724166233 45.2764434397664
> 05 01 01 12  8679 3411 0.590127200904018 28.5108787793162
> 05 01 01 13  8647 3428 0.219245293952879 29.1513610247716
> 05 01 01 14  8651 4596 0.265605532321772 73.156258830178
> 05 01 01 15  8638 3154 0.114934757622872 18.8282942450786
> 05 01 01 16  8635 2365 0.0801645788462025 -10.8976170292926
> 05 01 01 17  8628 2697 -0.00096583829935895 1.61062447018932
> 05 01 01 18  8653 3231 0.288785651506218 21.7293020627296
> 05 01 01 19  8657 3548 0.33514588987511 33.6724121691627
> 05 01 01 20  8650 2645 0.254015472729549 -0.348497692380145
> 05 01 01 21  8605 2153 -0.26753720892049 -18.8848073843835
> 05 01 01 22  8600 1759 -0.325487506881605 -33.7289253084676
> Where the first four columns are year, month, day and hour. The last
> four columns are the parameters of interest which I am trying to plot,
> taking two at a time.
> I will remain indebted for your assistance.
> Best Regards
> Ogbos
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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 -- To UNSUBSCRIBE and more, see
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
|

Datetime in doubly y-axis plot: Solved

Ogbos
In reply to this post by Ogbos
Dear Jim and Petr,

Thank you.

The last code by Jim worked like magic. A great relief. Thank you very much.
Best wishes
Ogbos

On Thu, Nov 21, 2019 at 10:59 AM Jim Lemon <[hidden email]> wrote:

>
> Hi Ogbos,
> As Petr says, you can use twoord.plot for this:
>
> dta<-read.table(text="year month day hour Li CR x1 x2
> 04 12 31 10  8637 4992 0.310676228913214 49.7113438132607
> 04 12 31 11  8634 4183 0.275834035016405 25.4492290005748
> 04 12 31 12  8652 4160 0.484887198397259 24.7594531777172
> 04 12 31 13  8626 3411 0.182921517958247 2.2967535550946
> 04 12 31 15  8618 3904 0.09000900090009 17.0819483667808
> 04 12 31 16  8620 3428 0.113237130164629 2.80658785894585
> 04 12 31 17  8603 3041 -0.0842019685839552 -8.79964011696198
> 04 12 31 18  8579 2340 -0.362939519758427 -29.8228075875341
> 04 12 31 19  8588 3006 -0.258412938068 -9.8492989778322
> 04 12 31 20  8604 3280 -0.0725879039516855 -1.63196960987679
> 04 12 31 21  8568 3696 -0.490694230713394 10.8439757078949
> 04 12 31 22  8567 2307 -0.502308295345664 -30.8124859420688
> 04 12 31 23  8570 2726 -0.467466101448855 -18.2465698647939
> 05 01 01 00  8579 2879 -0.568878758318289 8.46755203918244
> 05 01 01 01  8581 1586 -0.545698639133843 -40.2467740416313
> 05 01 01 02  8562 1183 -0.765909771386082 -55.4299708015447
> 05 01 01 03  8602 1491 -0.302307387697159 -43.8259395309409
> 05 01 01 04  8576 1742 -0.603648937094958 -34.369407553923
> 05 01 01 05  8622 2001 -0.0705061958526974 -24.6114721672789
> 05 01 01 06  8613 2675 -0.174816732182705 0.781765093717623
> 05 01 01 07  8622 3293 -0.0705061958526974 24.0651784873316
> 05 01 01 08  8628 4333 -0.00096583829935895 63.2476217387209
> 05 01 01 09  8669 16 0.474226604981787 -99.3971931807479
> 05 01 01 10  8698 3671 0.810338333156256 38.3064895921635
> 05 01 01 11  8671 3856 0.497406724166233 45.2764434397664
> 05 01 01 12  8679 3411 0.590127200904018 28.5108787793162
> 05 01 01 13  8647 3428 0.219245293952879 29.1513610247716
> 05 01 01 14  8651 4596 0.265605532321772 73.156258830178
> 05 01 01 15  8638 3154 0.114934757622872 18.8282942450786
> 05 01 01 16  8635 2365 0.0801645788462025 -10.8976170292926
> 05 01 01 17  8628 2697 -0.00096583829935895 1.61062447018932
> 05 01 01 18  8653 3231 0.288785651506218 21.7293020627296
> 05 01 01 19  8657 3548 0.33514588987511 33.6724121691627
> 05 01 01 20  8650 2645 0.254015472729549 -0.348497692380145
> 05 01 01 21  8605 2153 -0.26753720892049 -18.8848073843835
> 05 01 01 22  8600 1759 -0.325487506881605 -33.7289253084676",
> header=TRUE,stringsAsFactors=FALSE)
> library(plotrix)
> dta$year<-ifelse(dta$year < 50,dta$year+2000,dta$year+1900)
> dta$date<-strptime(paste(paste(paste(dta$year,dta$month,dta$day,sep="-"),
>  paste(dta$hour,0,0,sep=":"),sep=" ")),"%Y-%m-%d %H:%M:%S")
> xlim<-range(as.numeric(dta$date))
> xorigin<-as.POSIXct("1970-01-01 0:0:0","%Y-%m-%d %H:%M:%S")
> xticks<-as.POSIXct(pretty(range(as.numeric(dta$date))),origin=xorigin)
> twoord.plot(dta$date,dta$Li,dta$date,dta$CR,xlim=xlim,xaxt="n",
>  main="Lightning and GCR frequency",
>  xlab="Date",ylab="Lightning count/day",rylab="GCR count/day",
>  xtickpos=xticks,xticklab=format(xticks,"%Y-%m-%d %H"))
>
> Jim
>
> On Thu, Nov 21, 2019 at 7:49 PM Ogbos Okike <[hidden email]> wrote:
> >
> > Dear Members,
> > I have some hourly data. Usin:
> > dta$year <- with( dta, ifelse(year < 50, year + 2000, year + 1900))
> > dta$datetime <- with( dta, as.POSIXct(ISOdatetime(year, month,day,hour,0,0)))
> > I converted the hour to time format and stored in Year.
> > The data consists of two different observations and I wish to plot
> > same on one graph (two y-axis and common x-axis).
> >

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.