[R] Ticks on barplots

Stephen Tucker brown_emu at yahoo.com
Thu Mar 22 06:22:31 CET 2007


Hi Mike, you can try using axTicks as in this example (you can also use
pretty instead).

Below, instead of a barplot() I have used plot() but with type="h" and lend=3
(see ?par for details on lend) which I have found to be useful at times when
making barchart-like plots with time-series. In any case, barplot() or
plot(), I hope this illustration will be helpful:


# define function to convert numeric to POSIXct
# from http://tolstoy.newcastle.edu.au/R/help/04/05/0980.html
numToPOSIXct <- function(v) {
  now <- Sys.time()
  Epoch <- now - as.numeric(now)
  Epoch + v
}

# define example data
x <- seq(as.POSIXct("1950-01-01 00:00:00"),as.POSIXct("2000-01-01 
     00:00:00"),by="year")
y <- runif(length(x))

# plot and label axis with with axTicks()
plot(x,y,type="h",lwd=3,col=8,xaxt="n",lend=3)
axis(1,at=axTicks(1),lab=format(numToPOSIXct(axTicks(1)),"%Y"))



--- Mike Prager <mike.prager at noaa.gov> wrote:

> Marc Schwartz <marc_schwartz at comcast.net> wrote:
> 
> > On Tue, 2007-03-20 at 18:04 -0400, Michael H. Prager wrote:
> > > I am generating stacked barplots of age-composition of fish populations
> 
> > > (Y) over time (X).  As there are many years, not every bars is labeled.
>  
> > > When looking at the plot, it becomes difficult to associate labels with
> 
> > > their bars.
> > > 
> > > We have improved this a bit by using axis() to add a tickmark below
> each 
> > > bar.  Can anyone suggest a way to draw ticks ONLY at bars where a tick 
> > > label is drawn?  Or to make such ticks longer than those where there is
> 
> > > no label?
> > > 
> > > This is going into a function, so I'm hoping for a method that doesn't 
> > > require looking at the plot first.
> > >
> > > # sample code (simplified) #
> > > mp <- barplot(t(N.age), xlab = "Year", axisnames = FALSE)
> > > axis(side = 1, at = mp, labels = rownames(N.age), tcl = -0.75)
> > > 
> > > Thanks!
> > > 
> > > Mike Prager
> > > NOAA, Beaufort, NC
> > 
> > Mike,
> > 
> > How about something like this:
> > 
> >   mp <- barplot(1:50, axisnames = FALSE)
> > 
> >   # Create short tick marks at each bar
> >   axis(1, at = mp, labels = rep("", 50), tcl = -0.25)
> > 
> >   # Create longer tick marks every 5 years with labels
> >   axis(1, at = mp[seq(1, 50, 5)], 
> >        labels = 1900 + seq(0, 45, 5), tcl = -0.75, las = 2, 
> >        cex.axis = 0.75)
> > 
> > 
> > Just pick which labels you want to be shown (eg. every 5 years) and
> > synchronize the values of those with the 'at' argument in axis().
> > 
> > HTH,
> > 
> > Marc Schwartz
> > 
> 
> Thanks, Marc, for this solution and thanks equally to Jim Lemon
> for a similar idea.  This seems promising.  Since this is to go
> into a function (and should work without intervention), I'll
> need to devise an algorithm to decide at what interval the
> labels should be plotted.  Clearly "axis()" has such an
> algorithm.  Unfortunately, it reports its result only by placing
> the labels.
> 
> Mike
> 
> -- 
> Mike Prager, NOAA, Beaufort, NC
> * Opinions expressed are personal and not represented otherwise.
> * Any use of tradenames does not constitute a NOAA endorsement.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 



 
____________________________________________________________________________________
Expecting? Get great news right away with email Auto-Check.



More information about the R-help mailing list