[R] Scatterplot, Color by Grade Category

Jim Lemon jim at bitwrit.com.au
Wed Feb 20 10:23:42 CET 2013


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



More information about the R-help mailing list