0
votes

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

2
This is pretty easy to do. We can even show you, if you provide a reproducible example.gung - Reinstate Monica
Please read about Stackoverflow and what to ask. 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

2 Answers

1
votes

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)
1
votes

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.