class: center, middle, inverse, title-slide # Scales ### Gina Reynolds, July 2019 --- --- <!-- # For teachers --> <!-- This is a collection of thoughts about how to teach ggplot2, and any other grammar of graphics type data visualization system. --> <!-- It is tempting to rush to build a publication-ready plot right out of the gate. --> <!-- But maybe we should resist this temptation. Maybe we should "hang out" in each of the component zones for a bit. Explore. Get comfy in a zone. Then move on. --> --- ## Each aesthetic (color, x-position, size), that does representation for us has a scale associated with it.. --- class: split-40 count: false .left-panel-scales-auto[ ```r *ggplot(data = gapminder_2002) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + * aes(x = gdpPercap) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + * scale_x_continuous(trans = "log10") ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + * aes(y = lifeExp) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + * scale_y_continuous(breaks = c(50,70)) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + * geom_point() ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + * aes(color = continent) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + * scale_color_viridis_d() ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + scale_color_viridis_d() + * aes(size = gdpPercap) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + scale_color_viridis_d() + aes(size = gdpPercap) + * scale_size_continuous(limits = c(0, 400000)) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + scale_color_viridis_d() + aes(size = gdpPercap) + scale_size_continuous(limits = c(0, 400000)) + * aes(shape = continent) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + scale_color_viridis_d() + aes(size = gdpPercap) + scale_size_continuous(limits = c(0, 400000)) + aes(shape = continent) + * scale_shape_discrete(solid = F) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + scale_color_viridis_d() + aes(size = gdpPercap) + scale_size_continuous(limits = c(0, 400000)) + aes(shape = continent) + scale_shape_discrete(solid = F) + * aes(alpha = gdpPercap) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_13_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scales-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + scale_x_continuous(trans = "log10") + aes(y = lifeExp) + scale_y_continuous(breaks = c(50,70)) + geom_point() + aes(color = continent) + scale_color_viridis_d() + aes(size = gdpPercap) + scale_size_continuous(limits = c(0, 400000)) + aes(shape = continent) + scale_shape_discrete(solid = F) + aes(alpha = gdpPercap) + * scale_alpha(range = c(.4, .9)) ``` ] .right-panel-scales-auto[ <img src="scales_files/figure-html/scales_auto_14_output-1.png" width="100%" /> ] <style> .left-panel-scales-auto { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-scales-auto { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-scales-auto { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- # Concepts - breaks - *values* to be labeled along aesthetic scales - break labels - i.e. the exact text of the labels) - *value* limits of variable representation - transformations - range of *aesthetic* values --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + scale_x_continuous() ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_x_continuous(breaks = c(1500, 15431)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_y_continuous(breaks = c(50, 76, 81.3)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_color_continuous(breaks = c(1500, 15431)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_alpha_continuous(breaks = c(50, 76, 81.3)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_x_continuous(breaks = c(1000, 1500), labels = c("1k", "15k")) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_y_continuous(labels = c("40y", "50y", "60y","70y", "80y")) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_color_continuous(breaks = c(1000, 1500), labels = c("1k", "15k")) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_color_binned() ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_alpha_continuous(labels = c("40y", "50y", "60y","70y", "80y")) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_x_continuous(limits = c(-1000,100000)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_y_continuous(limits = c(30,100)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_color_continuous(limits = c(-1000,100000)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_13_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_alpha_continuous(limits = c(30,100)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_14_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_x_continuous(trans = "log10") ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_15_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_y_continuous(trans = "reverse") ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_16_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_color_continuous(trans = "log10") ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_17_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_alpha_continuous(trans = "reverse") ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_18_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_x_continuous(expand = c(1,2)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_19_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_y_continuous(expand = c(.1,.1)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_20_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_color_continuous(expand = c(1,2)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_21_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-scale_options-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + aes(color = gdpPercap) + aes(alpha = lifeExp) + geom_point() + * scale_alpha_continuous(expand = c(1,2)) ``` ] .right-panel-scale_options-rotate[ <img src="scales_files/figure-html/scale_options_rotate_22_output-1.png" width="100%" /> ] <style> .left-panel-scale_options-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-scale_options-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-scale_options-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Scales There are a variety of scaling alternatives for the x position aesthetic. The most commonly use have convenience functions --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + scale_x_continuous(trans = "hms") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "sqrt") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "log10") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_log10() # convenience ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "log1p") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "log2") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "pseudo_log") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "reciprocal") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "reverse") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_reverse() # convenience ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(trans = "sqrt") ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_trans-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_sqrt() # convenience ``` ] .right-panel-x_trans-rotate[ <img src="scales_files/figure-html/x_trans_rotate_12_output-1.png" width="100%" /> ] <style> .left-panel-x_trans-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-x_trans-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-x_trans-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + scale_x_continuous(breaks = NULL) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(breaks = 1:3*10000) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(breaks = c(1000, 7090)) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(breaks = (1:10)^4) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_dollar()) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_percent()) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_pvalue()) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_comma()) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_math()) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_number_si(unit = "g")) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_break-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * scale_x_continuous(labels = scales::label_number_si()) ``` ] .right-panel-x_break-rotate[ <img src="scales_files/figure-html/x_break_rotate_11_output-1.png" width="100%" /> ] <style> .left-panel-x_break-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-x_break-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-x_break-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-x_scales-user[ ```r *ggplot(data = gapminder_2002) + * aes(x = gdpPercap) + * aes(y = lifeExp) + * geom_point() # this much is familiar ``` ] .right-panel-x_scales-user[ <img src="scales_files/figure-html/x_scales_user_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_scales-user[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + # this much is familiar * scale_x_log10() # overrides default ``` ] .right-panel-x_scales-user[ <img src="scales_files/figure-html/x_scales_user_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_scales-user[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + # this much is familiar scale_x_log10() + # overrides default * scale_x_reverse() # overrides log10 ``` ] .right-panel-x_scales-user[ <img src="scales_files/figure-html/x_scales_user_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-x_scales-user[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + # this much is familiar scale_x_log10() + # overrides default scale_x_reverse() + # overrides log10 * scale_x_sqrt() # overrides reverse ``` ] .right-panel-x_scales-user[ <img src="scales_files/figure-html/x_scales_user_4_output-1.png" width="100%" /> ] <style> .left-panel-x_scales-user { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-x_scales-user { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-x_scales-user { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- ### More aesthetic scale modifications *Note that using multiple scales associated with a single aesthetic is not typical. We are just doing it here to expose you quickly to a variety of possibilities. ggplot2 uses whatever scale specification last declared in our examples.* Analogously, we can use scale\_\*\_\* statements to modify other aesthetic scales. Here are some examples <!-- I think people find it easy to think about x and y position and adjusting those scales, and related adjustment like labs() and scale_*, but we teachers need to help extend the logic to the other aesthetics. --> - scale_x_log10() - scale_y_reverse(limits = c(100, 0)) - scale_color_viridis_d() - scale_shape_discrete(solid = F) - scale_size_continuous(breaks = c(100, 1000, 10000, 100000)) - scale_alpha(range = c(.5, 1)) # full transparency is 0 Let's tour several of those to get a taste. --- ## Questions: <!-- to ponder --> - What does the parameter "breaks" seem to control? - What are some additional color scales that can be used? Use auto complete in RStudio. (i.e. type "scale_color" and then "tab" to see your options) - What does "d" stand for in the scale_color_viridis_d()? Use the help pane. - While you are in the help pane, see how would you set missing values (NA) to "black". --- class: center, middle, inverse # When categorial x-axis scales get messy --- class: split-40 count: false .left-panel-messy_axis_scales-rotate[ ```r gapminder %>% filter(year == 2002, continent == "Americas") %>% ggplot() + aes(x = country) + aes(y = lifeExp) + geom_col() + scale_x_discrete(guide = guide_axis(check.overlap = TRUE)) ``` ] .right-panel-messy_axis_scales-rotate[ <img src="scales_files/figure-html/messy_axis_scales_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-messy_axis_scales-rotate[ ```r gapminder %>% filter(year == 2002, continent == "Americas") %>% ggplot() + aes(x = country) + aes(y = lifeExp) + geom_col() + * scale_x_discrete(guide = guide_axis(n.dodge = 2)) ``` ] .right-panel-messy_axis_scales-rotate[ <img src="scales_files/figure-html/messy_axis_scales_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-messy_axis_scales-rotate[ ```r gapminder %>% filter(year == 2002, continent == "Americas") %>% ggplot() + aes(x = country) + aes(y = lifeExp) + geom_col() + * scale_x_discrete(guide = guide_axis(n.dodge = 3)) ``` ] .right-panel-messy_axis_scales-rotate[ <img src="scales_files/figure-html/messy_axis_scales_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-messy_axis_scales-rotate[ ```r gapminder %>% filter(year == 2002, continent == "Americas") %>% ggplot() + aes(x = country) + aes(y = lifeExp) + geom_col() + * theme(axis.text.x = element_text(angle = -45)) ``` ] .right-panel-messy_axis_scales-rotate[ <img src="scales_files/figure-html/messy_axis_scales_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-messy_axis_scales-rotate[ ```r gapminder %>% filter(year == 2002, continent == "Americas") %>% ggplot() + aes(x = country) + aes(y = lifeExp) + geom_col() + * theme(axis.text.x = element_text(angle = -45, hjust = 0)) ``` ] .right-panel-messy_axis_scales-rotate[ <img src="scales_files/figure-html/messy_axis_scales_rotate_5_output-1.png" width="100%" /> ] <style> .left-panel-messy_axis_scales-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-messy_axis_scales-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-messy_axis_scales-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: center, middle, inverse # Color scales --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + scale_color_viridis_c(option = "cividis") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(option = "magma") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(option = "plasma") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(option = "inferno") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(option = "viridis") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(direction = -1) ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(begin = .2, end = .5) ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(breaks = 76, label = "76 years") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_viridis_c(guide = F) ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_continuous(type = "gradient") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_continuous(type = "viridis") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_gradient(low = "blue", high = "green") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_c-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = lifeExp) + * scale_color_gradient2(low = "blue", mid = "red", high = "green") ``` ] .right-panel-viridis_c-rotate[ <img src="scales_files/figure-html/viridis_c_rotate_13_output-1.png" width="100%" /> ] <style> .left-panel-viridis_c-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-viridis_c-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-viridis_c-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + scale_color_viridis_d(option = "viridis") ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(option = "magma") ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(option = "cividis") ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(option = "plasma") ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(option = "inferno") ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d() ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(direction = -1) ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(breaks = c("Asia", "Europe", "Oceania", "Africa", "Americas")) ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(breaks = c("Asia")) ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(breaks = c("Asia"), labels = c("A continent")) ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(labels = c("AFRICA", "EUROPE", "OEANIA", "AFRICA", "AMERICAS")) ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-viridis_d-rotate[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap/1000) + aes(y = lifeExp) + geom_point(size = 4, alpha = .8) + aes(color = continent) + facet_wrap(~continent) + * scale_color_viridis_d(guide = F) ``` ] .right-panel-viridis_d-rotate[ <img src="scales_files/figure-html/viridis_d_rotate_12_output-1.png" width="100%" /> ] <style> .left-panel-viridis_d-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-viridis_d-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-viridis_d-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 50%} </style> <!-- ```{r, dpi=400, eval = F} --> <!-- gapminder %>% --> <!-- select(continent) %>% --> <!-- ggplot() + --> <!-- aes(x = 1) + --> <!-- aes(fill = continent) + --> <!-- geom_bar(color = "white", size = .2) + --> <!-- coord_polar(theta = "y") + --> <!-- theme_void() + --> <!-- scale_fill_viridis_d() + --> <!-- theme(rect = element_rect(fill = "grey", --> <!-- color = "grey", --> <!-- linetype = "solid", --> <!-- size = 0)) --> <!-- ggsave("pie.svg", dpi = 320,device = "svg") --> <!-- ``` --> <!-- --- --> <!-- Or at least an organized twitter thread!? Another idea for you: pull aes() out of ggplot(). Do you do it? Lot's of reasons to do it! Downside is currently most examples make use of nested approach. @grrrck does this too, and esquisse --> <!-- ggplot(data = my_data) + --> <!-- aes(x = my_var) --> <!-- Is it possible to do this with multiple geoms? I usually specify aes within the geom like --> <!-- ggplot(data) + --> <!-- geom_point(aes(x, y)) + --> <!-- geom_line(aes(x, y, group = id)) --> <!-- I prefer this approach because it's explicit which aesthetics are bound to which geoms --> <!-- My blog post, (which no one has probably ever read) exactly on this topic! https://evangelinereynolds.netlify.com/post/mapping-aesthetics/ … In general I'd say go global. I think in general, most folks don't have a bunch of conflicts for aesthetics geom by geom (though occasionally yes?). Let me know what you think! --> <!-- To change the data used for a plot, use the %+% operator! Oh!!! --> <!-- # Stat_* --> <!-- ## Univariate discrete --> <!-- ```{r univariate_discrete, eval = F, echo = F} --> <!-- ggplot(gapminder_2002) + --> <!-- aes(x = continent) + --> <!-- stat_count() + --> <!-- geom_bar() # convenience geom --> <!-- # default counting --> <!-- ``` --> <!-- --- --> <!-- r chunk_reveal("univariate_discrete")` --> <!-- --- --> <!-- ```{r} --> <!-- ggplot(data = gapminder_2002) + --> <!-- aes(x = continent) + --> <!-- aes(y = lifeExp) + --> <!-- geom_point(alpha = .1) + --> <!-- stat_summary( --> <!-- fun.ymin = min, --> <!-- fun.ymax = max, --> <!-- fun.y = median --> <!-- ) --> <!-- ``` --> <!-- --- --> <!-- ```{r} --> <!-- gapminder_2002 %>% --> <!-- mutate(seventy_plus = lifeExp > 60) %>% --> <!-- ggplot() + --> <!-- aes(x = continent) + --> <!-- aes(fill = seventy_plus) + --> <!-- geom_bar(alpha = .2) --> <!-- ``` -->