class: center, middle, inverse, title-slide # Plotting ## With ggplot2 ### The R Bootcamp
Twitter:
@therbootcamp
### April 2018 --- # You can do amazing plots in R! - As good as R is for statistics, it's as good if not better for plots. <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/ggplotgallery.png" width="60%" style="display: block; margin: auto;" /> --- .pull-left45[ ## Base R Plotting There are different frameworks for doing plotting in R. The classic framework is known as <b>Base R</b> plotting. In Base R plotting, there is a separate function for each 'type' of plot | Plot type| Function| |:------|:----| | Bar plot| `barplot()`| | Box plot| `boxplot()`| | Scatterplot| `plot()`| ] .pull-right5[ ```r # Histogram in base R hist(x = ChickWeight$weight, xlab = "Weight", ylab = "Frequency", main = "Histogram in Base R") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-3-1.png" width="80%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ## Base R Plotting There are different frameworks for doing plotting in R. The classic framework is known as <b>Base R</b> plotting. In Base R plotting, there is a separate function for each 'type' of plot | Plot type| Function| |:------|:----| | Bar plot| `barplot()`| | Box plot| `boxplot()`| | Scatterplot| `plot()`| ] .pull-right5[ ```r # Boxplot in base R boxplot(formula = cty ~ trans, data = mpg, xlab = "Transmission", ylab = "City miles per gallon", main = "Boxplot in Base R") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-4-1.png" width="80%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ## Base R Plotting There are different frameworks for doing plotting in R. The classic framework is known as <b>Base R</b> plotting. In Base R plotting, there is a separate function for each 'type' of plot | Plot type| Function| |:------|:----| | Bar plot| `barplot()`| | Box plot| `boxplot()`| | Scatterplot| `plot()`| ] .pull-right5[ ```r # Scatterplot in base R plot(x = economics$date, y = economics$unemploy, xlab = "Date", ylab = "Unemployment rate", main = "Scatterplot in Base R" ) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-5-1.png" width="80%" style="display: block; margin: auto;" /> ] --- .pull-left5[ ## Problems with Base R plotting <br> <font size = 5>Complex plots can quickly require a <b>LOT of code</b>.</font><br><br> <font size = 5>Default plots look pretty <b>outdated</b></font><br><br> <font size = 5>No way to set plotting <b>themes</b></font><br><br> <font size = 5>Can't store plots as <b>objects</b> to reference and update later</font><br><br> ] .pull-right5[ <br><br><br> <font size = 5>This plot would take a <b>lot</b> of code in Base R</font> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/complexplot1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## ggplot2 .pull-left45[ <br> <font size = 5>How do we make elegant, easy to program plots according to the grammar of graphics in R?</font><br><br> <font size = 5></b>Answer: ggplot2</b></font><br> <br> <font size = 5>One of the most widely used R packages, used to generate the vast majority of plots from R.</font> ] .pull-right5[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/wickham_portrait.png" width="70%" style="display: block; margin: auto;" /> ] --- ## Grammar of Graphics .pull-left5[ <br> <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 .pull-left5[ 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-right5[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/complexplot1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Our goal .pull-left5[ - In this introduction, we'll introduce the basic building blocks of creating plots with ggplot2 by creating the following plot from the ground-up: <img src="PlottingI_files/figure-html/unnamed-chunk-10-1.png" style="display: block; margin: auto;" /> ] .pull-right45[ ### In the practical, you will create all of these!! <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/ggplotgallery.png" width="100%" style="display: block; margin: auto;" /> ] --- ## ggplot2 .pull-left6[ <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/ggplot_hex.png" width="60%" style="display: block; margin: auto;" /> ] .pull-right4[ ### Load the ggplot2 package ```r # Load the tidyverse (includes ggplot2) library(tidyverse) ``` ### Or ```r # Load ggplot2 directly library(ggplot2) ``` ] --- ## `mpg` data .pull-left6[ The `mpg` data is a tibble of car data contained in the `ggplot2` package |manufacturer |model | cty| hwy|class | |:------------|:------------------|---:|---:|:----------| |land rover |range rover | 11| 15|suv | |hyundai |sonata | 19| 28|midsize | |toyota |camry | 21| 31|midsize | |jeep |grand cherokee 4wd | 15| 20|suv | |chevrolet |c1500 suburban 2wd | 14| 20|suv | |ford |mustang | 15| 23|subcompact | |volkswagen |new beetle | 29| 41|subcompact | |ford |expedition 2wd | 11| 17|suv | ] .pull-right3[ <br> <br> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/carlot.jpg" width="80%" style="display: block; margin: auto;" /> ] --- ## Creating this plot .pull-left5[ **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-right5[ Our goal is to build the following plot step by step: <img src="PlottingI_files/figure-html/unnamed-chunk-17-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ## ggplot(data) .pull-left5[ - To create a ggplot2 object, use the `ggplot()` function. | argument| description| |:------|:----| | `data`| A dataframe (or tibble)| | `mapping`|A call to aes()| - Including only a `data` argument returns a blank plotting space, because we haven't specified any plotting *aesthetics* or *geometric objects* ] .pull-right5[ ```r ggplot(data = mpg) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-18-1.png" width="70%" style="display: block; margin: auto;" /> ] --- ## ggplot(mapping = aes()) .pull-left4[ - An **aesthetic mapping** is a visual property of the objects in your plot. ### Common aesthetics | aesthetics| Description| |:------|:----| | `x`, `y`| Data mapped to coordinates| | `color`, `fill`| Border and fill colors| | `alpha`| Transparency| | `size`| Size| | `shape`| Shape| - Add aesthetics with the `aes()` function - Arguments can be <b>columns</b> in your dataframe, or in some cases <b>fixed</b> (i.e.; string or integer) values ] .pull-right5[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-19-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... # MORE + (and then...) # MORE + (and then...) ``` ] .pull-right5[ ### ggplot uses `+` to add additional elements to a plot <img src="PlottingI_files/figure-html/unnamed-chunk-21-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 | geom| output| |:------|:----| | `geom_point()`| Points| | `geom_bar()`| Bar| | `geom_boxplot()`| Boxplot | `geom_count()`| Points with size reflecting frequency| | `geom_smooth()`| Smoothed line| ] .pull-right5[ ### Add a point geom with `geom_point()` ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-22-1.png" width="60%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ### geom_boxplot() ```r ggplot(data = mpg, mapping = aes(x = class, y = hwy, fill = class)) + geom_boxplot() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-23-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right45[ ### geom_violin() ```r ggplot(data = mpg, mapping = aes(x = class, y = hwy, fill = class)) + geom_violin() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-24-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ### geom_bar() ```r ggplot(data = mpg, mapping = aes(x = class, fill = class)) + geom_bar() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-25-1.png" width="80%" style="display: block; margin: auto;" /> ] .pull-right45[ ### geom_count() ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_count() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-26-1.png" width="100%" style="display: block; margin: auto;" /> ] --- .pull-left45[ ### geom_density() ```r ggplot(data = mpg, mapping = aes(x = hwy, fill = class)) + geom_density() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-27-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right45[ ### geom_tile() ```r ggplot(nba_long, mapping = aes(x = Name, y = measure, fill = value)) + geom_tile(col = "white") + scale_fill_gradientn(colors = c("red", "white", "blue")) + coord_flip() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-29-1.png" width="60%" style="display: block; margin: auto;" /> ] --- ## Aesthetics .pull-left45[ > 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)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-30-1.png" width="80%" style="display: block; margin: auto;" /> ] .pull-right5[ <img src="PlottingI_files/figure-html/unnamed-chunk-31-1.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Color aesthetic ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + # Map mpg$class to color aes geom_point() # Add points that respect the aesthetics ``` <img src="PlottingI_files/figure-html/unnamed-chunk-32-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Geometric objects (geoms) .pull-left45[ *Code WITHOUT color aesthetic* ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-33-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right45[ *Code WITH color aesthetic* ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-34-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ### What's next? .pull-left45[ ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-35-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right45[ <font size=5>Our goal</font> <br> <img src="PlottingI_files/figure-html/unnamed-chunk-36-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ### Smoothed lines with `geom_smooth()` .pull-left45[ To add a smoothed line to a plot, use geom_smooth() Main geom_smooth() arguments<br><br> | Arguments| Description| |:------|:----| | method| What function does the line represent?| | col, size, ...| Other plotting aesthetics| <br><font size = 5>If you add additional plotting aesthetics, they will <b>override</b> the general plotting aesthetics</font> ] .pull-right45[ <font size =5>method = "lm"</font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-37-1.png" style="display: block; margin: auto;" /> ] --- ### Smoothed lines with `geom_smooth()` .pull-left45[ To add a smoothed line to a plot, use geom_smooth() Main geom_smooth() arguments<br><br> | Arguments| Description| |:------|:----| | method| What function does the line represent?| | col, size, ...| Other plotting aesthetics| <br><font size = 5>If you add additional plotting aesthetics, they will <b>override</b> the general plotting aesthetics</font> ] .pull-right45[ <font size =5>method = "lm"</font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue", method = "lm") # regression ``` <img src="PlottingI_files/figure-html/unnamed-chunk-38-1.png" style="display: block; margin: auto;" /> ] --- ## Overriding aesthetics - You can include additional aesthetics, like color, shape, and size, in *any* geom. - This will override the *global* aesthetics ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + # Global aesthetics geom_point() + geom_smooth(col = "blue") # geom_smooth IGNORES global col aesthetic ``` <img src="PlottingI_files/figure-html/unnamed-chunk-39-1.png" width="50%" style="display: block; margin: auto;" /> --- ## Example A: No overriding ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + # Global aesthetics geom_point() + # geom_point RESPECTS global col aesthetic (class) geom_smooth() # geom_smooth RESPECTS global col aesthetic (class) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-40-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Example B: Override col aesthetic in geom_smooth() ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + # Global aesthetics geom_point() + # geom_smooth(col = "blue") # Overrides global col aesthetic (class) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-41-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Example C: Override col aesthetic in geom_smooth() & geom_point() ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + # Global aesthetics geom_point(col = "pink") + # Overrides global col aesthetic (class) geom_smooth(col = "blue") # Overrides global col aesthetic (class) ``` <img src="PlottingI_files/figure-html/unnamed-chunk-42-1.png" width="60%" style="display: block; margin: auto;" /> --- ## Example D: Override col aesthetic in geom_point() ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + # Global aesthetics geom_point(col = "pink") + # Overrides global col aesthetic (class) geom_smooth() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-43-1.png" width="60%" style="display: block; margin: auto;" /> --- ## What we want ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue", # Overrides global col aesthetic (class) method = "lm") # Use lm (linear model) smoothing line ``` <img src="PlottingI_files/figure-html/unnamed-chunk-44-1.png" width="60%" style="display: block; margin: auto;" /> --- ### What's next? .pull-left45[ <font size=5>Where we are at</font>. ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point() + geom_smooth(col = "blue", method = "lm") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-45-1.png" width="90%" style="display: block; margin: auto;" /> ] .pull-right45[ <font size=5>Our goal</font> <br> <br> <br> <img src="PlottingI_files/figure-html/unnamed-chunk-46-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ## Add labels with `labs()` .pull-left45[ You can add labels to a plot with the `labs()` function ### `labs()` arguments | Arguments| Description| |:------|:----| | `title`|Main Title| | `subtitle`| Subtitle| | `caption`| Caption below plot| ] .pull-right45[ ```r ggplot(...) + labs(x = "Engine Displ...", y = "Highway miles...", title = "MPG data", subtitle = "Cars with ...", caption = "Source...") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-48-1.png" width="90%" style="display: block; margin: auto;" /> ] --- ### What's next? .pull-left6[ <font size = 5>Where are are at</font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy, col = class)) + geom_point(size = 3, alpha = .9) + geom_smooth(col = "blue", method = "lm")+ labs(x = "Engine...", y = "Highway ...", title = "MPG data", subtitle = "Cars with...", caption = "Source:...") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-49-1.png" width="60%" style="display: block; margin: auto;" /> ] .pull-right35[ <br> <font size=5>Our goal</font> <br> <br> <br> <img src="PlottingI_files/figure-html/unnamed-chunk-50-1.png" width="100%" 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 | Themes| |:------| | `theme_bw()`| | `theme_minimal()`| | `theme_classic()`| | `theme_light()`| | `theme_dark()`| - You can easily add a theme to a plot by including `+ theme_XX()` ] .pull-right5[ <font size = 5>No theme specified </font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + geom_smooth(method = "lm") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-51-1.png" width="90%" 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 | Themes| |:------| | `theme_bw()`| | `theme_minimal()`| | `theme_classic()`| | `theme_light()`| | `theme_dark()`| - You can easily add a theme to a plot by including `+ theme_XX()` ] .pull-right5[ <font size = 5>+ theme_bw()</font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme_bw() # Use the black and white theme ``` <img src="PlottingI_files/figure-html/unnamed-chunk-52-1.png" width="90%" 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 | Themes| |:------| | `theme_bw()`| | `theme_minimal()`| | `theme_classic()`| | `theme_light()`| | `theme_dark()`| - You can easily add a theme to a plot by including `+ theme_XX()` ] .pull-right5[ <font size = 5>+ theme_dark()</font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme_dark() # Use the dark theme ``` <img src="PlottingI_files/figure-html/unnamed-chunk-53-1.png" width="90%" 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 | Themes| |:------| | `theme_bw()`| | `theme_minimal()`| | `theme_classic()`| | `theme_light()`| | `theme_dark()`| - You can easily add a theme to a plot by including `+ theme_XX()` ] .pull-right5[ <font size = 5>+ theme_light()</font> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme_light() # Use the light theme ``` <img src="PlottingI_files/figure-html/unnamed-chunk-54-1.png" width="90%" style="display: block; margin: auto;" /> ] --- .pull-left65[ ## Themes <font size = 5>You can customise specific elements of themes in the `theme()` function</font><br> ```r ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + theme(plot.background = element_rect(fill = "skyblue"), panel.background = element_rect(fill = "skyblue"), title = element_text(colour = "white", size = 20), axis.title = element_text(colour = "white")) + labs(title = "White Title") ``` <img src="PlottingI_files/figure-html/unnamed-chunk-55-1.png" width="60%" style="display: block; margin: auto;" /> ] .pull-right3[ <br><br><br> <font size = 5>Look at the theme help menu with ?theme() to see all possible theme adjustments</font> <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/theme_help_ss.png" width="100%" style="display: block; margin: auto;" /> ] --- ## Set a global theme with theme_set() .pull-left45[ <br> <font size = 5>You can set a <b>global theme</b> with the theme_set() function</font> <br> ```r # Set global theme to minimal theme_set(theme_bw()) # All fugure plots will now use theme_bw()! ``` <br> <font size = 5>When you do, all future plots will use that theme!</font> ] .pull-right5[ ```r #Set global theme to theme_dark() theme_set(theme_dark()) ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-58-1.png" width="90%" 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-59-1.png" width="40%" style="display: block; margin: auto;" /> --- ## Facetting with `facet_wrap()` .pull-left4[ <br> <font size = 5><i>Facetting</i> = Create different plots for different groups</font><br> <font size = 5>To facet plots, use `facet_wrap()`</font><br> ```r # No faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-60-1.png" width="70%" style="display: block; margin: auto;" /> ] .pull-right55[ ```r # With faceting ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point() + facet_wrap(~ class) # Tilde first! ``` <img src="PlottingI_files/figure-html/unnamed-chunk-61-1.png" width="80%" style="display: block; margin: auto;" /> ] --- ## Assigning a ggplot to an object .pull-left35[ <br><br> <font size = 5>ggplot returns an object of the class "gg"</font><br> <font size = 5>You can assign the result of `ggplot` to an object.</font><br> <font size = 5>Evaluating the object will show the plot</font><br> <font size = 5>You can even edit existing `ggplot` objects</font><br> ] .pull-right6[ ```r # Create myplot myplot <- ggplot(data = mpg, aes(x = cty, y = hwy)) + geom_point() + theme_bw() class(myplot) ``` ``` [1] "gg" "ggplot" ``` ```r myplot # Evaluate myplot ``` <img src="PlottingI_files/figure-html/unnamed-chunk-62-1.png" width="60%" style="display: block; margin: auto;" /> ] --- ## Assigning ggplot() to an object .pull-left35[ ```r # Original plot myplot ``` <img src="PlottingI_files/figure-html/unnamed-chunk-63-1.png" width="100%" style="display: block; margin: auto;" /> ] .pull-right6[ ```r # Take the existing plot and add elements myplot + facet_wrap(~trans) + theme_dark() ``` <img src="PlottingI_files/figure-html/unnamed-chunk-64-1.png" width="70%" style="display: block; margin: auto;" /> ] --- ## Saving plots to image with ggsave() .pull-left5[ <br><br> <font size = 5>To save plots to a file (e.g.; .jpg, .pdf, .png), use the ggsave() function</font><br> |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[ <font size = 5>Save ggplot object called myplot to a pdf file</font><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 <font size 5>Check these links for more in depth ggplot2 guides</font> - ggplot2 main page: http://ggplot2.tidyverse.org/index.html - 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;" /> --- # Cheat Sheet! https://www.rstudio.com/wp-content/uploads/2015/03/ggplot2-cheatsheet.pdf <img src="https://raw.githubusercontent.com/therbootcamp/therbootcamp.github.io/master/_sessions/_image/ggplot_cheatsheet_ss.png" width="600" style="display: block; margin: auto;" /> --- ## Plotting Pratical <p><font size=6><b><a href="https://therbootcamp.github.io/BaselRBootcamp_2018April/_sessions/D3S2_PlottingI/PlottingI_practical.html">Link to Plotting practical</a> ---