Plot (x, y, col =) not working correctly in R?

I have a problem understanding how the col argument works in the plot.

Look at this first:

plot(collection[,"x"], collection[,"y"], 
     col=rainbow(21300)[order(collection[,"y"])],pch=".")

enter image description here

The color is almost aligned on the Y axis, but not completely.

Now do the following:

plot(collection[,"x"], collection[,"y"],
    col=rainbow(21300)[order(collection[,"x"])],pch=".")

I would expect the colors to be gradient from left to right when I ordered them from the same data as the x axis.

But no, this is complete chaos.

So how does it work?

enter image description here

+4
source share
1 answer

, y ( - ) . col= , , , . order(), , . , . , . , . order() , , rank(). .

y<-cumsum(runif(1000, -.5, 1))
x<-sin(seq(1:length(y))/7+rnorm(length(y),.5,.1))

,

plot(x,y, col=rainbow(length(y))[order(y)], main="order y")
plot(x,y, col=rainbow(length(y))[order(x)], main="order x")

enter image description here

y , y . X , , x, x x.

plot(x,y, col=rainbow(length(y))[rank(y)], main="rank y")
plot(x,y, col=rainbow(length(y))[rank(x)], main="rank x")

enter image description here

, . ,

plot(x[order(y)],y[order(y)], col=rainbow(length(y)), main="data y")
plot(x[order(x)],y[order(x)], col=rainbow(length(y)), main="data x")

enter image description here

+7

Source: https://habr.com/ru/post/1547220/


All Articles