class: center, middle, inverse, title-slide # ggplot basics II ### Intro to data visualization with ggplot2
The R Bootcamp
### November 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"> therbootcamp.github.io </font> </span> </a> <a href="therbootcamp.github.io"> <font color="#7E7E7E"> Intro to data visualization with ggplot2 | Novemeber 2021 </font> </a> </span> </div> --- .pull-left45[ # Simple styling <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>.png</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 <- sdg_props %>% ggplot(aes(x = year, y = prop, col = sdg)) + geom_point() + geom_line() ``` ] .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> ```r # Fixing the legend my_plot + theme_minimal() + # move legend to top theme(legend.position = "top") ``` ] .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> ```r # Fixing the legend my_plot + theme_minimal() + # move legend to top theme(legend.position = c(.85, .5)) ``` ] .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> ```r # Fixing the legend my_plot + theme_minimal() + theme(legend.position = c(.85, .5), # remove legend title legend.title = element_blank()) ``` ] .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>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> ```r # Fixing the legend my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), # increase legend text size legend.text = element_text(size = 10)) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-18-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> ```r # Fixing the legend my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), legend.text = element_text(size = 10), # add white background legend.background = element_rect(fill = "white")) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-20-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> ```r # Fixing the legend my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), legend.text = element_text(size = 10), # add white background legend.background = element_rect(fill = "white", color = "white")) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-22-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[ <img src="PlottingII_files/figure-html/unnamed-chunk-23-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> ```r # Changing colors my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), legend.text = element_text(size = 10), legend.background = element_rect(fill = "white", color = "white")) + # color using own values scale_color_manual(values = c("#A5D7D2", "#2D373C")) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-25-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 # Changing colors my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), legend.text = element_text(size = 10), legend.background = element_rect(fill = "white", color = "white")) + # color using colorblind scale_color_colorblind() ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-27-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> ```r # Changing colors my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), legend.text = element_text(size = 10), legend.background = element_rect(fill = "white", color = "white")) + # color using colorblind scale_color_colorblind( labels = c("No poverty (SDG 1)", "Clean water (SDG 6)")) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-29-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # `geom` constants <ul> <li class="m1"><span>Most aesthetics can be set to constants within the <mono>geom_*()</mono> outside of <mono>aes()</mono></span></li> </ul> <br> ```r # store plot as object my_plot <- sdg_props %>% ggplot(aes(x = year, y = prop, col = sdg)) + geom_point(size = 4) + geom_line() ``` ] .pull-right45[ <br> ```r my_plot ``` <img src="PlottingII_files/figure-html/unnamed-chunk-31-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> ```r # Constants have changed my_plot + theme_minimal() + theme( legend.position = c(.85, .5), legend.title = element_blank(), legend.text = element_text(size = 10), legend.background = element_rect(fill = "white", color = "white")) + # color using colorblind scale_color_colorblind( labels = c("No poverty (SDG 1)", "Clean water (SDG 6)")) ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-33-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 of project start", y = "Relative proportion", title = "SDGs in Swiss Research...", subtitle = "Relative proportion of...") ``` ] .pull-right45[ <br> <img src="PlottingII_files/figure-html/unnamed-chunk-36-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, dpi = 300) ``` ] --- class: middle, center <h1><a href="https://therbootcamp.github.io/SDGDataViz_2021Nov/">Schedule</a></h1>