[R] Scatterplot - symbols coded by factor

Sat Feb 10 14:17:00 CET 2001

```At 08:51 10/02/01 +0000, Brian D Ripley wrote:
>On Fri, 9 Feb 2001, Matt Pocernich wrote:
>
>> Hi - I would like to create a scatterplot with the symbols coded by a
factor in each record.  Is their a better way than what I have used below?
I attempted to use the plot.factor, but cannot force it to make a
scatterplot instead of boxplots.  I have considerd making a function with a
'for' statement for each level of the factor.  Is there a better way for
when I am dealing with more levels of factors?
>>
>> Thanks,  Matt Pocernich
>>
>> Y <- c(40,38,30,47,25,32,13,35,27,24,20,13)
>> X<-c(110,75,93,97,60,75,38,140,62,90,45,59)
>> manuf <- factor(rep(LETTERS[1:3],c(4,4,4)))
>> data3 <- data.frame(X,Y,manuf)
>> attach(data3)
>>
>> plot(X, Y, type="n")
>> points(X[manuf=="A"], Y[manuf=="A"], pch = "A")
>> points(X[manuf=="B"], Y[manuf=="B"], pch = "B")
>> points(X[manuf=="C"], Y[manuf=="B"], pch = "C")
>
>You can use
>
>> plot(X, Y, type="n")
>> points(X, Y, pch=as.character(manuf))

The following (inspired from demo(graphics)) may also be useful:

plot(X, Y, pch=levels(manuf)[codes(manuf)])

The example in demo(graphics) shows a case where the user wants different
colours for the symbols with respect to a factor (here the Iris species):

pairs(iris[1:4], main = "Edgar Anderson's Iris Data",
pch = 21, bg = c("red", "green3", "blue")[codes(iris\$Species)])