Dear Gurus, Using R 2.4.1 on Windows XP 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. I have attached a PDF. # 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
On Tue, 2007-03-20 at 18:04 -0400, Michael H. Prager wrote:> Dear Gurus, > > Using R 2.4.1 on Windows XP > > 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. > > I have attached a PDF. > > # 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, NCMike, 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(). BTW, your PDF did not come through. HTH, Marc Schwartz
Michael H. Prager wrote:> Dear Gurus, > > Using R 2.4.1 on Windows XP > > 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. > > I have attached a PDF. > > # sample code (simplified) # > mp <- barplot(t(N.age), xlab = "Year", axisnames = FALSE) > axis(side = 1, at = mp, labels = rownames(N.age), tcl = -0.75) >Hi Mike, PDF didn't get through, but I think I understand what you want. Here's a suggestion: oddaxlab<-function(side=1,at,labels) { at<-at[which(nchar(labels)>0)] labels<-labels[which(nchar(labels)>0)] axis(side,at,labels) } groupie<-matrix(rnorm(32)+3,nrow=8) labels<-matrix(c(c("1","","","","5","","7",""), c("","","","","13","","",""), c("17","","","20","","","",""), c("25","","","","","","31","")),nrow=8) bpos<-barplot(groupie,beside=TRUE) oddaxlab(at=barpos,labels=labels) What the function does is only draw ticks and labels where there is a non-empty label. You pass only those labels that you want to appear. Hope I have gotten the right idea. Jim