Skip to content
Back to formatted view

Raw Message

Message-ID: <5124961E.3030505@bitwrit.com.au>
Date: 2013-02-20T09:23:42Z
From: Jim Lemon
Subject: Scatterplot, Color by Grade Category
In-Reply-To: <1361349890311-4659135.post@n4.nabble.com>

On 02/20/2013 07:44 PM, David Arnold wrote:
> Hi,
>
> I have:
>
> hours=c(5,6,6,7,7,8,8,9,7,8,8,8,9,9,10,10,9,10,10,11,11,11,12);
> level=c(1.0,1.2,0.8,0.8,1.0,1.0,0.6,0.8,1.4,1.2,1.4,1.6,
>          1.2,1.4,1.0,1.4,1.6,1.6,1.8,1.4,1.6,1.8,1.6);
> grade=c(rep("First",8),rep("Second",8),rep("Third",7))
> length(hours)
> length(level)
> length(grade)
> data=data.frame(hours=hours,level=level,grade=grade)
> data
> plot(data$hours,data$level)
>
> Without using ggplot, just using core basic R, how can I:
>
> 1. Color each point according to the grade factor.
>
> 2. Select a different point type according to the grade factor.
>
Hi David,
As factors can be converted to numbers beginning with 1, you can do it 
easily:

plot(data$hours,data$level,pch=as.numeric(data$grade),
  col=as.numeric(data$grade))

Obviously if you want other than symbols 1, 2 and 3 in colors black, red 
and green, you can create vectors of symbol numbers and colors:

mysymbols<-c(4,6,19)
mycolors<-c("palevioletred4","mediumorchid3","lightgoldenrod2")

and then index into them:

plot(data$hours,data$level,pch=mysymbols[as.numeric(data$grade)],
  col=mycolors[as.numeric(data$grade)])

Jim