[R] categorical variable in scatterplot (car)

Peter Ehlers ehlers at ucalgary.ca
Sun Apr 25 19:20:29 CEST 2010


Hi John,

The problem seems to be with the order in which the 'levels' of
the conditioning variable appear. Here's a reproducible example:

  Prestige$tp<- with(Prestige, ifelse(type == "prof", 0, 1))
  scatterplot(prestige ~ income | tp, data=Prestige)

Note that I've just switched the 0/1 from your example.

A quick look at scatterplot.formula suggests that wrapping
the 'X[, 3]' in this line

    scatterplot(X[, 2], X[, 1], groups = X[, 3], xlab = xlab,

inside an as.factor() would solve the problem.

  -Peter


On 2010-04-25 8:40, John Fox wrote:
> Dear Peter and Anthony,
>
> Thanks, Peter, for answering the question, but scatterplot() should work
> even if z is not a factor, and does for me in the following example:
>
>> library(car)
>> Prestige$tp<- with(Prestige, ifelse(type == "prof", 1, 0))
>> scatterplot(prestige ~ income | tp, data=Prestige)
>
> So, Anthony, the usual advice about providing a reproducible example seems
> applicable here.
>
> Regards,
>   John
>
> --------------------------------
> John Fox
> Senator William McMaster
>    Professor of Social Statistics
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada
> web: socserv.mcmaster.ca/jfox
>
>
>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On
>> Behalf Of Peter Ehlers
>> Sent: April-24-10 11:57 PM
>> To: Anthony Lopez
>> Cc: R-help at r-project.org
>> Subject: Re: [R] categorical variable in scatterplot (car)
>>
>> On 2010-04-24 21:30, Anthony Lopez wrote:
>>> Hello R folks,
>>>
>>> I am encountering a problem with the following scatterplot function from
>> the
>>> car package:
>>>
>>>> scatterplot(y~x|z)
>>>
>>> where y and x are continuous (interval) random variables and z is a
>>> categorical variable.  When z is a categorical variable coded 1 or 2, I
>>> (appropriately) get a scatterplot of y by x, coded by z.  Similarly,
> when z
>>> is a categorical variable coded 1, 2, or 3, there is again, no problem.
>>>    However, when z is a categorical variable coded 0 or 1, the
> scatterplot
>>>
>>>> scatterplot(y~x|z)
>>>
>>> is exactly identical to the one generated by
>>>
>>>> scatterplot(y~x)
>>>
>>> It is not possible that this is due to the fact that there is no
> difference
>>> between the categories.  It is as if R doesn't "see" that I want it
> coded
>> by
>>> z.  But this only happens when one of the categories of z is coded "0"
>> (i.e.
>>> zero).  Any ideas why this is so, or how I can fix this without recoding
> my
>>> variable?
>>
>> Make z a factor (which it really should be anyway).
>>
>>    -Peter Ehlers
>>
>>>
>>> Thank you!
>>>
>>> Anthony
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help at r-project.org 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.
>>>
>>>
>>
>> --
>> Peter Ehlers
>> University of Calgary
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>
>
>

-- 
Peter Ehlers
University of Calgary



More information about the R-help mailing list