R

GGPLOT2 - geom_segment 사용하기 (그림에서 화살표 넣기)

쿠키아버님 2021. 12. 5. 02:44

그림에 화살표를 넣어야 하는 경우, geom_segment를 사용한다.

입력값은 geom_rect와 비슷하게 시작점과 끝 점에 대한 정보를 넣어준다.

 

library(ggplot2)

a<-runif(10)
b<-runif(10)

df<-data.frame(a, b)

ggplot()+
  geom_line(aes(a, b))+
  geom_segment(aes(x=0.25,
                   xend=0.75, # x 범위 지정
                   y=0.5,
                   yend=0.5), # y  범위 지정
               arrow=arrow()
               )

단방향의 경우 geom_segment 내부의 x, xend, y, yend 값 조정으로 방향 조절이 가능하며,

 

양방향의 경우 arrow() 내에 end='both' 인수를 입력해주면 된다

ggplot()+
  geom_line(aes(a, b))+
  geom_segment(aes(x=0.75,
                   xend=0.25, # x 범위 지정
                   y=0.5,
                   yend=0.5), # y  범위 지정
               arrow=arrow(ends='both'),
               size=2,
               color="red"
               )