N'ya
N'ya

Reputation: 347

Create specific vector of colors

How to create a vector of colors where one certain output receives a different color than the others?

Manually it can be done like in the following example:

pts.7 <- cbind(1:7, 1:7)
mycols <- c("black","black","black","red","black","black","black")
plot(pts.7, col=mycols)

However for bigger data set like the following it doesn't work:

pts.400 <- cbind(runif(400), runif(400))
df <- data.frame(a = 1:400)
pts.400.df <- SpatialPointsDataFrame(pts.400, df)

How could a vector of colors be build that all points plotted get a grayscale color except point$a==158 which should be plotted red?

Upvotes: 2

Views: 3049

Answers (1)

Axeman
Axeman

Reputation: 35187

A trick is to use logical indexing:

plot(pts.400.df, col = c('black', 'red')[(pts.400.df$a == 158) + 1])

Each FALSE will be black, each TRUE will be red.

Upvotes: 1

Related Questions