Skip to content

adding infrequent date labels to x-axis

7 messages · Hrishi Mittal, Morway, Eric, Peter Ehlers

#
I'm sure there is a clever way to do the following, but I've been unable to
find it on this forum or by writing my own functions.  I have 8 years worth
of weekly data but would like to restrict the labels on the x-axis to months
only.  I've included the first year's worth of data below. 

My line of thought has been along these lines

x<-seq(as.Date("1999-04-01"),as.Date("2007-10-25"),by="1 month")
y<-as.POSIXlt(x)$mon+1
months<-month.name
month.names<-months[as.numeric(y)]
month.names<-substr(month.names,1,3)

plot(cropped.cast1$date,cropped.cast1$Frac_ET_Satsfd_mean,xaxt='n')
mtext(text=month.names,side=1,at=???

I'm not sure how to tie "month.names" back to their true location on the
x-axis?

I appreciate any insights, Eric
Also,
[1] "POSIXt"  "POSIXct"
date Frac_ET_Satsfd_mean Frac_ET_Satsfd_sd
1999-04-08           0.8344885        0.13545515
1999-04-15           0.8355466        0.12810387
1999-04-22           0.8595579        0.11259251
1999-04-29           0.8997225        0.09611060
1999-05-06           0.8714364        0.09527164
1999-05-13           0.8530203        0.11088544
1999-05-20           0.8437866        0.12689882
1999-05-27           0.8310003        0.13985307
1999-06-03           0.8031203        0.15851422
1999-06-10           0.8288505        0.12827027
1999-06-17           0.8251130        0.13051783
1999-06-24           0.8227639        0.14227501
1999-07-01           0.7914689        0.15892716
1999-07-08           0.8050929        0.14465413
1999-07-15           0.8370141        0.11843615
1999-07-22           0.8448697        0.10823010
1999-07-29           0.8561925        0.10694348
1999-08-05           0.8520790        0.09953065
1999-08-12           0.8429925        0.10545427
1999-08-19           0.8397966        0.11629002
1999-08-26           0.8367953        0.12363411
1999-09-02           0.8219479        0.13870596
1999-09-09           0.8218193        0.13617427
1999-09-16           0.8217584        0.13346997
1999-09-23           0.8216834        0.13304117
1999-09-30           0.8111005        0.14367143
1999-10-07           0.8090813        0.14967750
1999-10-14           0.8265188        0.13484263
1999-10-21           0.8391333        0.11873929
1999-10-28           0.8426102        0.11215439
1999-11-04           0.8431813        0.11007485
1999-11-11           0.8394140        0.11206864
1999-11-18           0.8350650        0.11042384
1999-11-25           0.8360082        0.11011926
1999-12-02           0.8362129        0.10834491
1999-12-09           0.8377512        0.10519698
1999-12-16           0.8367339        0.10176535
1999-12-23           0.8338621        0.10273662
1999-12-30           0.8317094        0.10470654
#
Hi emorway,

It seems to me that all you need is one command:

plot(as.Date(cropped.cast1$date,"%Y-%m-%d"),cropped.cast1$Frac_ET_Satsfd_mean)

-----
Try  http://prettygraph.com Pretty Graph , the easiest way to make R-powered
graphs on the web.
#
Hi emorway,

You are right. I tried the command only with one year's data, in which case
it plots month names as labels. But for multiple years, the labels become
years. I found this old post useful -
http://n4.nabble.com/Month-tick-marks-on-a-plot-td879121.html#a879121.
Picking the code from the comment by Gavin Simson, I think this should work:


plot(as.Date(cropped.cast1$date,"%Y-%m-%d"),cropped.cast1$Frac_ET_Satsfd_mean,xaxt="n") 

x<-as.Date(cropped.cast1$date,"%Y-%m-%d")

ticks.at <- seq(min(x), max(x), by = "months")
## format the labels as abbreviated month names
ticks.lab <- format(ticks.at, format = "%b")
## indicator variable; is month January?
m1 <- ticks.lab == "Jan"
## plot small ticks and labels for months not Jan
Axis(x, at = ticks.at[!m1], side = 1,
     labels = ticks.lab[!m1], las = 2, cex.axis = 0.7)
## plot the default tick locations for years
Axis(x, side = 1, las = 2)
## add the box 
box()



-----
Try  http://prettygraph.com Pretty Graph , the easiest way to make R-powered
graphs on the web.
1 day later
#
Hi Hrishi, 

With regard to the post you helped me out with, I've got my graph almost
dialed in the way I would like it.  There was one more small thing my
searches have been unable to turn up.  I've tried searching with "stagger
labels" "offset Labels" "alternate labels" to no avail.  

the pertinent lines of code I'm basing my question on are:
m2<-ticks.lab=="Feb"|ticks.lab=="May"|ticks.lab=="Aug"|ticks.lab=="Nov"
Axis(x,at=ticks.at[m2],side=1,labels=ticks.lab[m2],las=1,cex.axis=0.75,tick=F,padj=-2)

I would prefer to use cex.axis=1, but because the labels crowd each other it
doesn't place them all.  Is there a command I can use that would essentially
stagger the labels?, like so...
   |
   |
   |   (Graph Area)
   +______________________________________
    May      Nov      May     Nov 
          Aug      Feb      Aug     Feb

http://n4.nabble.com/file/n1566433/TimeSeries_Example1.jpg 

Respectfully,
Eric
#
On 2010-02-23 12:58, emorway wrote:
The plotrix package has staxlab().

  -Peter Ehlers