If you have the classes separated in a data frame or a matrix, then you can use matplot
. For example, if we have
dat<-as.data.frame(cbind(c(1,2,5,7),c(2.1,4.2,-0.5,1),c(9,3,6,2.718)))
plot.new()
plot.window(c(0,nrow(dat)),range(dat))
matplot(dat,col=c("red","blue","yellow"),pch=20)
Then you'll get a scatterplot where the first column of dat
is plotted in red, the second in blue, and the third in yellow. Of course, if you want separate x and y values for your color classes, then you can have datx
and daty
, etc.
An alternate approach would be to tack on an extra column specifying what color you want (or keeping an extra vector of colors, filling it iteratively with a for
loop and some if
branches). For example, this will get you the same plot:
dat<-as.data.frame(
cbind(c(1,2,5,7,2.1,4.2,-0.5,1,9,3,6,2.718)
,c(rep("red",4),rep("blue",4),rep("yellow",4))))
dat[,1]=as.numeric(dat[,1]) #This is necessary because
#the second column consisting of strings confuses R
#into thinking that the first column must consist of strings, too
plot(dat[,1],pch=20,col=dat[,2])