young
is this it
young
전체 방문자
오늘
어제
  • 분류 전체보기 (143)
    • 웹_프론트엔드 (1)
      • 로드맵 챌린지 (73)
      • Svelte (2)
      • React (6)
      • JavaScript (8)
      • TypeScript (2)
      • HTML+CSS (5)
    • 웹_백엔드 (0)
      • Django (0)
    • 빅데이터 (33)
      • R (30)
      • Python (2)
    • 기타 (11)
      • git (3)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • ssl
  • 인증
  • bem
  • rstudio지도정보
  • owasp
  • ggmap()
  • 구글맵api
  • css후처리기
  • 버전관리
  • ggplot
  • 암호화
  • 대칭키
  • 공개키
  • vcs
  • 태스크러너
  • Regex
  • 웹보안
  • 보안취약점
  • form
  • css네이밍

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
young

is this it

[R] 고급 시각화 분석 - ggplot() 함수 사용 예시
빅데이터/R

[R] 고급 시각화 분석 - ggplot() 함수 사용 예시

2019. 12. 9. 21:34
반응형
    • 세로 막대 차트

1
2
3
4
ggplot(df, aes(x = month, y=rain)) + # aes 함수는 ggplot() 안에 써도, 또는 geom_bar()안에 써도 상관 없음
  geom_bar(stat = "identity", #데이터셋 안에 포함되지 않은 변수를 쓸때는 stat = “bin”을 입력
           width = 0.7,
           fill = "steelblue")
Colored by Color Scripter

    • 가로 막대 차트

1
2
3
4
5
6
7
8
9
10
ggplot(df, aes(x = month, y = rain)) +
  geom_bar(stat = "identity",
           width = 0.7,
           fill = "steelblue") +
  ggtitle ("월별 강수량") + # 차트 제목
  theme(plot.title = element_text(size = 25,
                                face = "bold",
                                color = 'red')) +
  labs(x = '월', y = '강수량') + 
  coord_flip() #가로-세로 flip

 

    • 히스토그램 Histogram

1
2
3
4
ggplot(iris, aes(x = Sepal.Width, fill = Species, # fill은 막대를 채우는 색
                 color = Species)) + #color는 경계선의 색
  geom_histogram(binwidth = 0.1, position = "dodge") + # dodge : 막대 이어 붙이기 
  theme(legend.position = 'bottom') # legend.position :범례 위치

 

    • 산점도 Scatter Chart

1
2
3
4
5
6
7
8
9
ggplot ( data = iris, mapping = aes (x = Petal.Length,
                                     y = Petal.Width,
                                     color = Species,
                                     shape = Species )) +
  geom_point(size = 3) +
  ggtitle("꽃잎의 길이와 폭") +
  theme(plot.title = element_text (size = 25,
                                  face = 'bold',
                                   color = rgb( 128,0,128,maxColorValue = 255)))

 

  • 상자 그래프 Box Plot

     

    1
    2
    3
    4
    5
    6
    7
    ggplot (data = iris, mapping = aes(y=Petal.Length)) +
      geom_boxplot(fill = 'navy',
                   color = 'red')
     
    ggplot (data = iris, mapping = aes(y=Petal.Length,
                                       fill = Species)) +
      geom_boxplot()
    Colored by Color Scripter

 

    • 선 그래프

1
2
3
4
5
year <- 1937:1960
cnt <- as.vector(airmiles)
df <- data.frame(year, cnt)
ggplot(df, aes(x = year, y = cnt)) +
  geom_line(col = "navy")

 

 

+ 사선 & 평행선

1
2
3
4
5
ggplot(economics, aes(x = date, y = psavert)) +
  geom_line() + 
  geom_abline(intercept = 12.18671, # 사선, intercept : y 절편값
              slope = -0.0005333) + # slope : 기울기
  geom_hline(yintercept = mean(economics$psavert)) # 평행선

 

+ 수직선

1
2
3
ggplot(economics, aes(x = date, y=psavert)) +
  geom_line() +
  geom_vline(xintercept = x_inter) # 수직선 vertical line

 

 

+ 텍스트 추가 (산점도 배경)

1
2
3
ggplot(economics, aes(x = date, y=psavert)) +
  geom_line() +
  geom_vline(xintercept = x_inter) # 수직선 vertical line

 

+ 강조 영역 및 화살표 추가 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
ggplot(mtcars, aes(x = wt, y = mpg)) +
  geom_point() +
  annotate('rect',
           xmin = 3, # 좌표 지정
           xmax = 4,
           ymin = 12,
           ymax = 21,
           alpha = 0.5, #투명도 지정
           fill = 'red') +
  annotate('segment', x= 2.5, xend = 3.7, # 화살표 넣어 강조하기
           y = 10, yend=17, color = 'blue',
           arrow = arrow(60) ) + # 화살표 각도
  annotate('text', x= 2.5, y = 10, # 화살표 이름
           label = 'point')

 

 

 

 

 

 

반응형

'빅데이터 > R' 카테고리의 다른 글

[R] treemap() 함수 사용 예시  (0) 2019.12.10
[R] RStudio에서 read.csv 후 한글 깨짐 현상 해결 방법 (영문 OS)  (0) 2019.12.10
[R] 데이터 조작 - dplyr 패키지 활용하기  (0) 2019.12.08
[R] 데이터 전처리 - 코딩 변경  (0) 2019.12.08
[R] 데이터 전처리 - 결측치 처리 (제거 및 대체)  (0) 2019.12.08
    '빅데이터/R' 카테고리의 다른 글
    • [R] treemap() 함수 사용 예시
    • [R] RStudio에서 read.csv 후 한글 깨짐 현상 해결 방법 (영문 OS)
    • [R] 데이터 조작 - dplyr 패키지 활용하기
    • [R] 데이터 전처리 - 코딩 변경
    young
    young

    티스토리툴바