user1805343
user1805343

Reputation: 127

Coluring the scatterplot in R

I have two vectors

 A=rnorm(500)
 B=rnorm(500)

And wanted to create a scatterplot and used

   Plot(A,B,cex=0.5,col="grey") ### this creates the base scatterplot

Now, I have two more conditions wherein I have three vectors which is a subset of the original ones:

 C<-subset[A,select=c(1:10,20:30,60:75,90,100) ### to be coloured in blue
 D<-subset[A,select=c(25:60)] ### to be coloured in blue
 E<-subset[B,select=c(100:150,120:125)] ### to be colured in red.

How should I modify the scatterplot to change the color for these vectors C,D,E alone from the original grey colour?? The concept is similar to this:

XY Scatterplot

Upvotes: 0

Views: 582

Answers (1)

ialm
ialm

Reputation: 8717

This is how I interpreted your question:

"Plot the subset of points indicated by the indices C, D, E in the question with different colours".

A <- rnorm(500)
B <- rnorm(500)

## Set the indices, as written in question
Ci <- c(1:10,20:30,60:75,90,100) ### to be coloured in blue
Di <- c(25:60)                   ### to be coloured in blue
Ei <- c(100:150,120:125)         ### to be coloured in red.

## Plot the original scatterplot, then plot over the points of interest with colour
## Use the "points" function from base graphics to plot points on existing plot
## Grab the relevant points from vectors A and B by accessing them at indices 
## Ci, Di, Ei using `[]`
plot(A, B, cex=0.5, col="grey")
points(A[Ci], B[Ci], cex=0.5, col="blue")
points(A[Di], B[Di], cex=0.5, col="blue")
points(A[Ei], B[Ei], cex=0.5, col="red")

Upvotes: 3

Related Questions