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...