Gonzalo Garcia-Perate
2010-Mar-01 11:16 UTC
[R] setting the steps for x axis labels on plot
Hello, I'm new to R, I've been working with it for the last 2 weeks. I am plotting some data and not getting the labels on the x axis I am expecting on my plot. my code reads #hours in the day h <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) #hp is a data frame with a pivot table of 25 columns (label and data for 24 hours) plot(h, as.matrix(hp[1,2:25]), main = hp[1,1], type="l", xlim=c(0,23), ylim=c(0,1800), xlab="hour", ylab="visitor activity") the result you can see here: http://www.flickr.com/photos/gonzillaaa/4397357491/ I was hoping the steps on the x axis would be every hour or at least every two hours I am getting unevenly spaced steps (0, 5, 10, 20) instead. apologies if this is too obvious a question, I've looked around on documentation etc. but found no reference to this. Many thanks, Gonzalo.
On 3/1/2010 6:16 AM, Gonzalo Garcia-Perate wrote:> Hello, I'm new to R, I've been working with it for the last 2 weeks. I > am plotting some data and not getting the labels on the x axis I am > expecting on my plot. > > > my code reads > > #hours in the day > h <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) > #hp is a data frame with a pivot table of 25 columns (label and data for > 24 hours) > plot(h, as.matrix(hp[1,2:25]), main = hp[1,1], type="l", xlim=c(0,23), > ylim=c(0,1800), xlab="hour", ylab="visitor activity") > > > the result you can see here: > http://www.flickr.com/photos/gonzillaaa/4397357491/ > > I was hoping the steps on the x axis would be every hour or at least > every two hours I am getting unevenly spaced steps (0, 5, 10, 20) instead. > > > > apologies if this is too obvious a question, I've looked around on > documentation etc. but found no reference to this.You can suppress the x axis in the call to plot() and then add it with a call to axis(). For, example plot(0:23, runif(24, min=0, max=1800), type="l", xlim=c(0,23), ylim=c(0,1800), xlab="hour", ylab="visitor activity", xaxt='n') axis(side=1, at=0:23, cex.axis=.5) ?axis> Many thanks, > > > Gonzalo. > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On 03/01/2010 10:16 PM, Gonzalo Garcia-Perate wrote:> Hello, I'm new to R, I've been working with it for the last 2 weeks. I > am plotting some data and not getting the labels on the x axis I am > expecting on my plot. > > > my code reads > > #hours in the day > h <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) > #hp is a data frame with a pivot table of 25 columns (label and data for > 24 hours) > plot(h, as.matrix(hp[1,2:25]), main = hp[1,1], type="l", xlim=c(0,23), > ylim=c(0,1800), xlab="hour", ylab="visitor activity") > > > the result you can see here: > http://www.flickr.com/photos/gonzillaaa/4397357491/ > > I was hoping the steps on the x axis would be every hour or at least > every two hours I am getting unevenly spaced steps (0, 5, 10, 20) instead. > > > > apologies if this is too obvious a question, I've looked around on > documentation etc. but found no reference to this. > >Hi Gonzalo, The plotting functions try to space the axis tick labels so that there is no crowding or overlapping. One way to get around this is to not display the x axis on the initial plot (xaxt="n") and then add a custom axis. One function that you can use is staxlab in the plotrix package. staxlab(1,at=0:23,labels=0:23) although you may want nlines=3 if you are willing to adjust the x axis label. Jim
Gonzalo Garcia-Perate
2010-Mar-01 17:18 UTC
[R] setting the steps for x axis labels on plot
Thank you both for your help, axis did the trick very nicely. On 1 March 2010 11:34, Chuck Cleland <ccleland at optonline.net> wrote:> On 3/1/2010 6:16 AM, Gonzalo Garcia-Perate wrote: >> Hello, I'm new to R, I've been working with it for the last 2 weeks. I >> am plotting some data and not getting the labels on the x axis I am >> expecting on my plot. >> >> >> my code reads >> >> #hours in the day >> h <- c(0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23) >> #hp is a data frame with a pivot table of 25 columns (label and data for >> 24 hours) >> plot(h, as.matrix(hp[1,2:25]), main = hp[1,1], type="l", xlim=c(0,23), >> ylim=c(0,1800), xlab="hour", ylab="visitor activity") >> >> >> the result you can see here: >> http://www.flickr.com/photos/gonzillaaa/4397357491/ >> >> I was hoping the steps on the x axis would be every hour or at least >> every two hours I am getting unevenly spaced steps (0, 5, 10, 20) instead. >> >> >> >> apologies if this is too obvious a question, I've looked around on >> documentation etc. but found no reference to this. > > ?You can suppress the x axis in the call to plot() and then add it with > a call to axis(). ?For, example > > > plot(0:23, runif(24, min=0, max=1800), type="l", xlim=c(0,23), > ylim=c(0,1800), xlab="hour", ylab="visitor activity", xaxt='n') > > axis(side=1, at=0:23, cex.axis=.5) > > ?axis > >> Many thanks, >> >> >> Gonzalo. >> >> ______________________________________________ >> 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. > > -- > Chuck Cleland, Ph.D. > NDRI, Inc. (www.ndri.org) > 71 West 23rd Street, 8th floor > New York, NY 10010 > tel: (212) 845-4495 (Tu, Th) > tel: (732) 512-0171 (M, W, F) > fax: (917) 438-0894 >