Hi, I am new to R and am trying to create a graph with Time(24hr) along the x axis. Rather than start at 01.00, I wanted to start at 14.00. I tried to use the axis(side=1, at=c( )) function but it continues to put then in numeric order. Is there another way I can add labels to the x axis? Thank You. Michael [[alternative HTML version deleted]]
Did you add xaxt = "n" in the plot function?
Try the following:
plot(x,y, xaxt = "n")
axis(1, at = c(14, 20),labels = c("14h", "20h") )
2014-07-01 12:41 GMT-05:00 Michael Millar <michael88millar@hotmail.co.uk>:
> Hi,
>
> I am new to R and am trying to create a graph with Time(24hr) along the x
> axis. Rather than start at 01.00, I wanted to start at 14.00.
>
> I tried to use the axis(side=1, at=c( )) function but it continues to put
> then in numeric order. Is there another way I can add labels to the x axis?
>
> Thank You.
>
> Michael
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@r-project.org mailing list
> 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]]
On Tue, 1 Jul 2014 06:41:52 PM Michael Millar wrote:> Hi, > > I am new to R and am trying to create a graph with Time(24hr) alongthe x> axis. Rather than start at 01.00, I wanted to start at 14.00. > > I tried to use the axis(side=1, at=c( )) function but it continues to put > then in numeric order. Is there another way I can add labels to the xaxis?>Hi Michael, Perhaps this will get you out of trouble. mmdat<-data.frame(time=paste(c(14:23,0:13),"00",sep=":"), wind_speed=sample(0:30,24)) plot(mmdat$wind_speed,type="b",xaxt="n",xlab="Time") axis(1,at=1:24,labels=mmdat$time) If you want to get more tick labels on the time axis, look at staxlab (plotrix). Jim
Hi Michael
Dates and times are always a problem as they are irregular not 1,2,3 ...,
100
If you want more fancy formatting of the x axis try this
First convert your time to a datetime class
# Use a dummy date for datetime as it is easier
mmdat$time <- seq(strptime("20140702 14", "%Y%m%d %H"),
by = "hours",
length= 24)
# only gives numerical sequence on xlab
plot(mmdat$wind_speed,type="b",xlab="Time")
However
library(lattice)
?xyplot
# by starting at 15:00 hours get sequence and use formatting of dates
xyplot(wind_speed ~time, data = mmdat,
type = "b",
xlab="Time",
scales = list(x = list(at = seq(mmdat[2,1], by = "3 hours",
length 8),
labels = format(seq(mmdat[2,1], by = "3
hours", length = 8),"%H:%M")))
)
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Michael Millar
Sent: Wednesday, 2 July 2014 03:42
To: r-help at R-project.org
Subject: [R] x axis labelling
Hi,
I am new to R and am trying to create a graph with Time(24hr) along the x
axis. Rather than start at 01.00, I wanted to start at 14.00.
I tried to use the axis(side=1, at=c( )) function but it continues to put
then in numeric order. Is there another way I can add labels to the x axis?
Thank You.
Michael
[[alternative HTML version deleted]]
______________________________________________
R-help at r-project.org mailing list
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.