티스토리 뷰

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

 

[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    79
2   2   B    77
3   3   C    36
4   4   A    14
5   5   B    75
6   6   C    30

 

기본 그래프

> ggplot(data = bar_df, aes(x = var,
+                           y = value,
+                           fill = value))+
+   geom_bar(stat = 'identity')

 

그래프 1

> ggplot(data = bar_df, aes(x = var,
+                           y = value,
+                           fill = value))+
+   geom_bar(stat = 'identity')+
+   scale_x_discrete(expand = c(0.01, 0.01))

x축은 이산

 

그래프 2

> ggplot(data = bar_df, aes(x = var,
+                           y = value,
+                           fill = value))+
+   geom_bar(stat = 'identity')+
+   scale_x_discrete(expand = c(0.5,0.5))

 

그래프 3

ggplot(data = bar_df, aes(x = var,
                          y = value,
                          fill = value))+
  geom_bar(stat = 'identity')+
  scale_y_continuous(expand = c(0.01,0.01))

y축은 연속이므로

 

그래프 4

ggplot(data = bar_df, aes(x = var,
                          y = value,
                          fill = value))+
  geom_bar(stat = 'identity')+
  scale_y_continuous(expand = c(0.5,0.5))

 

최대-최소값 설정

그래프 1

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value))+
  geom_line(size = 2)

* + theme(axis.text = element(size =20))하면 축의 숫자 크기가 커진다.

 

그래프 2

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value))+
  geom_line(size = 2)+
  scale_x_continuous(limits = c(5,10))

 

그래프 3

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value))+
  geom_line(size = 2)+
  scale_y_continuous(limits = c(20,80))

 

그래프 4

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value))+
  geom_line(size = 2)+
  scale_y_continuous(limits = c(0,200))

 

구간설정

그래프 1

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value))+
  geom_line(size = 2)+
  scale_x_continuous(breaks = c(5, 10),
                     labels = c(5, 10))

눈금을 정확하게 찍거나 내가 원하는 지점에 점을 찍고 싶을때

5와 10 지점에 5와 10을 찍겠다.

 

그래프 2

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value))+
  geom_line(size = 2)+
  scale_y_continuous(breaks = seq(0, 100, 10),
                     labels = seq(0, 100, 10))

그래프 1에서는 손으로 직접 찍어 만들었지만, 수열 생성 함수 seq를 이용하여 만들 수 있다.

breaks = (2:9)*10

labels = (2:9)*10 와 같다.

 

 

 

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

ggplot 텍스트 설정  (0) 2019.08.08
ggplot 요소설정  (0) 2019.08.07
ggplot 색상 설정-3  (0) 2019.08.06
ggplot 색상설정-2  (0) 2019.08.01
ggplot 색상 설정-1  (0) 2019.07.31
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함