class: center, middle, inverse, title-slide # Customizing Plot Scales ## Customizing Your Scales in ggplot2 ### Peter Higgins ### 2021-10-28 --- ### Customizing Plot Scales #### There are Multiple Functions to Customize the Default Scales of Your ggplot2 plot. - The {ggplot2} package supplies default scales and labels for your plots so that you can get a reasonable result quickly. - However, you can customize these scales by adding (+) lines to your plot code that have the format of scales_(x|y|color|shape)_description(). - Let's see an example --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r *prostate ``` ] .panel2-scales1-auto[ ``` # A tibble: 316 × 20 rbc_age_group median_rbc_age age aa fam_hx p_vol t_vol t_stage b_gs <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> 1 3 25 72.1 0 0 54 3 1 3 2 3 25 73.6 0 0 43.2 3 2 2 3 3 25 67.5 0 0 103. 1 1 3 4 2 15 65.8 0 0 46 1 1 1 5 2 15 63.2 0 0 60 2 1 2 6 3 25 65.4 0 0 45.9 2 1 1 7 3 25 65.5 1 0 42.6 2 1 1 8 1 10 67.1 0 0 40.7 3 1 1 9 1 10 63.9 0 0 45 2 1 1 10 2 15 63 1 0 67.6 2 1 2 # … with 306 more rows, and 11 more variables: bn <dbl>, organ_confined <dbl>, # preop_psa <dbl>, preop_therapy <dbl>, units <dbl>, s_gs <dbl>, # any_adj_therapy <dbl>, adj_rad_therapy <dbl>, recurrence <dbl>, # censor <dbl>, time_to_recurrence <dbl> ``` ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot * ggplot() ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_02_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + * aes(x = factor(recurrence)) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_03_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + * aes(y = p_vol) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_04_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + aes(y = p_vol) + * geom_jitter() ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_05_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + aes(y = p_vol) + geom_jitter() + * aes(color = factor(recurrence)) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_06_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + aes(y = p_vol) + geom_jitter() + aes(color = factor(recurrence)) + # expand x margins * scale_x_discrete(expand = expansion(mult=1.2)) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_07_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + aes(y = p_vol) + geom_jitter() + aes(color = factor(recurrence)) + # expand x margins scale_x_discrete(expand = expansion(mult=1.2)) + # set y limits * scale_y_continuous(limits= c(0,90)) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_08_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + aes(y = p_vol) + geom_jitter() + aes(color = factor(recurrence)) + # expand x margins scale_x_discrete(expand = expansion(mult=1.2)) + # set y limits scale_y_continuous(limits= c(0,90)) + # format color legend * scale_color_discrete(name = "Recurrence", * labels = c("No Recurrence", "Recurrence")) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_09_output-1.png" width="432" /> ] --- count: false Scales 1: x,y, and color .panel1-scales1-auto[ ```r prostate %>% # slow plot ggplot() + aes(x = factor(recurrence)) + aes(y = p_vol) + geom_jitter() + aes(color = factor(recurrence)) + # expand x margins scale_x_discrete(expand = expansion(mult=1.2)) + # set y limits scale_y_continuous(limits= c(0,90)) + # format color legend scale_color_discrete(name = "Recurrence", labels = c("No Recurrence", "Recurrence")) ``` ] .panel2-scales1-auto[ <img src="scales_microflip_files/figure-html/scales1_auto_10_output-1.png" width="432" /> ] <style> .panel1-scales1-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-scales1-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-scales1-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center # End of This Flipbook ## Let's Learn How to Do This!