class: center, middle, inverse, title-slide # Plotting ### The R Bootcamp @ Erfurt
www.therbootcamp.com
@therbootcamp
### June 2018 --- layout: true <div class="my-footer"><span> <a href="https://therbootcamp.github.io/"><font color="#7E7E7E">Erfurt, June 2018</font></a>                                           <a href="https://therbootcamp.github.io/"><font color="#7E7E7E">www.therbootcamp.com</font></a> </span></div> --- ## You can do amazing plots in R! .pull-left2[ <br> <i>"As good as R is for statistics, it's as good if not better for plots."</i><br> <br>Nathaniel D. Phllips<br>2018 ] .pull-right8[ <br> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/ggplotgallery.png" width="60%" style="display: block; margin: auto;" /> ] --- # Base R Plotting .pull-left45[ The **classic framework** of plotting. Contains separate **function for each 'type'** of plot. E.g. `barplot()` for a bar plot, `boxplot()` for a box plot, and `plot()` for a scatterplot. <br><br><br> ```r # Histogram in base R hist(x = mpg$displ, xlab = "Cylinder displacement (l)", ylab = "Frequency", main = "Histogram of displacement") ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> ] --- # Base R Plotting .pull-left45[ The **classic framework** of plotting. Contains separate **function for each 'type'** of plot. E.g. `barplot()` for a bar plot, `boxplot()` for a box plot, and `plot()` for a scatterplot. <br><br><br> ```r # Boxplot in base R boxplot(formula = cty ~ trans, data = mpg, xlab = "Transmission", ylab = "City miles per gallon", main = "Box plot") ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> ] --- # Base R Plotting .pull-left45[ The **classic framework** of plotting. Contains separate **function for each 'type'** of plot. E.g. `barplot()` for a bar plot, `boxplot()` for a box plot, and `plot()` for a scatterplot. <br><br><br> ```r # Scatterplot in base R plot(x = mpg$displ, y = mpg$cty, xlab = "Engine Displacement in Liters", ylab = "City miles per gallon", main = "Scatterplot") ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] --- # Problems with Base R plotting .pull-left35[ 1) Complex plots can quickly require a <b>LOT of code</b>.<br> 2) Default plots look pretty <b>outdated</b>.<br> 3) No way to set plotting <b>themes</b>.<br> 4) Can't store plots as <b>objects</b> to reference and update later</font><br><br><br> The plot to the right would take <b>a lot of code in Base R</b> but **just 10 lines of code**, 5 of which controlling the labels, in `ggplot2`. ] .pull-right55[ <img src="PlottingI_files/figure-html/unnamed-chunk-11-1.png" style="display: block; margin: auto;" /> ] --- # Grammar of Graphics .pull-left5[ <br> <font size = 5>1. A plot is built of simple building blocks</font><br><br> <font size = 5>2. By combining different building blocks, plots of any complexity can be created.</font><br><br> <font size = 5>3. Plots that look superficially different, can actually be created with very similar code.</font><br><br> ] .pull-right5[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/wilkonson_book.png" width="60%" style="display: block; margin: auto;" /> ] --- ## Grammar of Graphics in ggplot .pull-left45[ The Grammar of graphics breaks down plots into several key pieces: | aesthetics| Description| |:------|:----| | Data| What dataframe contains the data?| | Aesthetics| What does the x-axis, y-axis, color (etc) represent?| | Geometries| What kind of geometric object do you want to plot?| | Facets| Should there be groups of plots?| | Statistics|What statistic summaries / transformations should be done?| | Coordinates| What is the scale of the axes?| | Theme| What should the overall plot look like?| ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-13-1.png" style="display: block; margin: auto;" /> ] --- ## Out goal: Creating this plot .pull-left45[ **Data** - Use the `mpg` tibble **Aesthetics** - Show engine displacement (`disp`) on the x axis - Show highway miles per gallon (`hwy`) on the y-axis - Color plotting elements by the class of car (`class`) **Geometric objects** - Show data as points. - Add a regression line **Labels and themes** - Add plotting labels - Use a black and white plotting theme ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-14-1.png" style="display: block; margin: auto;" /> ] --- # `ggplot(data)` .pull-left45[ To create a ggplot2 object, use the `ggplot()` function. This is how every plot begins. `ggplot()` has two main arguments: - `data` - A data frame (aka tibble) - `mapping` - A call to `aes()` Including only a `data` argument returns a blank plotting space, because we haven't specified any plotting **aesthetics** (`aes()`) or **geometric objects** (`geoms()`) ] .pull-right45[ ```r ggplot(data = mpg) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-15-1.png" style="display: block; margin: auto;" /> ] --- # `ggplot(mapping = aes())` .pull-left4[ An **aesthetic mapping** is a visual property of the objects in your plot. Add aesthetics with the `aes()` function. Common aesthetics are... | aesthetics| Description| |:------|:----| | `x`, `y`| Data mapped to coordinates| | `color`, `fill`| Border and fill colors| | `alpha`| Transparency| | `size`| Size| | `shape`| Shape| ] .pull-right5[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-16-1.png" width="70%" style="display: block; margin: auto;" /> ] --- # Adding elements to plots with '+' .pull-left45[ Once you have specified data with `data` argument, and global aesthetics with `mapping = aes()`, you can add additional elements to the plot with `+` The `+` sign works just like the pipe `%>%` in dplyr. It just means "and then..." ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + #and then geom_point() ``` ] .pull-right5[ <img src="PlottingI_files/figure-html/unnamed-chunk-18-1.png" width="90%" style="display: block; margin: auto;" /> ] --- # Geometric objects (geoms) .pull-left4[ A **geom** is a geometric object in a plot that represents data To add a geom to a plot, just include ` + geom_X()` where X is the type of geom. Common geoms are... | geom| output| |:------|:----| | `geom_point()`| Points| | `geom_bar()`| Bar| | `geom_boxplot()`| Boxplot | `geom_count()`| Points with size reflecting frequency| | `geom_smooth()`| Smoothed line| ] .pull-right5[ <img src="PlottingI_files/figure-html/unnamed-chunk-19-1.png" width="90%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ## geom_boxplot() <br> ```r ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_boxplot() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-20-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right45[ ## geom_violin() <br> ```r ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + geom_violin() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-21-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ## geom_bar() <br> ```r ggplot(data = mpg, mapping = aes(x = class)) + geom_bar() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-22-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right45[ ## geom_count() <br> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_count() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-23-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # Aesthetics: `color` .pull-left4[ What plotting aesthetic is missing from the code below to make our plot on the right? ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = class)) + geom_point() ``` ] .pull-right5[ <img src="PlottingI_files/figure-html/unnamed-chunk-25-1.png" width="100%" style="display: block; margin: auto;" /> ] --- # What's next? .pull-left5[ <img src="PlottingI_files/figure-html/unnamed-chunk-26-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-27-1.png" style="display: block; margin: auto;" /> ] --- # `geom_smooth()` .pull-left45[ `geom_smooth()` adds a smoothes line. `geom_smooth()` has two main sets of arguments... - `method` - What function does the line represent? - `col`, `size`, `...` <br> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue") ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-29-1.png" style="display: block; margin: auto;" /> ] --- # `geom_smooth()` .pull-left45[ `geom_smooth()` adds a smoothes line. `geom_smooth()` has two main sets of arguments... - `method` - What function does the line represent? - `col`, `size`, `...` <br> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue", method = "lm") ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-31-1.png" style="display: block; margin: auto;" /> ] --- # Overriding aesthetics .pull-left45[ Note, If you add additional plotting aesthetics, they will <b>override</b> the general plotting aesthetics. This is what happens, when you don't override... <br> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth() # no overriding ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-33-1.png" style="display: block; margin: auto;" /> ] --- # What's next? .pull-left5[ <img src="PlottingI_files/figure-html/unnamed-chunk-34-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-35-1.png" style="display: block; margin: auto;" /> ] --- # Add labels with `labs()` .pull-left45[ You can add labels to a plot with the `labs()` function `labs()` arguments are ... - `title` - Main title - `subtitle` - Subtitle - `caption` - Caption below ```r ggplot(...) + labs(x = "Engine Displ...", y = "Highway miles...", title = "MPG data", subtitle = "Cars with ...", caption = "Source...") ``` ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-37-1.png" style="display: block; margin: auto;" /> ] --- # What's next? .pull-left5[ <img src="PlottingI_files/figure-html/unnamed-chunk-38-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right45[ <img src="PlottingI_files/figure-html/unnamed-chunk-39-1.png" style="display: block; margin: auto;" /> ] --- # Themes with `theme_XX()` .pull-left45[ A plotting **theme** controls many aspects of its overall look, from the background, to the grid lines, to the label font to the spacing between plot labels and the plotting space. Common themes are... `theme_bw()`<br> `theme_minimal()`<br> `theme_classic()`<br> `theme_light()`<br> `theme_dark()` ] .pull-right45[ ```r ggplot(...) + theme_light() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-41-1.png" style="display: block; margin: auto;" /> ] --- # Themes with `theme_XX()` .pull-left45[ A plotting **theme** controls many aspects of its overall look, from the background, to the grid lines, to the label font to the spacing between plot labels and the plotting space. Common themes are... `theme_bw()`<br> `theme_minimal()`<br> `theme_classic()`<br> `theme_light()`<br> `theme_dark()` ] .pull-right45[ ```r ggplot(...) + theme_dark() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-43-1.png" style="display: block; margin: auto;" /> ] --- # Themes with `theme_XX()` .pull-left45[ A plotting **theme** controls many aspects of its overall look, from the background, to the grid lines, to the label font to the spacing between plot labels and the plotting space. Common themes are... `theme_bw()`<br> `theme_minimal()`<br> `theme_classic()`<br> `theme_light()`<br> `theme_dark()` ] .pull-right45[ ```r ggplot(...) + theme_bw() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-45-1.png" style="display: block; margin: auto;" /> ] --- ## Final result! ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue", method = "lm")+ labs(x = "Engine Displacement in Liters", y = "Highway miles per gallon", title = "MPG data", subtitle = "Cars with higher engine displacement tend to have lower highway mpg", caption = "Source: mpg data in ggplot2") + theme_bw() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-46-1.png" width="40%" style="display: block; margin: auto;" /> --- # `facet_wrap()` .pull-left4[ Facetting = Create different plots for different groups To facet plots, use `facet_wrap()` ```r # Without faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() ``` ] .pull-right55[ <img src="PlottingI_files/figure-html/unnamed-chunk-48-1.png" style="display: block; margin: auto;" /> ] --- # `facet_wrap()` .pull-left4[ Facetting = Create different plots for different groups To facet plots, use `facet_wrap()` ```r # With faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + facet_wrap(~ class) # Tilde first ``` ] .pull-right55[ <img src="PlottingI_files/figure-html/unnamed-chunk-50-1.png" style="display: block; margin: auto;" /> ] --- # Assigning a ggplot to an object .pull-left4[ 1) ggplot returns an object of the class "gg".<br> 2) You can assign the result of `ggplot` to an object.<br> 3) Evaluating the object will show the plot.<br> 4) You can even edit existing `ggplot` objects.<br> ```r # Create myplot myplot <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_bw() class(myplot) ``` ``` [1] "gg" "ggplot" ``` ] .pull-right5[ ```r myplot # Evaluate myplot ``` <img src="PlottingI_files/figure-html/unnamed-chunk-52-1.png" style="display: block; margin: auto;" /> ] --- # Assigning a ggplot to an object .pull-left4[ 1) ggplot returns an object of the class "gg".<br> 2) You can assign the result of `ggplot` to an object.<br> 3) Evaluating the object will show the plot.<br> 4) You can even edit existing `ggplot` objects.<br> ```r # Create myplot myplot <- ggplot(data = mpg, aes(x = displ, y = hwy)) + geom_point() + theme_bw() class(myplot) ``` ``` [1] "gg" "ggplot" ``` ] .pull-right5[ ```r myplot + geom_smooth() # add geom ``` <img src="PlottingI_files/figure-html/unnamed-chunk-54-1.png" style="display: block; margin: auto;" /> ] --- # `ggsave()` .pull-left5[ To save plots to a file (e.g.; .jpg, .pdf, .png), use the `ggsave()` function. `ggsave()` main arguments are... |Argument| Definition| |:-------|:----------| |`filename`|File name| |`plot`|Plotting object| |`device`|File type (e.g.; "pdf", "jpeg", "png")| |`path`|File path to save plot| |`width`|Plot width (inches)| |`height`|Plot height (inches)| ] .pull-right45[ Save ggplot object called myplot to a pdf file...<br> ```r # Create myplot object myplot <- ggplot(...) # Create "myplot.pdf", from myplot ggsave(filename = "myplot.pdf", plot = myplot, device = "pdf", path = "figures", width = 6, height = 4) ``` ] --- # So much more Check these links for more in depth ggplot2 guides... 1) `ggplot2` main page: http://ggplot2.tidyverse.org/index.html 2) Wickham's data visualization guide: http://r4ds.had.co.nz/data-visualisation.html <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/plotting_books.png" width="80%" style="display: block; margin: auto;" /> --- <br><br><br> # [Plotting Practical](https://therbootcamp.github.io/Erfurt_2018June/_sessions/D2S1_PlottingI/PlottingI_practical.html)