0

I was wondering that how I can change the color of certain data points in a scatter plot in R?

So, for example, I want the data point in the 7th, 8th, and 15th row to have a red color and the rest have a black color.

Thanks a lot for your help

Lucia
  • 901
  • 1
  • 11
  • 16
  • 3
    This is pretty easy to do. We can even show you, if you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – gung - Reinstate Monica Nov 17 '13 at 19:11
  • Please read [about Stackoverflow](http://stackoverflow.com/about) and [what to ask](http://stackoverflow.com/help/on-topic). As you will find in these two links, you should "show your work", and "Questions asking for code must include attempted solutions, [and] why they didn't work". – Henrik Nov 17 '13 at 19:29

2 Answers2

1

The following will work given that you data is in a data.frame called "dat".

cols <- rep('black', nrow(dat))
cols[c(7, 8, 15)] <- 'red'

In your plot command set col = cols.

John
  • 23,360
  • 7
  • 57
  • 83
1

How about like this?

randomdata<-    
data.frame(x=1:20,y=rnorm(20,8,1),col=as.character("black"),stringsAsFactors=FALSE)
randomdata[c(7,8,15),"col"]<-"red"
plot(randomdata$x,randomdata$y,col=randomdata$col)
Troy
  • 8,581
  • 29
  • 32