เทคนิคของ 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...