jeudi 11 avril 2019

How to format x-axis of my plot in R based on color condition?

I am using SPEI package that comes with data and some plotting functionality. I want to format the x-axis of the plot to reflect month-year (or only year) instead year sequence.

install.packages("SPEI")
library(SPEI)
data("wichita")
wichita$PET=hargreaves(Tmin=wichita$TMIN, Tmax = wichita$TMAX, lat = 37.64)
wichita$BAL=wichita$PRCP - wichita$PET
SPEI_12=spei(wichita[,"BAL"],12)
plot(SPEI_12,main = "12 month SPEI")

#I tried
SPEIData=SPEI_12$fitted
myDates=as.data.frame(seq(as.Date("1980-01-01"), to=as.Date("2011-10-31"), by="month"))
names(myDates)= "Dates"
plot(SPEIData,, axes=FALSE,  axis.Date(1, at=myDates$Dates, format = "%m-%Y"),col=ifelse(SPEIData_12 < 0,"red","blue"))

The plot should somewhat look like this

https://www.bing.com/images/search?view=detailV2&id=217AB349ABF81803FA21C35E5F9A37F01BE5AFAB&thid=OIP.rqEtCxtk7nm-f2KIRBm-DAHaDh&mediaurl=http%3A%2F%2Fspei.csic.es%2Fimg%2Ffig5.jpg&exph=500&expw=1050&q=SPEI+drought+index&selectedindex=31&ajaxhist=0&vt=0&eim=1,6&ccid=rqEtCxtk&simid=608050454551137267

Aucun commentaire:

Enregistrer un commentaire