2014-07-13 7 views
3

post의 답변과 비슷하지만 geom_path() 대신 geom_segment()을 사용하고 싶습니다. 이제 내 줄에 화살표를 추가하고 싶습니다.r - coord_polar로 geom_segment 사용하기

설정 :

enter image description here

example <- data.frame(r=c(5,4,3),theta=c(0.9,1.1,0.6)) 

is.linear.polar2 <- function(x) TRUE 
coord_polar2 <- coord_polar(theta="y", start = 3/2*pi, direction=-1) 
class(coord_polar2) <- c("polar2", class(coord_polar2)) 

myplot <- ggplot(example, aes(r, theta)) + geom_point(size=3.5) + 
    scale_x_continuous(breaks=seq(0,max(example$r)), lim=c(0, max(example$r))) + 
    scale_y_continuous(breaks=round(seq(0, 2*pi, by=pi/4),2), expand=c(0,0), lim=c(0,2*pi)) + 
    geom_text(aes(label=rownames(example)), size=4.4, hjust=0.5, vjust=-1) 

myplot + coord_polar2 + geom_path() 
내가 다음과 같습니다 플롯을 원하지만 시퀀스에서 다음 포인트의 방향 화살표. 이 솔루션도 hackier 첫 번째 질문에 대한 대답보다는이다,

myplot + coord_polar2 + 
geom_segment(data=example,aes(x=r, y=theta, xend=c(tail(r, n=-1), NA), 
           yend=c(tail(theta, n=-1), NA)), 
      arrow=arrow(length=unit(0.3,"cm"), type="closed")) 

enter image description here

myplot + coord_polar(theta="y", start = 3/2*pi, direction=-1) + 
geom_segment(data=example,aes(x=r, y=theta, xend=c(tail(r, n=-1), NA), 
           yend=c(tail(theta, n=-1), NA)), 
      arrow=arrow(length=unit(0.3,"cm"), type="closed")) 

enter image description here

답변

3

음 :

이 내 시도이다. geom_segment을 사용하는 대신 geom_path을 사용하지만 마지막 화살표 만 생성하므로 group 미적을 추가하여 세그먼트를 하나씩 연결합니다. 이것은 우리의 원래 데이터 프레임이 약간 수정되어야 의미 : 당신의 아이디어에 대한

tweak_data <- function(df) 
{ 
    if (nrow(df) == 1) return(df) 
    idx_x2 <- c(1, rep(2:(nrow(df) - 1), each=2), nrow(df)) 
    gr <- rep(seq.int(nrow(df) - 1), each=2) 
    df_res <- data.frame(r = df$r[idx_x2], theta = df$theta[idx_x2], 
         label = rownames(df)[idx_x2], group = gr) 
    df_res 
} 

example_tw <- tweak_data(example) 
myplot2 <- ggplot(example2, aes(r, theta)) + geom_point(size=3.5) + 
    scale_x_continuous(breaks=seq(0,max(example$r)), lim=c(0, max(example$r))) + 
    scale_y_continuous(breaks=round(seq(0, 2*pi, by=pi/4), 2), 
        expand=c(0, 0), lim=c(0, 2*pi)) + 
    geom_text(aes(label=label), size=4.4, hjust=0.5, vjust=-1) 
myplot2 + coord_polar2 + 
    geom_path(aes(group=group), arrow=arrow(length=unit(0.3,"cm"), type="closed")) 

enter image description here

+0

감사합니다! 이것은 내가 원했던 그래프입니다 :) –

+0

당신은 환영합니다! – tonytonov

+0

슬프게도 coord_polar2 해킹이 더 이상 작동하지 않습니다 .. – geotheory

관련 문제