Tricks of ggplot2

Categorical Data in the Tidyverse

Emily Robinson

Instructor

Job title data

 

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 
Categorical Data in the Tidyverse

Initial plot

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

A scatterplot with "Current Job Title Select" on the x-axis and "Perc w title" on the y-axis. The x-axis ticks are unreadable because the text runs into each other. The scatterplot is not ordered by the y-axis.

Categorical Data in the Tidyverse

Changing tick labels angle

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

The same scatterplot as before but with the x-axis ticks vertical so they are readable.

Categorical Data in the Tidyverse

Using 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))

The same scatterplot as before but ordered left to right increasing along the y-axis.

Categorical Data in the Tidyverse

Adding 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))

The same scatterplot as before but ordered left to right decreasing along the y-axis.

Categorical Data in the Tidyverse

Using 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")

The same scatterplot as before but the x-axis labeled "Job Title" and the y-axis labeled "Percent with title".

Categorical Data in the Tidyverse

Changing to % scales

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())

The same scatterplot as before but the y-axis ticks now are percentages. For example, .05 is now 5%.

Categorical Data in the Tidyverse

Let's practice!

Categorical Data in the Tidyverse

Preparing Video For Download...