2017-10-24 15 views
1

R의 트렁크 섹션에서 링의 모양을 시뮬레이션하려고하지만 실제 모양에 접근하고 싶을 때마다 더 어려워집니다. 4 개의 반경 측정으로 시작했는데 좋은 해결책이 있습니다 (see here). 그러나, 지금은 네 반경보다하지만 다른 각도에서 더 플롯, 그리고 내가 한이 스케치 같은 반지 시뮬레이션 라인이 포인트를 연결하려면 : enter image description here다른 각도로 정렬 된 점을 그려서 선으로 연결하는 방법은 무엇입니까?

내 첫 번째 방법은 데이터의 매트릭스를 회전하는 것이 었습니다 , 그러나 나는 모든 반경이 같은 위치 (0,0)에서 시작되도록 만들 수는 없었다. 또한 성공하지 못한 채 축을 평가하려고했습니다.

그래서 나는 그것을 할 방향을 물어보고 각 반지의 면적을 계산하고 싶습니다.
아무 도움도 환영합니다.

답변

2

herespline.poly 기능을 사용하고 있습니다.

spline.poly

spline.poly <- function(xy, vertices, k=3, ...) { 
    # Assert: xy is an n by 2 matrix with n >= k. 

    # Wrap k vertices around each end. 
    n <- dim(xy)[1] 
    if (k >= 1) { 
     data <- rbind(xy[(n-k+1):n,], xy, xy[1:k, ]) 
    } else { 
     data <- xy 
    } 

    # Spline the x and y coordinates. 
    data.spline <- spline(1:(n+2*k), data[,1], n=vertices, ...) 
    x <- data.spline$x 
    x1 <- data.spline$y 
    x2 <- spline(1:(n+2*k), data[,2], n=vertices, ...)$y 

    # Retain only the middle part. 
    cbind(x1, x2)[k < x & x <= n+k, ] 
} 

DATA

df = data.frame(A = c(1, 4, 5, 8, 10), 
       B = c(1, 3, 7, 9, 10), 
       C = c(2, 6, 8, 9, 10), 
       D = c(1, 3, 4, 7, 9), 
       E = c(1, 2, 3, 4, 5)) 

DRAW

#Calculate angles based on number of columns 
angles = 0:(NCOL(df) - 1) * 2*pi/NCOL(df) 

#Calculate x and y corresponding to each radial distance 
toplot = lapply(1:NCOL(df), function(i){ 
    data.frame(x = df[,i]*cos(angles[i]), 
       y = df[,i]*sin(angles[i])) 
}) 

#Split toplot and merge back together the same rows 
toplot2 = lapply(toplot, function(x) data.frame(x, ID = sequence(NROW(x)))) 
toplot2 = do.call(rbind, toplot2) 
toplot2 = split(toplot2, toplot2$ID) 

#Create empty plot  
graphics.off() 
plot(do.call(rbind, toplot), type = "n", axes = FALSE, ann = FALSE, asp = 1) 

#Allow drawing outside the plot region just in case 
par(xpd = TRUE) 

#Draw polygons 
lapply(toplot2, function(a){ 
    polygon(spline.poly(xy = cbind(a$x, a$y), vertices = 100, k = 3)) 
}) 

#Draw points  
lapply(toplot, function(a){ 
    points(a) 
}) 

#Draw radial lines  
lapply(toplot, function(a){ 
    lines(a) 
}) 

012,343,572,

AREA

area_data = lapply(toplot2, function(a){ 
    spline.poly(xy = cbind(a$x, a$y), vertices = 100, k = 3) 
}) 
library(geometry) 
lapply(area_data, function(P) polyarea(P[,1], P[,2])) 
#$`1` 
#[1] 4.35568 

#$`2` 
#[1] 38.46985 

#$`3` 
#[1] 96.41331 

#$`4` 
#[1] 174.1584 

#$`5` 
#[1] 240.5837 
+0

미안 해요,하지만 난 각 경선에 레이블을 넣어 어떻게 알고 싶습니다? –