[R] forcing all tick labels to plot

Marc Schwartz MSchwartz at MedAnalytics.com
Wed Jan 19 22:12:13 CET 2005


On Wed, 2005-01-19 at 15:43 -0500, William Briggs wrote:
> I'm trying to find a way to force all the x-axis tick labels to plot, 
> regardless whether or not they overlap or look pretty.
> 
> V is a factor with, say, 4 levels.  A call to plot(V) gives a 
> histogram-like plot, one bar for each level in V.  The problem is that 
> all the label names may not be plotted because some of the names are 
> lengthy and would tend to overlap if plotted.
> 
> I don't care if they do, I want to see all the labels, overplotted or not.
> 
> I tried:
> 
>      plot(V,axes=F)
>      d<-levels(V)
>      axis(1,at=1:4,label=d)
>      axis(2)
> 
> But this does nothing more than the default, that is, only some of the 
> labels print.  The other problem is the "at=1:4", which is my guessing 
> where the tickmarks go.  The function "axTicks(1)" may help, but it 
> frequently has more tick marks then levels in "V", so again I have to 
> guess which goes where.
> 
> I see this:
> 
>      c<-par()
>      c$xaxp = 0.5 3.5 4
> 
> or something similar.  Is there another par() in which the tick marks 
> that are calculated are given explicitly?
> 
> I have tried:
> 
>      par(cex=.7)
> 
> and that works for some V, but not for all, and it isn't a very general 
> solution.
> 
> Is there some flag that I can set to force all the tick labels to plot?
> 
> Matt Briggs

plot.factor() is the plot method used in this case. Since it uses
barplot() internally, it will return the bar midpoints invisibly (which
I noted is not defined as a return value in the help for plot.factor)

You can then use mtext() to force the drawing of the labels:

# Draw the plot, but not the x axis
# This example presumes V is a four level factor
mp <- plot(V, xaxt = "n")

# Create some long labels
labels <- c(paste("Very Very Very Long Label Here", 1:4, sep = ""))

# Now plot the labels below the bar midpoint
mtext(side = 1, labels, at= mp, line = 1)

That should do it.

See ?mtext and ?barplot for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list