ggplot2 활용 팁

Tidyverse로 배우는 범주형 데이터

Emily Robinson

Instructor

직책 데이터

 

job_titles_by_perc
# A tibble: 16 x 2
   CurrentJobTitleSelect                perc_w_title
   <chr>                                       <dbl>
 1 Business Analyst                          0.0673 
 2 Computer Scientist                        0.0283 
 3 Data Analyst                              0.103  
 4 Data Miner                                0.00997
 5 Data Scientist                            0.206  
 6 DBA/Database Engineer                     0.0158 
Tidyverse로 배우는 범주형 데이터

초기 플롯

ggplot(job_titles_by_perc,
        aes(x = CurrentJobTitleSelect,, y = perc_w_title)) + 
    geom_point() 

x축에 "Current Job Title Select", y축에 "Perc w title"이 표시된 산점도. x축 눈금 텍스트가 겹쳐 읽기 어려우며, y축 기준으로 정렬되지 않음.

Tidyverse로 배우는 범주형 데이터

눈금 레이블 각도 변경하기

ggplot(job_titles_by_perc,
        aes(x = CurrentJobTitleSelect, y = perc_w_title)) + 
    geom_point() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

이전과 동일한 산점도이지만 x축 눈금 텍스트가 수직으로 회전되어 읽기 쉬워짐.

Tidyverse로 배우는 범주형 데이터

fct_reorder() 사용하기

ggplot(job_titles_by_perc, 
   aes(x = fct_reorder(CurrentJobTitleSelect, perc_w_title), 
       y = perc_w_title)) + 
    geom_point() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

이전과 동일한 산점도이지만 y축 값 기준으로 왼쪽에서 오른쪽으로 오름차순 정렬됨.

Tidyverse로 배우는 범주형 데이터

fct_rev() 추가하기

ggplot(job_titles_by_perc, 
        aes(x = fct_rev(fct_reorder(CurrentJobTitleSelect, 
        perc_w_title)), y = perc_w_title)) + 
    geom_point() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1))

이전과 동일한 산점도이지만 y축 값 기준으로 왼쪽에서 오른쪽으로 내림차순 정렬됨.

Tidyverse로 배우는 범주형 데이터

labs() 사용하기

ggplot(job_titles_by_perc, 
        aes(x = fct_rev(fct_reorder(CurrentJobTitleSelect, perc_w_title)),
            y = perc_w_title)) + 
    geom_point() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    labs(x = "Job Title", y = "Percent with title")

이전과 동일한 산점도이지만 x축 레이블은 "Job Title", y축 레이블은 "Percent with title"로 표시됨.

Tidyverse로 배우는 범주형 데이터

% 스케일로 변경하기

ggplot(job_titles_by_perc, 
       aes(x=fct_rev(fct_reorder(CurrentJobTitleSelect,perc_w_title)),
           y=perc_w_title)) + 
    geom_point() + 
    theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
    labs(x = "Job Title", y = "Percent with title") + 
    scale_y_continuous(labels = scales::percent_format())

이전과 동일한 산점도이지만 y축 눈금이 퍼센트로 표시됨. 예: .05가 5%로 표시됨.

Tidyverse로 배우는 범주형 데이터

연습해 봅시다!

Tidyverse로 배우는 범주형 데이터

Preparing Video For Download...