2016-10-24 2 views
0

R에서 플롯하고 기본 불평등 조건을 만족하면 쌍선 점 데이터에 수평선을 추가하는 방법을 배우고 있습니다. 예를 들어, 주어진 입력 세트에 대해 3 세트의 출력 값을가집니다. 쌍의 점에 대한 수평선을 플롯 R

enter image description here

input <- c(1,2,3,4) 
a <- c(1,2,3,4) 
b <- c(2,3,4,5) 
c <- c(5,6,7,3) 
plot(a, input, xlim=c(min(a,b,c), max(a,b,c)), pch=16, col=rgb(0,0,0,0.5), xlab='output', ylab='input') 
points(b, input, pch=16, col=rgb(1,0,0,0.5)) 
points(c, input, pch=16, col=rgb(0,1,0,0.5)) 

그러나, I는 출력 값의 페어의 차이를 설명 할. 따라서 산점도 대신 각 입력에 대해 검정색 (a) 및 빨간색 (b) 도트를 연결하는 선 (노란색)을 입력하십시오. i b[i] > a[i] 인 경우. 마찬가지로, c[i] > b[i] 인 경우 빨간색 (b) 및 녹색 (c) 점을 연결하는 다른 선 (파란색)이 있습니다.

어떻게하면됩니까?

답변

1

논리 판단, ifsegments()을 사용하여 수행 할 수 있습니다. sapply(input, function(...))은 각 input에 대해 function을 적용합니다.

plot(a, input, xlim=c(min(a,b,c), max(a,b,c)), pch=16, col=rgb(0,0,0,0.5), xlab='output', ylab='input') 
points(b, input, pch=16, col=rgb(1,0,0,0.5)) 
points(c, input, pch=16, col=rgb(0,1,0,0.5)) 

sapply(input, function(x, a, b, c) { 
    if(b[x] > a[x]) segments(a[x], x, b[x], x, col = "yellow3") 
    if(c[x] > b[x]) segments(b[x], x, c[x], x, col = "blue") 
    }, a = a, b = b, c = c) 

enter image description here

1

단순히이 시도 :

indices <- which(b > a) 
segments(a[indices], input[indices], b[indices], input[indices], col='yellow') 
indices <- which(c > b) 
segments(b[indices], input[indices], c[indices], input[indices], col='blue') 

enter image description here