简体   繁体   English

改变r中某些数据点的颜色

[英]change the color of certain data points in r

I was wondering that how I can change the color of certain data points in a scatter plot in R? 我想知道如何在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. 因此,例如,我希望第7行,第8行和第15行中的数据点为红色,其余部分为黑色。

Thanks a lot for your help 非常感谢你的帮助

The following will work given that you data is in a data.frame called "dat". 鉴于您的数据位于名为“dat”的data.frame ,以下内容将起作用。

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

In your plot command set col = cols . 在你的情节命令集col = cols

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM