class: center, middle, inverse, title-slide # Styling with ggplot2 ### Data Visualization for Social Good
CorrelAid Switzerland
### February 2021 --- layout: true <div class="my-footer"> <span style="text-align:center"> <span> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/by-sa.png" height=14 style="vertical-align: middle"/> </span> <a href="https://therbootcamp.github.io/"> <span style="padding-left:82px"> <font color="#7E7E7E"> https://correlaid.org/correlaid-x/switzerland/ </font> </span> </a> <a href="https://correlaid.org/correlaid-x/switzerland/"> <font color="#7E7E7E"> Data Visualization for Social Good | February 2021 </font> </a> </span> </div> --- .pull-left45[ # Styling plots <ul> <li class="m1"><span>Save plot as <highm>gg</highm> object.</span></li> <li class="m2"><span>Use existing <highm>theme_*()</highm> presets.</span></li> <li class="m3"><span>Customize details using <highm>theme()</highm>.</span></li> <li class="m4"><span>Adjust dimensions using <highm>scale_*()</highm>.</span></li> <li class="m5"><span>Add annotion using <highm>labs()</highm>.</span></li> <li class="m6"><span>Write your plot as a <highm>.pdf</highm> or <highm>.png</highm>.</span></li> </ul> ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # the <mono>gg</mono> object <ul> <li class="m1"><span>The output of <mono>ggplot()</mono> can be stored in an <mono>gg</mono> object.</span></li> <li class="m2"><span>The <mono>gg</mono> object can be expanded using <mono>+</mono> and the plot can be generated by a simple print.</span></li> </ul> <br> ```r # store plot as object my_plot <- ggplot(basel, aes(x = year, y = income_median, col = quarter)) + geom_line() + geom_point() ``` ] .pull-right45[ <br> ```r my_plot ``` <img src="PlottingII_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme_*()` <ul> <li class="m1"><span>Using <mono>theme_*()</mono> the plot can be styled according to various presets.</span></li> <li class="m2"><span>A few <mono>theme</mono>s: <br><br> <ul class="level"> <li><span><mono>theme_gray()</mono></span></li> <li><span><mono>theme_classic()</mono></span></li> <li><span><mono>theme_void()</mono></span></li> <li><span><mono>theme_minimal()</mono></span></li> <li><span><mono>theme_excel() (<mono>ggthemes</mono>)</mono></span></li> <li><span><mono>theme_economist() (<mono>ggthemes</mono>)</mono></span></li> </ul> </span></li> </ul> ] .pull-right45[ <br> ```r my_plot + theme_classic() ``` <img src="PlottingII_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme_*()` <ul> <li class="m1"><span>Using <mono>theme_*()</mono> the plot can be styled according to various presets.</span></li> <li class="m2"><span>A few <mono>theme</mono>s: <br><br> <ul class="level"> <li><span><mono>theme_gray()</mono></span></li> <li><span><mono>theme_classic()</mono></span></li> <li><span><mono>theme_void()</mono></span></li> <li><span><mono>theme_minimal()</mono></span></li> <li><span><mono>theme_excel() (<mono>ggthemes</mono>)</mono></span></li> <li><span><mono>theme_economist() (<mono>ggthemes</mono>)</mono></span></li> </ul> </span></li> </ul> ] .pull-right45[ <br> ```r my_plot + theme_excel() ``` <img src="PlottingII_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme_*()` <ul> <li class="m1"><span>Using <mono>theme_*()</mono> the plot can be styled according to various presets.</span></li> <li class="m2"><span>A few <mono>theme</mono>s: <br><br> <ul class="level"> <li><span><mono>theme_gray()</mono></span></li> <li><span><mono>theme_classic()</mono></span></li> <li><span><mono>theme_void()</mono></span></li> <li><span><mono>theme_minimal()</mono></span></li> <li><span><mono>theme_excel() (<mono>ggthemes</mono>)</mono></span></li> <li><span><mono>theme_economist() (<mono>ggthemes</mono>)</mono></span></li> </ul> </span></li> </ul> ] .pull-right45[ <br> ```r my_plot + theme_economist() ``` <img src="PlottingII_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme_*()` <ul> <li class="m1"><span>Using <mono>theme_*()</mono> the plot can be styled according to various presets.</span></li> <li class="m2"><span>A few <mono>theme</mono>s: <br><br> <ul class="level"> <li><span><mono>theme_gray()</mono></span></li> <li><span><mono>theme_classic()</mono></span></li> <li><span><mono>theme_void()</mono></span></li> <li><span><mono>theme_minimal()</mono></span></li> <li><span><mono>theme_excel() (<mono>ggthemes</mono>)</mono></span></li> <li><span><mono>theme_economist() (<mono>ggthemes</mono>)</mono></span></li> </ul> </span></li> </ul> ] .pull-right45[ <br> ```r my_plot + theme_wsj() ``` <img src="PlottingII_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme_*()` <ul> <li class="m1"><span>Using <mono>theme_*()</mono> the plot can be styled according to various presets.</span></li> <li class="m2"><span>A few <mono>theme</mono>s: <br><br> <ul class="level"> <li><span><mono>theme_gray()</mono></span></li> <li><span><mono>theme_classic()</mono></span></li> <li><span><mono>theme_void()</mono></span></li> <li><span><mono>theme_minimal()</mono></span></li> <li><span><mono>theme_excel() (<mono>ggthemes</mono>)</mono></span></li> <li><span><mono>theme_economist() (<mono>ggthemes</mono>)</mono></span></li> </ul> </span></li> </ul> ] .pull-right45[ <br> ```r my_plot + theme_minimal() ``` <img src="PlottingII_files/figure-html/unnamed-chunk-9-1.png" style="display: block; margin: auto;" /> ] --- # `theme()` .pull-left45[ <ul> <li class="m1"><span>With <high>87 arguments</high> <mono>theme()</mono> permits specification of all aesthetic details.</span></li> <li class="m2"><span>Makes uses of helper functions:</span></li> <ul class="level"> <li><span><mono>element_rect()</mono> | for rectangles</span></li> <li><span><mono>element_line()</mono> | for lines</span></li> <li><span><mono>element_text()</mono> | for text</span></li> <li><span><mono>element_blank()</mono> | for removals</span></li> </ul> </ul> <br> ```r # Using theme my_plot + theme(argument = element_*(), argument = element_*(), ...) ``` ] .pull-right45[ <p align="center"> <img src="image/theme.png"> </p> ] --- .pull-left45[ # `theme()` <ul> <li class="m1"><span>With <high>87 arguments</high> <mono>theme()</mono> permits specification of all aesthetic details.</span></li> <li class="m2"><span>Makes uses of helper functions:</span></li> <ul class="level"> <li><span><mono>element_rect()</mono> | for rectangles</span></li> <li><span><mono>element_line()</mono> | for lines</span></li> <li><span><mono>element_text()</mono> | for text</span></li> <li><span><mono>element_blank()</mono> | for removals</span></li> </ul> </ul> <br> ```r # Fixing the legend my_plot + theme_minimal() + # move legend to bottom theme(legend.position = "bottom") ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme()` <ul> <li class="m1"><span>With <high>87 arguments</high> <mono>theme()</mono> permits specification of all aesthetic details.</span></li> <li class="m2"><span>Makes uses of helper functions:</span></li> <ul class="level"> <li><span><mono>element_rect()</mono> | for rectangles</span></li> <li><span><mono>element_line()</mono> | for lines</span></li> <li><span><mono>element_text()</mono> | for text</span></li> <li><span><mono>element_blank()</mono> | for removals</span></li> </ul> </ul> <br> ```r # Fixing the legend my_plot + theme_minimal() + theme(legend.position = "bottom", # remove legend title legend.title = element_blank()) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme()` <ul> <li class="m1"><span>With <high>87 arguments</high> <mono>theme()</mono> permits specification of all aesthetic details.</span></li> <li class="m2"><span>Makes uses of helper functions:</span></li> <ul class="level"> <li><span><mono>element_rect()</mono> | for rectangles</span></li> <li><span><mono>element_line()</mono> | for lines</span></li> <li><span><mono>element_text()</mono> | for text</span></li> <li><span><mono>element_blank()</mono> | for removals</span></li> </ul> </ul> <br> ```r # Fixing the legend my_plot + theme_minimal() + theme( legend.position = "bottom", legend.title = element_blank(), # reduce legend text size legend.text = element_text(size = 7)) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-16-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `theme` <ul> <li class="m1"><span><mono>theme</mono>s can be stored in an object.</span></li> <li class="m2"><span><mono>theme</mono> objects are not functions.</span></li> </ul> <br> ```r # save my theme my_theme <- theme( legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size = 7)) # Add my theme my_plot + theme_minimal() + my_theme ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-18-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `scale_*()` <ul> <li class="m1"><span>Various <mono>scale_*()</mono> permit specification of all <high>dimensions</high>, inclding axes, colors, sizes, etc.</span></li> <li class="m2"><span>Groups of <mono>scale_*()</mono> functions:</span></li> <ul class="level"> <li><span><mono>scale_xy_*</mono> | Scales axes</span></li> <li><span><mono>scale_color_*</mono> | Scales colors</span></li> <li><span><mono>scale_size_*</mono> | Sclaes sizes</span></li> <li><span><mono>scale_alpha_*</mono> | Scales opacity</span></li> </ul> </ul> ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-19-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `scale_*()` <ul> <li class="m1"><span>Various <mono>scale_*()</mono> permit specification of all <high>dimensions</high>, inclding axes, colors, sizes, etc.</span></li> </ul> <br> ```r # Fixing the legend my_plot + theme_minimal() + theme( legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size=7)) + # color using viridis scale_color_viridis_d() ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-21-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Wrangling <ul> <li class="m1">Again, wrangling can help with plotting.</span></li> <li class="m2">The order of discrete variables can be controlled using <highm>factors</highm>.</span></li> </ul> ```r basel %>% # sort by income and factor quarter arrange(year, income_median) %>% mutate(quarter = as_factor(quarter)) %>% # original code ggplot(aes(x = year, y = income_median, col = quarter)) + geom_line() + geom_point() + theme_minimal() + theme( legend.position = "bottom", legend.title = element_blank(), legend.text = element_text(size=7)) + scale_color_viridis_d() ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `labs()` <ul> <li class="m1"><span>Vaious annotions can be added using <mono>labs()</mono>.</span></li> <li class="m2"><span>Key arguments:</span></li> <ul class="level"> <li><span><mono>x,y</mono> | Axes</span></li> <li><span><mono>title, subtitle</mono> | Title and Subtitle</high></span></li> <li><span><mono>caption</mono> | Caption</high></span></li> </ul> </ul> <br> ```r my_plot + labs(x = "Year", y = "Median income", title = "Inequality in Basel", subtitle = "Income development...", caption = "Source: Open Data...") ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-26-1.png" style="display: block; margin: auto;" /> ] --- # `ggsave()` .pull-left45[ <ul> <li class="m1"><span>Saves plots to the harddrive.</span></li> <li class="m2"><span>Key arguments:</span></li> <ul class="level"> <li><span><mono>filename</mono> | Filename/path</span></li> <li><span><mono>device</mono> | e.g., <mono>".pdf"</mono> or <mono>".png"</mono></span></li> <li><span><mono>path</mono> | Path to folder</span></li> <li><span><mono>height, width</mono> | Height, Width</span></li> <li><span><mono>unit</mono> | Unit for Height, Width</span></li> <li><span><mono>dpi</mono> | Resolution</span></li> </ul> </ul> ] .pull-right45[ <br> ```r # Save as pdf ggsave(filename = "my_plot.pdf", plot = my_plot, device = "pdf", path = "3_Figures", width = 7, height = 7) # Save as png ggsave(filename = "my_plot.png", plot = my_plot, device = "png", path = "3_Figures", width = 7, height = 7) ``` ] --- class: middle, center <h1><a href="https://correlaidswitzerland.github.io/DataViz4Good/">Schedule</a></h1>