티스토리 뷰

beginner/R 시각화 기초

ggplot 범례

johh 2019. 8. 13. 22:59

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

 

[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    56
2   2   B    79
3   3   C    74
4   4   A     9
5   5   B    10
6   6   C    80

 

기본 그래프

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

 

범례(legend) - 위치조정

그래프 1

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value,
           shape = var)) +
  geom_point(size = 10) + 
  theme(legend.position = 'top')

 

그래프 2

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value,
           shape = var)) +
  geom_point(size = 10) +
  theme(legend.position = 'bottom')

 

범례(legend) - 텍스트

그래프 3

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value,
           shape = var)) +
  geom_point(size = 10) + 
  theme(legend.text = element_text(size = 15))

 

그래프 4

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value,
           shape = var)) +
  geom_point(size = 10) +
  theme(legend.title = element_text(size = 20))

 

범례(legend) 

그래프 5

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value,
           shape = var)) +
  geom_point(size = 10) +
  theme(legend.key.size = unit(x = 0.5, units = 'cm'))

 

그래프 6

ggplot(data = bar_df,
       aes(x = obs,
           y = value,
           color = value,
           shape = var)) +
  geom_point(size = 10) +
  theme(legend.key.width = unit(x = 2, units = 'cm'))

 

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

ggplot 덧그리기  (0) 2019.08.12
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
글 보관함