티스토리 뷰

김승욱님 강의를 듣고 작성하였습니다.

 

[R을 R려줘] R 시각화 기초 - 인프런

R 문법 기초에 이어서 진행되는 시각화 강좌 입니다. R의 강력한 시각화 패키지인 ggplot2를 집중적으로 배웁니다. 입문 데이터 분석 프로그래밍 언어 R 온라인 강의

www.inflearn.com

 

가로-세로선 추가

데이터 준비

> bar_df = data.frame( obs = 1:10,
+                      var = rep(c('A', 'B', 'C'), length.out = 10),
+                      value = sample(1:100, size=10),
+                      stringsAsFactors = FALSE)
> head(bar_df)
  obs var value
1   1   A    60
2   2   B    33
3   3   C     1
4   4   A    53
5   5   B    55
6   6   C    16

 

기본 그래프

ggplot(data = bar_df, aes(x = obs,
                          y = value,
                          color = value))+
  geom_point(size=10)

 

그래프(가로선 추가)

ggplot(data = bar_df, aes(x = obs,
                          y = value,
                          color = value))+
  geom_point(size = 10)+
  geom_hline(yintercept = 10, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 1) +
  geom_hline(yintercept = 20, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 2) +
  geom_hline(yintercept = 30, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 3) +
  geom_hline(yintercept = 40, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 4) +
  geom_hline(yintercept = 50, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 5) +
  geom_hline(yintercept = 60, size = 2, color = '#44FF44', alpha = 0.7, linetype = 6) +
  geom_hline(yintercept = 70, size = 2, color = '#44FF44', alpha = 0.7, linetype = 7) +
  geom_hline(yintercept = 80, size = 2, color = '#44FF44', alpha = 0.7, linetype = 8) +
  geom_hline(yintercept = 90, size = 2, color = '#44FF44', alpha = 0.7, linetype = 9) +
  geom_hline(yintercept = 100, size = 2, color = '#44FF44', alpha = 0.7, linetype = 10)

 

ggplot(data = bar_df, aes(x = obs,
                          y = value,
                          color = value))+
  geom_hline(yintercept = (1:3)*20, size = 3, color='#FFFFFF')+
  geom_point(size=10)

 

그래프(세로선 추가)

ggplot(data = bar_df, aes(x = obs,
                          y = value,
                          color = value))+
  geom_point(size = 10)+
  geom_vline(xintercept = 10, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 1) +
  geom_vline(xintercept = 20, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 2) +
  geom_vline(xintercept = 30, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 3) +
  geom_vline(xintercept = 40, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 4) +
  geom_vline(xintercept = 50, size = 2, color = '#FFAACC', alpha = 0.7, linetype = 5) +
  geom_vline(xintercept = 60, size = 2, color = '#44FF44', alpha = 0.7, linetype = 6) +
  geom_vline(xintercept = 70, size = 2, color = '#44FF44', alpha = 0.7, linetype = 7) +
  geom_vline(xintercept = 80, size = 2, color = '#44FF44', alpha = 0.7, linetype = 8) +
  geom_vline(xintercept = 90, size = 2, color = '#44FF44', alpha = 0.7, linetype = 9) +
  geom_vline(xintercept = 100, size = 2, color = '#44FF44', alpha = 0.7, linetype = 10)

뒤에 보조선 지워버리고 내가 원하는 색의 보조선을 그을때, 월별로 구분하는 선을 그을 때.

 

Annotate() 함수

텍스트(geom = 'text')

ggplot() +
  geom_point(aes(x = 1: 10,
                 y = 1:10),
             size = 3) +
  annotate(geom = 'text',
           label = 'Adf',
           size = 10,
           x = 7,
           y = 3)

글자의 정 중앙이 (7, 3). 내가 원하는 위치에 놓고 싶을때 미세조정 해야해서 힘들어.

 

사격형(geom = 'rect')

ggplot() +
  geom_point(aes(x = 1:10,
                 y = 1:10),
             size = 3) +
  annotate(geom = 'rect',
           fill = '#FFA500',
           xmin = 5, xmax = 7,
           ymin = 2, ymax = 3)

 

선분(geom = 'segment')

ggplot() +
  geom_point(aes(x = 1:10,
                 y = 1:10),
             size = 3) +
  annotate(geom = 'segment',
           color = '#FF0000',
           size = 2,
           x = 5, xend = 7,
           y = 2, yend = 3)

 

값 범위(geom = 'text')

ggplot() +
  geom_point(aes(x = 1:10,
                 y = 1:10),
             size = 3) +
  annotate(geom = 'pointrange',
           color = '#FF0000',
           size = 2,
           x = 7, y = 3,
           ymin = 2, ymax = 5)

 

'beginner > R 시각화 기초' 카테고리의 다른 글

ggplot 범례  (0) 2019.08.13
ggplot 텍스트 설정  (0) 2019.08.08
ggplot 요소설정  (0) 2019.08.07
ggplot 축 설정  (0) 2019.08.07
ggplot 색상 설정-3  (0) 2019.08.06
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함