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

Ogbos Okike giftedlife2014 @ending from gm@il@com
Thu Nov 29 14:33:15 CET 2018


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



More information about the R-help mailing list