[R] Σχετ: plotting dates in x axis

Jim Lemon drjimlemon at gmail.com
Tue Feb 28 22:12:21 CET 2017


Hi Maria,
No problem. Taking the example I used before:

aa<-data.frame(var1=runif(180),
 SDATE=paste(sample(1998:2012,180,TRUE),
 sample(1:12,180,TRUE),sample(1:28,180,TRUE),sep="-"))
aa$sdate<-as.Date(aa$SDATE)
plot(aa$sdate,aa$var1,xaxt="n")
# set the tick marks at the middle of every other year
axis.dates<-as.Date(paste(seq(1998,2012,by=2),6,30,sep="-"))
axis(1,axis.dates,seq(1998,2012,by=2))

If you want to add months, the "axis" function will drop some of the
dates. The "staxlab" function uses either multiple lines or rotates
the labels to fit more in:

plot(aa$sdate,aa$var1,xaxt="n")
library(plotrix)
staxlab(1,axis.dates,format(axis.dates,"%b-%Y"),srt=45)

Jim


On Tue, Feb 28, 2017 at 11:13 PM, Maria Lathouri <mlathouri at yahoo.gr> wrote:
> Dear Jim
>
> Many thanks for this. I tried and it somehow worked. I removed the nlines=3
> from your script so I can have the years in one line, but still the problem
> is that while trying to show all the years, there are years in the first
> line and then other years in a second line.
>
> I was wondering if I can show every two years instead. I think it would be
> much better.
>
> Many thanks.
>
> Kind regards,
> Maria
>
>
> Στις 11:56 π.μ. Τρίτη, 28 Φεβρουαρίου 2017, ο/η Maria Lathouri via R-help
> <r-help at r-project.org> έγραψε:
>
>
> Dear all,
> Sorry about that. My mistake. Here is an example of my data
>> head(aa)       SDATE  var11 1998-01-29 0.7282 1998-02-17 1.0803 1998-03-20
>> 0.6374 1998-05-07 1.1205 1998-05-26 0.9036 1998-06-05
>> 1.210.........................178 2012-10-4  0.71179 2012-11-4  0.663180
>> 2012-12-10 0.484
>
> I hope this helps.
> Maria
>
>     Στις 10:00 μ.μ. Δευτέρα, 27 Φεβρουαρίου 2017, ο/η Jeff Newmiller
> <jdnewmil at dcn.davis.ca.us> έγραψε:
>
>
> While humorous, the term "Mexican Wall" is unlikely to be clear to the OP.
> This is a reference to the mailing list anti-virus strategy of cutting out
> attachments that don't meet a very restrictive set of requirements outlined
> in the Posting Guide that all R-users are supposed to have read and
> memorized, but which few even seem to know exists. You can best avoid the
> "Mexican Wall" by setting your email program to send plain text instead of
> HTML, and to embed your R code example (believe it or not, this is not the
> Excel-To-R translation service, so show us your R code and text data, not
> your Excel file) in the email along with your description of your problem.
> For more help on getting help search the Internet for "R reproducible
> example".
> --
> Sent from my phone. Please excuse my brevity.
>
> On February 27, 2017 1:11:05 PM PST, Jim Lemon <drjimlemon at gmail.com> wrote:
>>Hi Maria,
>>First, Excel files don't make it through the Mexican Wall. A CSV with
>>the extension changed to .txt might. You can get all of the years like
>>this:
>>
>>aa<-data.frame(var1=runif(180),
>> SDATE=paste(sample(1998:2012,180,TRUE),
>> sample(1:12,180,TRUE),sample(1:28,180,TRUE),sep="-"))
>>aa$sdate<-as.Date(aa$SDATE)
>>plot(aa$sdate,aa$var1,xaxt="n")
>>library(plotrix)
>># set the tick marks at the middle of each year
>>axis.dates<-as.Date(paste(1998:2012,6,30,sep="-"))
>>staxlab(1,axis.dates,1998:2012,nlines=3)
>>
>>Obviously you don't want all of the months, so just add the months to
>>the years:
>>
>>plot(aa$sdate,aa$var1,xaxt="n")
>>staxlab(1,axis.dates,format(axis.dates,"%b/%Y"),nlines=3)
>>
>>Jim
>>
>>
>>On Tue, Feb 28, 2017 at 2:26 AM, Maria Lathouri via R-help
>><r-help at r-project.org> wrote:
>>> Dear all,
>>> I have an excel file of 180 observations with dates and one variable,
>>from 1998 to 2012 by random months (there are some years that I might
>>not have all the months or I might have two observations in one month).
>>I am trying to plot the dates in x axis and the variable in y axis. I
>>have already used as.Date for the dates so I can import them into R.
>>> Here is my script:> aa<-read.csv("aa.csv")> attach(aa)>
>>names(aa)#"SDATE" "var1"
>>> I convert the dates into R: > sdate<-as.Date(SDATE,
>>format="%Y-%m-%d")
>>>
>>> I am plotting the dates with my var1:> plot(sdate, var1, type="l")
>>> Up to now, everything seems ok. However, in the x-axis I only get
>>three years, 2000, 2005 and 2010. As I want to show all the years or at
>>least as many as it could be, I am using the following:
>>>> plot(sdate, var1, type="l", xaxt="n")
>>>
>>>> d1<-c((sdate[1]), (sdate[183]))> d2<-as.Date((d1[1])+365*(0:15))
>>>> axis(side=1, at=0:15, labels=strftime(d2, format="%Y"),
>>cex.axis=0.8,las=2);  I tried also to plot the dates in a month-Year
>>form:
>>>
>>>> d2<-as.Date((d1[1])+150*(0:20))
>>>> plot(sdate, var1, type="l", xaxt="n")> axis(side=1, at=0:15,
>>labels=strftime(d2, format="%m-%Y"), cex.axis=0.8,las=2)
>>>
>>> But nothing happened. I cannot understand why it doesn't show
>>anything.
>>> I have attached the file as well in case you want to have a more
>>clear picture.
>>> I really appreciate it if you can help me on this.
>>> Thank you very much in advance.
>>> Kind regards,Maria
>>> ______________________________________________
>>> R-help at 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.
>>
>>______________________________________________
>>R-help at 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.
>
>
>     [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at 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.
>
>



More information about the R-help mailing list