[R] Use of axis() in conjunction with plot(..., axes=F)

Marc Schwartz (via MN) mschwartz at mn.rr.com
Mon Nov 28 23:38:23 CET 2005


On Mon, 2005-11-28 at 13:18 -0800, Dennis Fisher wrote:
> Colleagues
> 
> On occasion, I want to control either tick marks or labels in axes  
> different from the defaults created with "axes=T" in the plot  
> command.  If I invoke "axes=F" and axis(n), I can do so.  However,  
> the axes produced by axis() differ slightly from those produced  
> within plot.  I have "bty" in par set to "l" (i.e., left and bottom  
> axes only).  Differences include:
> 
> 1.  when an axis contains a factor, plot() produces axes showing the  
> actual factors whereas axis() replaces these factors with integers  
> representing the level of the factor (e.g., if the factor is  
> countries, plot() yields axes labeled "Argentina", "Brazil", etc.  
> whereas axes() yields 1, 2, ...

Without your actual code here, I may be wrong, but I am going to guess
that the difference is that when you are using a factor, plot.factor()
gets used, which ends up using barplot() in the case where 'x' is a
factor.

plot.factor() by default uses table(x) on the 'x' argument from the
initial plot() call and passes this to barplot().

barplot() by default uses the names attribute of the table object
created above as the 'names.arg' argument and thus, the resultant labels
on the x axis. Hence, there is an implicit coercion of the factor levels
to character by using table().

If you specify axes = FALSE, there is no implicit coercion that takes
place. You can do that yourself of course by using as.character(FACTOR)
for the labels argument.

See ?plot.factor and ?barplot for more information here.

> 2.  axes produced by plot() are "full-length" (i.e., the axes connect  
> at the corner of the plot) whereas axes produced by axis() are not  
> full-length (i.e., they run between the smallest and largest label).   
> It appears that this can be overcome by using the "at" option within  
> axes().  However, I cannot figure out how to use the "at" option when  
> the axis is a factor.
> 
> Thoughts?

For number 2, plotting functions such as plot.default use box() to place
a full frame around the plot region. In plot.default(), this is done
using the 'frame.plot' argument, which is set by default to the value of
the  'axes' argument. So, if you set 'axes = FALSE', box() is not used.
So just call it explicitly after using axis(). See ?box().

HTH,

Marc Schwartz




More information about the R-help mailing list