class: center, middle, inverse, title-slide # Plotting II ### Introduction to R
Bern R Bootcamp
### June 2020 --- 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"> www.therbootcamp.com </font> </span> </a> <a href="https://therbootcamp.github.io/"> <font color="#7E7E7E"> R Bootcamp Bern | June 2020 </font> </a> </span> </div> --- # Customizing plots .pull-left45[ <br2> <font style="font-size:24px"> <b>1</b> <high>Store</high> and customize plot as a <high><mono>gg</mono> object</high>. <br><br> <b>2</b> Create multiple plots using <high>facetting</high>. <br><br> <b>3</b> Use <high>themes</high> to control every <high>detail</high> of the plot. <br><br> <b>4</b> Create your <high>own themes</high>. <br><br> <b>5</b> Create <high>image files</high> in <mono>.pdf</mono>, <mono>.png</mono>, etc. </font> ] .pull-right45[ <img src="image/button_sm.jpeg" style="width:400px"> ] --- # The <mono>gg</mono> object .pull-left4[ <b>1</b> ggplot returns an object of the class `gg`.<br> <b>2</b> You can assign the result of `ggplot` to an <high>object</high>.<br> <b>3</b> Evaluating the object will show the plot.<br> <b>4</b> You can even edit existing <high>`ggplot` objects</high>.<br> ```r # Create myplot myplot <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_bw() class(myplot) ``` ``` [1] "gg" "ggplot" ``` ] .pull-right45[ ```r myplot # Evaluate myplot ``` <img src="PlottingII_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> ] --- # The <mono>gg</mono> object .pull-left4[ <b>1</b> ggplot returns an object of the class `gg`.<br> <b>2</b> You can assign the result of `ggplot` to an <high>object</high>.<br> <b>3</b> Evaluating the object will show the plot.<br> <b>4</b> You can even edit existing <high>`ggplot` objects</high>.<br> ```r # Create myplot myplot <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_bw() class(myplot) ``` ``` [1] "gg" "ggplot" ``` ] .pull-right45[ ```r myplot + geom_smooth() # add geom ``` <img src="PlottingII_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] --- # `facet_wrap()` .pull-left45[ Faceting = <high>same plot for different groups</high>. To facet plots, use, e.g., `facet_wrap()`. ```r # Without faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme_bw() ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> ] --- # `facet_wrap()` .pull-left4[ Faceting = <high>same plot for different groups</high>. To facet plots, use, e.g., `facet_wrap()`. ```r # With faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme_bw() + facet_wrap(~ class) # Tilde first ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] --- # `facet_grid()` .pull-left4[ Faceting = <high>same plot for different groups</high>. Use `facet_grid()` to create plots of one (set of) variable against those another. ```r # With faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme_bw() + facet_grid(drv ~ class) ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-12-1.png" style="display: block; margin: auto;" /> ] --- # `theme()` .pull-left45[ To <high>customize the appearance of your plot use `theme()`. `theme()` has <high>87 arguments</high> allowing to fine-tune, e.g., the background, the axes, the legend, etc. Arguments are specified via `element_*()` helper functions, mostly `element_rect()` or `element_line()`. ```r # use theme to change appearance myplot + theme(argument = element_*(), argument = element_*(), etc.) ``` ] .pull-right45[ <p align="center"> <img src="image/theme.png"> </p> ] --- # Background color: `theme()` .pull-left45[ Address the <high>panel background</high> using `panel.background` and that of the <high>plot background</high> using `plot.background` and change the color using `element_rect(fill = color)`. ```r # change panel and plot color myplot + theme( panel.background = element_rect(fill = 'tomato'), plot.background = element_rect(fill = 'burlywood')) ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> ] --- # Background grid: `theme()` .pull-left45[ Address the <high>grid line colors</high> using `panel.grid.major` (large grid lines) and `panel.grid.minor` (small grid lines) and change the color using `element_line(colour = color)`. ```r # change grid line color myplot + theme( panel.grid.major = element_line(colour = "salmon"), panel.grid.minor = element_line(colour = "seagreen") ) ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-17-1.png" style="display: block; margin: auto;" /> ] --- # Background grid: `theme()` .pull-left45[ Change the <high>grid line sizes</high> using `element_line(size = number)`. ```r # change grid line color myplot + theme( panel.grid.major = element_line(colour = "salmon", size = 3), panel.grid.minor = element_line(colour = "seagreen", size = 1.5) ) ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-19-1.png" style="display: block; margin: auto;" /> ] --- # Axes: `theme()` .pull-left45[ Change the <high>axes</high> using the ` axis.line.x` and ` axis.line.y` arguments and `element_line(colour, size, lineend, ...)`. ```r # change grid line color myplot + theme( axis.line.x = element_line(colour = "deeppink", size = 3.5, lineend = "butt"), axis.line.y = element_line(colour = "deeppink", size = 3.5) ) ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-21-1.png" style="display: block; margin: auto;" /> ] --- # Axes: `theme()` .pull-left45[ Change the <high>axes labels</high> using the ` axis.title.x` and ` axis.title.y` arguments and `element_text(family, face, ...)`. ```r # change grid line color myplot + theme( axis.title.x = element_text( family = "Comic Sans MS", size = 30), axis.title.y = element_text(family = "Comic Sans MS", size = 30) ) ``` ] .pull-right45[ <img src="PlottingII_files/figure-html/unnamed-chunk-23-1.png" style="display: block; margin: auto;" /> ] --- .pull-left45[ # Further arguments<font style="font-size:16px"> (incomplete)</font> <u><mono>theme()</mono></u> <table style="width:100%"> <tr> <td> <b>Argument</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>axis.title.*</mono> </td> <td> Everything about the axis labels </td> </tr> <tr> <td> <mono>axis.ticks.*</mono> </td> <td> Everything about the axis tick marks </td> </tr> <tr> <td> <mono>axis.line.*</mono> </td> <td> Everything about the axis line </td> </tr> <tr> <td> <mono>legend.*</mono> </td> <td> Everything about the legend </td> </tr> <tr> <td> <mono>panel.*</mono> </td> <td> Everything about the panel (inner plot region) </td> </tr> <tr> <td> <mono>plot.*</mono> </td> <td> Everything about the plot (outer plot region) </td> </tr> <tr> <td> <mono>strip.*</mono> </td> <td> Everything about facet headers </td> </tr> </table> ] .pull-right45[ <br> <u><mono>element_rect()</mono></u> <table style="width:100%"> <tr> <td> <b>Argument</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>fill</mono> </td> <td> Fill color </td> </tr> <tr> <td> <mono>colour</mono> </td> <td> Border color </td> </tr> </table> <u><mono>element_line()</mono></u> <table style="width:100%"> <tr> <td> <b>Argument</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>size</mono> </td> <td> Line size </td> </tr> <tr> <td> <mono>linetype</mono> </td> <td> Type of line (solid, dashed, etc.) </td> </tr> </table> <u><mono>element_text()</mono></u> <table style="width:100%"> <tr> <td> <b>Argument</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>face</mono> </td> <td> Font face </td> </tr> <tr> <td> <mono>colour</mono> </td> <td> Font colour </td> </tr> </table> ] --- # Create themes .pull-left45[ ```r # define my theme mytheme <- theme( panel.background = element_rect(fill = 'tomato'), plot.background = element_rect(fill = 'burlywood'), panel.grid.major = element_line( colour = "salmon", size = 3), panel.grid.minor = element_line( colour = "seagreen", size = 1.5), axis.line.x = element_line( colour = "deeppink", size = 3.5, lineend = "butt"), axis.line.y = element_line( colour = "deeppink", size = 3.5), axis.title.x = element_text( family = "Comic Sans MS", size = 30), axis.title.y = element_text( family = "Comic Sans MS", size = 30) ) ``` ] .pull-right45[ ```r myplot ``` <img src="PlottingII_files/figure-html/unnamed-chunk-26-1.png" style="display: block; margin: auto;" /> ] --- # Create themes .pull-left45[ ```r # define my theme mytheme <- theme( panel.background = element_rect(fill = 'tomato'), plot.background = element_rect(fill = 'burlywood'), panel.grid.major = element_line( colour = "salmon", size = 3), panel.grid.minor = element_line( colour = "seagreen", size = 1.5), axis.line.x = element_line( colour = "deeppink", size = 3.5, lineend = "butt"), axis.line.y = element_line( colour = "deeppink", size = 3.5), axis.title.x = element_text( family = "Comic Sans MS", size = 30), axis.title.y = element_text( family = "Comic Sans MS", size = 30) ) ``` ] .pull-right45[ ```r myplot + mytheme ``` <img src="PlottingII_files/figure-html/unnamed-chunk-29-1.png" style="display: block; margin: auto;" /> ] --- # Scales .pull-left45[ <high>`Scales` control</high> many automatic aspects of the plot such as the axis limits or the color scale. <u>Scale functions</u> <table style="width:100%"> <tr> <td> <b>Function</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>scale_color_*</mono>, <mono>scale_fill_*</mono> </td> <td> Control coloring </td> </tr> <tr> <td> <mono>scale_scale_xy*</mono> </td> <td> Control axis dimensions </td> </tr> <tr> <td> <mono>scale_size_*</mono> </td> <td> Control geom sizes </td> </tr> <tr> <td> <mono>scale_alpha_*</mono> </td> <td> Control transparency </td> </tr> <tr> <td> ... </td> <td> ... </td> </tr> </table> ] .pull-right45[ ```r # Original plot myplot ``` <img src="PlottingII_files/figure-html/unnamed-chunk-31-1.png" style="display: block; margin: auto;" /> ] --- # Scales .pull-left45[ <high>`Scales` control</high> many automatic aspects of the plot such as the axis limits or the color scale. <u>Scale functions</u> <table style="width:100%"> <tr> <td> <b>Function</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>scale_color_*</mono>, <mono>scale_fill_*</mono> </td> <td> Control coloring </td> </tr> <tr> <td> <mono>scale_scale_xy*</mono> </td> <td> Control axis dimensions </td> </tr> <tr> <td> <mono>scale_size_*</mono> </td> <td> Control geom sizes </td> </tr> <tr> <td> <mono>scale_alpha_*</mono> </td> <td> Control transparency </td> </tr> <tr> <td> ... </td> <td> ... </td> </tr> </table> ] .pull-right45[ ```r myplot + scale_x_continuous(limits = c(1, 30)) ``` <img src="PlottingII_files/figure-html/unnamed-chunk-33-1.png" style="display: block; margin: auto;" /> ] --- # Scales .pull-left45[ <high>`Scales` control</high> many automatic aspects of the plot such as the axis limits or the color scale. <u>Scale functions</u> <table style="width:100%"> <tr> <td> <b>Function</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>scale_color_*</mono>, <mono>scale_fill_*</mono> </td> <td> Control coloring </td> </tr> <tr> <td> <mono>scale_scale_xy*</mono> </td> <td> Control axis dimensions </td> </tr> <tr> <td> <mono>scale_size_*</mono> </td> <td> Control geom sizes </td> </tr> <tr> <td> <mono>scale_alpha_*</mono> </td> <td> Control transparency </td> </tr> <tr> <td> ... </td> <td> ... </td> </tr> </table> ] .pull-right45[ ```r myplot + scale_x_reverse() ``` <img src="PlottingII_files/figure-html/unnamed-chunk-35-1.png" style="display: block; margin: auto;" /> ] --- # Scales .pull-left45[ <high>`Scales` control</high> many automatic aspects of the plot such as the axis limits or the color scale. <u>Scale functions</u> <table style="width:100%"> <tr> <td> <b>Function</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>scale_color_*</mono>, <mono>scale_fill_*</mono> </td> <td> Control coloring </td> </tr> <tr> <td> <mono>scale_scale_xy*</mono> </td> <td> Control axis dimensions </td> </tr> <tr> <td> <mono>scale_size_*</mono> </td> <td> Control geom sizes </td> </tr> <tr> <td> <mono>scale_alpha_*</mono> </td> <td> Control transparency </td> </tr> <tr> <td> ... </td> <td> ... </td> </tr> </table> ] .pull-right45[ ```r myplot + scale_colour_hue(h = c(160, 260)) ``` <img src="PlottingII_files/figure-html/unnamed-chunk-37-1.png" style="display: block; margin: auto;" /> ] --- # Scales .pull-left45[ <high>`Scales` control</high> many automatic aspects of the plot such as the axis limits or the color scale. <u>Scale functions</u> <table style="width:100%"> <tr> <td> <b>Function</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>scale_color_*</mono>, <mono>scale_fill_*</mono> </td> <td> Control coloring </td> </tr> <tr> <td> <mono>scale_scale_xy_*</mono> </td> <td> Control axis dimensions </td> </tr> <tr> <td> <mono>scale_size_*</mono> </td> <td> Control geom sizes </td> </tr> <tr> <td> <mono>scale_alpha_*</mono> </td> <td> Control transparency </td> </tr> <tr> <td> ... </td> <td> ... </td> </tr> </table> ] .pull-right45[ ```r myplot + scale_size(range = c(1, 15)) ``` <img src="PlottingII_files/figure-html/unnamed-chunk-39-1.png" style="display: block; margin: auto;" /> ] --- # Multiple panels .pull-left45[ Using `grid.arrange()` from the `gridExtra` package, <high>multiple plots</high> (as `gg` objects) can be placed next to or on top of each other in multiple panels. ```r grid.arrange(plot1, plot2, nrow = number, ncol = number) ``` ] .pull-right45[ ```r grid.arrange(myplot, myplot + mytheme, nrow = 1) ``` <img src="PlottingII_files/figure-html/unnamed-chunk-42-1.png" style="display: block; margin: auto;" /> ] --- # Multiple panels .pull-left45[ Using `grid.arrange()` from the `gridExtra` package, <high>multiple plots</high> (as `gg` objects) can be placed next to or on top of each other in multiple panels. ```r grid.arrange(plot1, plot2, nrow = number, ncol = number) ``` ] .pull-right45[ ```r grid.arrange(myplot, myplot + mytheme, ncol = 1) ``` <img src="PlottingII_files/figure-html/unnamed-chunk-44-1.png" style="display: block; margin: auto;" /> ] --- # `ggsave()` .pull-left5[ To create an <high>image file</high> of a plot (e.g., `.jpg`, `.pdf`, `.png`), use the `ggsave()` function. <u>Arguments</u> <table style="width:100%"> <tr> <td> <b>Argument</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>filename</mono> </td> <td> Name of the to-be-created file </td> </tr> <tr> <td> <mono>device</mono> </td> <td> File type (e.g.; "pdf", "jpeg", "png") </td> </tr> <tr> <td> <mono>path</mono> </td> <td> Folder to store image in </td> </tr> <tr> <td> <mono>width</mono>, <mono>height</mono> </td> <td> Plot width, height (e.g., inches) </td> </tr> </table> ] .pull-right45[ ```r # Create myplot object myplot <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + mytheme # Create "myplot.pdf", from myplot ggsave(filename = "myplot.pdf", plot = myplot, device = "pdf", path = "figures", width = 6, height = 4) ``` ] --- # `ggsave()` .pull-left5[ To create an <high>image file</high> of a plot (e.g., `.jpg`, `.pdf`, `.png`), use the `ggsave()` function. <u>Arguments</u> <table style="width:100%"> <tr> <td> <b>Argument</b> </td> <td> <b>Description</b> </td> </tr> <tr> <td> <mono>filename</mono> </td> <td> Name of the to-be-created file </td> </tr> <tr> <td> <mono>device</mono> </td> <td> File type (e.g.; "pdf", "jpeg", "png") </td> </tr> <tr> <td> <mono>path</mono> </td> <td> Folder to store image in </td> </tr> <tr> <td> <mono>width</mono>, <mono>height</mono> </td> <td> Plot width, height (e.g., inches) </td> </tr> </table> ] .pull-right45[ ```r # Create myplot object myplot <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + mytheme # Create "myplot.png", from myplot ggsave(filename = "myplot.png", plot = myplot, device = "png", path = "figures", width = 6, height = 4) ``` ] --- class: middle, center <h1><a href="https://dwulff.github.io/Intro2R_Unibe/_sessions/PlottingII/PlottingII_practical.html">Practical</a></h1>