[R] Correct x-axis of two in one graph

Ogbos Okike giftedlife2014 @ending from gm@il@com
Thu Nov 29 17:17:18 CET 2018


Dear Petr,

Thank you so much for your contribution.

Let me show you what I have please.

I am attaching two plots. The first (twoineone) is my own while the
second (testrun) is plotted with the code you send.

The first is closer to my interest. The major problem I have is the
date. Those were set arbitrary with:
axis(side=1,at=c(1,400,800,1200,1500),labels=c("2004","2005","2006","2007","2008"))
and they seem not to be correct.

The second one does not show date and date is very important to me here.

Is there a way your code can implement the correct date on x-axis? I
would be very glad for further assistance.

Thank you again.
Best wishes
Ogbos
On Thu, Nov 29, 2018 at 3:03 PM PIKAL Petr <petr.pikal using precheza.cz> wrote:
>
> Hi
>
> If I understand correctly you want Li and CR appear in one plot with the same x axis. Although it is not usually recommended you could use twoord.plot from plotrix or undocumented code below.
>
> plot.yy <- function (x, yright, yleft, yleftlim = NULL, yrightlim = NULL,   xlab = NULL, yylab = list(NA, NA), pch = c(1, 2), col = c(1,    2), linky = F, smooth = 0, lwds = 1, length = 10, format = "%d/%m",    rect = NULL, type = "p", ...)
> {
>     par(mar = c(5, 4, 4, 2), oma = c(0, 0, 0, 3))
>     plot(x, yright, ylim = yrightlim, axes = F, ylab = "", xlab = xlab,  pch = pch[1], col = col[1], type = type, ...)
>     if (!is.null(rect))
>         rect(x[rect[1]], rect[2], x[rect[3]], rect[4], col = "grey")
>     points(x, yright, ylim = yrightlim, ylab = "", xlab = xlab, pch = pch[1], col = col[1], ...)
>     axis(4, pretty(range(yright, na.rm = T), 10), col = col[1])
>     if (linky)
>         lines(x, yright, col = col[1], ...)
>     if (smooth != 0)
>         lines(supsmu(x, yright, span = smooth), col = col[1],   lwd = lwds, ...)
>     if (is.na(yylab[[1]]))
>         mtext(deparse(substitute(yright)), side = 4, outer = T,   line = 1, col = col[1], ...)  else mtext(yylab[[1]], side = 4, outer = T, line = 1, col = col[1],    ...)
>     par(new = T)
>     plot(x, yleft, ylim = yleftlim, ylab = "", axes = F, xlab = xlab,
>         pch = pch[2], col = col[2], ...)
>     box()
>     axis(2, pretty(range(yleft, na.rm = T), 10), col = col[2],
>         col.axis = col[2])
>     if (!inherits(x, c("Date", "POSIXt")))
>         axis(1, pretty(range(x, na.rm = T), 10))
>     else {
>         l <- length(x)
>         axis(1, at = x[seq(1, l, length = length)], labels = format(as.POSIXct(x[seq(1,  l, length = length)]), format = format))
>     }
>     if (is.na(yylab[[2]]))
>         mtext(deparse(substitute(yleft)), side = 2, line = 2,   col = col[2], ...)
>     else mtext(yylab[[2]], side = 2, line = 2, col = col[2],    ...)
>     if (linky)
>         lines(x, yleft, col = col[2], lty = 2, ...)
>     if (smooth != 0)
>         lines(supsmu(x, yleft, span = smooth), col = col[2],    lty = 2, lwd = lwds, ...)
> }
>
> something like
>
> plot.yy(Year, Li, CR)
>
> Cheers
> Petr
>
> > -----Original Message-----
> > From: R-help <r-help-bounces using r-project.org> On Behalf Of Ogbos Okike
> > Sent: Thursday, November 29, 2018 2:33 PM
> > To: r-help <r-help using r-project.org>
> > Subject: [R] Correct x-axis of two in one graph
> >
> > Dear Contributors,
> >
> > I have a data of the form:
> > 4 8 10 8590 12516
> > 4 8 11 8641 98143
> > 4 8 12 8705 98916
> > 4 8 13 8750 89911
> > 4 8 14 8685 104835
> > 4 8 15 8629 121963
> > 4 8 16 8676 77655
> > 4 8 17 8577 81081
> > 4 8 18 8593 83385
> > 4 8 19 8642 112164
> > 4 8 20 8708 103684
> > 4 8 21 8622 83982
> > 4 8 22 8593 75944
> > 4 8 23 8600 97036
> > 4 8 24 8650 104911
> > 4 8 25 8730 114098
> > 4 8 26 8731 99421
> > 4 8 27 8715 85707
> > 4 8 28 8717 81273
> > 4 8 29 8739 106462
> > 4 8 30 8684 110635
> > 4 8 31 8713 105214
> > 4 9 1 8771 92456
> > 4 9 2 8759 109270
> > 4 9 3 8762 99150
> > 4 9 4 8730 77306
> > 4 9 5 8780 86324
> > 4 9 6 8804 90214
> > 4 9 7 8797 99894
> > 4 9 8 8863 95177
> > 4 9 9 8873 95910
> > 4 9 10 8827 108511
> > 4 9 11 8806 115636
> > 4 9 12 8869 85542
> > 4 9 13 8854 111018
> > 4 9 14 8571 93247
> > 4 9 15 8533 85105
> > 4 9 16 8553 114725
> > 4 9 17 8561 122195
> > 4 9 18 8532 100945
> > 4 9 19 8560 108552
> > 4 9 20 8634 108707
> > 4 9 21 8646 117420
> > 4 9 22 8633 113823
> > 4 9 23 8680 82763
> > 4 9 24 8765 121072
> > 4 9 25 8756 89835
> > 4 9 26 8750 104578
> > 4 9 27 8790 88429
> > 4 9 28 8824 84022
> > 4 9 29 8843 80413
> > 4 9 30 8795 118462
> > 4 10 1 8795 139761
> > 4 10 2 8805 103049
> > 4 10 3 8826 104996
> > 4 10 4 8841 104496
> > 4 10 5 8882 75603
> > 4 10 6 8865 89768
> > 4 10 7 8875 93353
> > 4 10 8 8898 98410
> > 4 10 9 8920 114540
> > 4 10 10 8937 95220
> > 4 10 11 8926 104083
> > 4 10 12 8912 106089
> > 4 10 13 8898 116228
> > 4 10 14 8926 149610
> > 4 10 15 8954 109594
> > 4 10 16 8941 75008
> > 4 10 17 8952 119182
> > 4 10 18 9003 151011
> > 4 10 19 9002 146797
> > 4 10 20 8995 145769
> > 4 10 21 8969 121248
> > 4 10 22 8954 107991
> > 4 10 23 8992 128
> > 4 10 24 8981 23036
> > 4 10 25 8920 137485
> > 4 10 26 8899 131756
> > 4 10 27 8913 108729
> > 4 10 28 8874 109478
> > 4 10 29 8846 119627
> > 4 10 30 8867 89999
> > 4 10 31 8868 64833
> > 4 11 1 9004 95864
> > 4 11 2 9028 82322
> > 4 11 3 8969 95591
> > 4 11 4 8932 69378
> > 4 11 5 8929 74281
> > 4 11 6 8916 103261
> > 4 11 7 8807 92473
> > 4 11 8 8449 84344
> > 4 11 9 8484 127415
> > 4 11 10 8148 123826
> > 4 11 11 8282 100029
> > 4 11 12 8305 76205
> > 4 11 13 8380 105162
> > 4 11 14 8530 119533
> > 4 11 15 8642 106490
> > 4 11 16 8780 114771
> > 4 11 17 8890 55593
> > 4 11 18 8962 227
> > 4 11 19 8949 109699
> > 4 11 20 8974 86004
> > 4 11 21 8956 74496
> > 4 11 22 8881 109350
> > 4 11 23 8872 134020
> > 4 11 24 8847 105212
> > 4 11 25 8868 91512
> > where columns 1= year 2004, 2 = month, 3= day, 4= CR data, 5= Lightning data.
> >
> > The data range is 2004/8/10 to 2008/8/22.
> > With the code below:
> > data <- read.table("CRandWWLLNremzro", col.names = c("year", "month",
> > "day", "CR","WW"))
> >
> >
> > new.century <- data$year < 50
> >
> > data$year <- ifelse(new.century, data$year + 2000, data$year + 1900)
> >
> > data$date <- as.Date(ISOdate(data$year, data$month, data$day))
> > x = data$date
> >  CR = data$CR
> > WWLLN=data$WW
> >
> > Year<-x
> > Li<-WWLLN
> > CR<-CR
> >
> > setEPS()
> >  postscript("twoinone2.eps")
> >  par(mar = c(5, 4, 4, 4) + 2) # Leave space for z axis
> > library(plotrix)
> > plot(Year,Li,pch=16,axes=F,xlab="",ylab="",type="l",col="black")
> > axis(2, col="black",las=1)  ## las=1 makes horizontal labels
> > mtext("Lightning Stroke/day", side=2, line=4)
> > #box()
> > par(new=TRUE)
> > plot(Year,CR, pch=15, xlab="",ylab="",axes=FALSE,type="l",col="red")
> > mtext("GCR count/day",side=4,col="red",line=3)
> > axis(4, ylim=c(-7499,9684), col="red",col.axis="red",las=1)
> >
> >
> >
> > axis(side=1) ##
> > mtext("YEAR",side=1,col="black",line=2.5)
> > mtext("Long-term Variation betwenn WWLLN and
> > GCRs",side=3,col="black",line=2.5)
> >
> >
> > legend("topleft",col=c("red","black"),lty=1,legend=c("GCRs","WWLLN"))
> >
> > dev.off()
> >
> >   I got the correct axes except that the x-axis is not what I want. I
> > want the date to appear on the x-axis. i actually got that by using:
> > axis(side=1,at=c(1,400,800,1200,1500),labels=c("2004","2005","2006","2007",
> > "2008"))
> >
> > But there is a little time shift, indicating that the above line may
> > not have been assigned correctly.
> >
> > I then fiddled with  axis(side=1) to see if the correct date can
> > naturally appear. I could not succeed.
> >
> > I can attach the plot I generated. Instead of date appearing on the
> > x-axis, ordinary numbers appeared.
> >
> > I guess the best way of getting the correct date on x-axis is to allow
> > the system to fix the date as data points are many. But I don't know
> > how to do that.
> >
> > Thank you in advance for your usual kind help.
> > Best regards
> > Ogbos
> >
> > ______________________________________________
> > R-help using r-project.org 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.
> Osobní údaje: Informace o zpracování a ochraně osobních údajů obchodních partnerů PRECHEZA a.s. jsou zveřejněny na: https://www.precheza.cz/zasady-ochrany-osobnich-udaju/ | Information about processing and protection of business partner’s personal data are available on website: https://www.precheza.cz/en/personal-data-protection-principles/
> Důvěrnost: Tento e-mail a jakékoliv k němu připojené dokumenty jsou důvěrné a podléhají tomuto právně závaznému prohláąení o vyloučení odpovědnosti: https://www.precheza.cz/01-dovetek/ | This email and any documents attached to it may be confidential and are subject to the legally binding disclaimer: https://www.precheza.cz/en/01-disclaimer/
>

-------------- next part --------------
A non-text attachment was scrubbed...
Name: testrun.png
Type: image/png
Size: 99905 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20181129/7d08589e/attachment.png>


More information about the R-help mailing list