[R] unexpected plot behavior

Marc Schwartz marc_schwartz at me.com
Sun Apr 22 17:30:58 CEST 2012


On Apr 22, 2012, at 1:25 AM, Martin Renner wrote:

> Thank you for the replies, Uwe and Marc. These are explanations that make perfect sense. However, shouldn't the behavior of plot.factor include the option of type = "n" for consistency with the default plot function? 
> 
> Best,
> Martin


I don't believe so.

The use of type = "n" is to facilitate the creation of a plotting environment, into which you can, in a piecemeal fashion, create a new plot from a blank canvas. The nature of that plot could be virtually anything with symbols, lines/curves, shapes and perhaps even pure text.

Since plot.factor() internally calls one of several specific plot functions (eg. boxplot(), barplot(), spineplot() or plot()) depending upon the nature of the argument(s) passed, you need to understand exactly what you intend to do with a blank plotting device as each of those functions has it's own set of characteristics, defaults and intents.

Thus, having plot() or more specifically, plot.default(), support the type = "n" paradigm, is sufficient in creating a plot device with desired axis ranges, parameters and so forth, to then enable you to then add whatever additional content you require.

There is no a priori expectation that a function's "child" methods inherit all of the parent's functionality, because the generic default method's functionality may not be apropos to the child classes. Similarly, the child classes may implement specific functionality not apropos to the generic parent class because more specific information is known about the structure of the child. 

Regards,

Marc

> On 21 Apr 2012, at 08:18 , Marc Schwartz wrote:
> 
>> On Apr 21, 2012, at 9:49 AM, Martin Renner wrote:
>> 
>>> When plotting a numerical vector against a factor, 'type="n"' seems to have no affect, e.g. 
>>>> plot (1:10~factor (1:10), type = "n")
>>> 
>>> looks just like
>>>> plot (1:10~factor (1:10))
>>> 
>>> Plotting a numerical against itself works as expected: 
>>>> plot (1:10, type = "n")
>>> 
>>> I see the same behavior under debian gnu/linux, Mac OS X, and Win7 (all current versions, see below). Is this a bug? 
>>> 
>>> Regards,
>>> Martin
>> 
>> 
>> 
>> This has to do with method dispatch. See ?plot.formula, which is the plot method called you pass a formula, as opposed to passing a vector as in your third example. 
>> 
>> In this case, ?plot.factor is called when the 'x' part of the formula (RHS) is a factor. When plot.factor is called, it internally calls ?boxplot and of course, there is no "type = 'n'" for boxplots, hence it is ignored.
>> 
>> Regards,
>> 
>> Marc Schwartz
>>



More information about the R-help mailing list