class: center, middle, inverse, title-slide # Introduction to ggplot2 ### Derek Ogle, May 2020 --- class: inverse, center, middle # Today's Goal <font size="7">Build upon the basics of ggplot2 that we developed last time by adding more controls over elements of the graph.</font> --- # Background - Estimated counts of Gray Wolf (*Canis lupus*) and Moose (*Alces alces*) from Isle Royale. .center[ <img src="https://derekogle.com/NCGraphing/modules/zimgs/wolves_moose.jpg" width="500px" /> <font size="1">Credit: <a href="https://www.mtu.edu/news/stories/2012/march/wolf-population-isle-royale-drops-lowest-has-ever-been.html">Michigan Technological University</a></font> ] --- # Background - Downloaded 'Data_wolves_moose_Isle_Royale_June2019.xlsx' Excel file from [Wolves & Moose of Isle Royale](https://isleroyalewolf.org/data/data/home.html), deleted first row of the first sheet, and saved the first sheet as [this CSV file](https://raw.githubusercontent.com/droglenc/NCData/master/WolvesMoose_IsleRoyale_June2019.csv). -- - Loaded the data into the `irmw` object. ```r ## #!# Set to your own working directory and have just your filename below. irmw <- read.csv("https://raw.githubusercontent.com/droglenc/NCData/master/WolvesMoose_IsleRoyale_June2019.csv", na.strings=c("NA","N/A")) ``` --- # Background - Selected only five variables. - Renamed two variables with space in names (and long). - Changed categorical data from numbers to words and converted to a factor. ```r irmw %<>% select(year,wolves,moose,Jan.Feb..temp..F.,ice.bridges..0.none..1...present.) %>% rename(winter_temp=Jan.Feb..temp..F., ice_bridges=ice.bridges..0.none..1...present.) %>% mutate(ice_bridges=plyr::mapvalues(ice_bridges,from=c(0,1),to=c("no","yes")), ice_bridges=factor(ice_bridges)) ``` --- # Background ```r head(irmw) ``` ``` year wolves moose winter_temp ice_bridges 1 1959 20 538 1.40 no 2 1960 22 564 8.45 no 3 1961 22 572 9.75 yes 4 1962 23 579 2.15 yes 5 1963 20 596 -0.35 yes 6 1964 26 620 12.40 no ``` - Variables are: - **year**: Calendar year of observation. - **wolves**: Estimated number of wolves present that year. - **moose**: Estimated number of moose present that year. - **winter_temp**: Mean January-February temperature (<sup>o</sup>F). - **ice_bridges**: Whether or not an ice bridge formed to the mainland. --- ### What is Happening Here? Where is the graph? ```r p <- ggplot(data=irmw,mapping=aes(x=year,y=wolves)) + geom_line() ``` -- ```r p ``` <img src="Lecture_MooseWolves_files/figure-html/unnamed-chunk-6-1.png" width="45%" /> --- class: inverse, center, middle ### Controlling Specific Aspects of the Graph --- # Aesthetics - Visual properties of geoms on the graph. -- - Commonly used aesthetics are: - `x=`: positioning along x-axis. - `y=`: positioning along y-axis. -- - `color=`: [color](https://derekogle.com/NCGraphing/resources/colors) of objects or, if 2-d (e.g., bars, some points), object's outline. -- - `fill=`: fill [color](https://derekogle.com/NCGraphing/resources/colors) of objects. -- - `alpha=`: color transparency color (0 [fully transparent] to 1 [fully opaque]). -- - `shape=`: [shape](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html#point) of markers in scatter plots. -- - `size=`: how large objects appear. -- - `linetype=`: how [lines](https://ggplot2.tidyverse.org/articles/ggplot2-specs.html#lines) should be drawn (`"solid"`, `"dashed"`, `"dotted"`, etc.) -- - Which aesthetics are required and allowed vary by geom. --- class: inverse, center, middle # Aesthetics ### Can be *set* for all data within each 'geom'. --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_point( ) + geom_line( ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2a_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_point( * color="red", ) + geom_line( ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2a_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_point( color="red", * size=1 ) + geom_line( ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2a_non_seq_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_point( color="red", size=1 ) + geom_line( * size=0.25, ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2a_non_seq_4_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_point( color="red", size=1 ) + geom_line( size=0.25, * linetype="dashed" ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2a_non_seq_5_output-1.png" width="100%" /> ]] --- class: inverse, center, middle ### Order of geoms Becomes Apparent --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=irmw, * mapping=aes(x=year,y=wolves)) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2b_auto_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + * geom_line(size=0.25,linetype="dashed") p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2b_auto_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line(size=0.25,linetype="dashed") + * geom_point(color="red",size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr2b_auto_3_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Aesthetics ### Can be *globally mapped* to *a color* in aes() in mapping= in ggplot(). --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves, )) + geom_line() + geom_point(size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3a_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves, * color="red" )) + geom_line() + geom_point(size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3a_non_seq_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Aesthetics ### Can be *globally mapped* to *a variable* in aes() in mapping= in ggplot(). --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves, )) + geom_line() + geom_point(size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3b_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves, * color=ice_bridges )) + geom_line() + geom_point(size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3b_non_seq_2_output-1.png" width="100%" /> ]] --- class: inverse, center, middle # Aesthetics ### Can be *mapped* to *a variable* for a specific 'geom' with aes() in the geom_XXXX(). --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( size=1 ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3c_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( * aes(color=ice_bridges), size=1 ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3c_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( size=1 ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3d_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( * aes(color=winter_temp), size=1 ) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3d_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( aes( ), size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3e_non_seq_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( aes( * color=winter_temp, ), size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3e_non_seq_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point( aes( color=winter_temp, * shape=ice_bridges ), size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr3e_non_seq_3_output-1.png" width="100%" /> ]] --- # Scales - Scales define how values of the aesthetic are mapped to values of the data -- - For example ... - Defines colors used for each group. - Defines shapes of points used for each group. - Defines characteristics of axes (e.g., labels, breaks, limits). -- - All scales have sensible defaults; but you may want to change. -- - Scale functions are of the form `scale_`*`aesthetic`*`_`*`suffix`*. - *`aesthetic`* is an aesthetic name (e.g., `color`, `shape`, `x`). - *`suffix`* is the functionality of the scale (e.g., `manual`, `discrete`). -- - Basic functionality introduced here, more details in subsequent modules. --- class: inverse, center, middle # Using Scales to Change ... ### Default Colors ### Axis Names and Breaks --- class: split-50 count: false .column[.content[ ```r *p <- ggplot(data=irmw, * mapping=aes(x=year,y=wolves)) + * geom_line() + * geom_point(aes(color=ice_bridges),size=1) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr4a_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point(aes(color=ice_bridges),size=1) + * scale_color_manual(values=c("blue","red"), * name="Ice Bridge?") p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr4a_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point(aes(color=ice_bridges),size=1) + scale_color_manual(values=c("blue","red"), name="Ice Bridge?") + * scale_x_continuous(name="Year", * breaks=seq(1960,2020,10), * minor_breaks=seq(1959,2019,1)) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr4a_user_3_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point(aes(color=ice_bridges),size=1) + scale_color_manual(values=c("blue","red"), name="Ice Bridge?") + scale_x_continuous(name="Year", breaks=seq(1960,2020,10), minor_breaks=seq(1959,2019,1)) + * scale_y_continuous(name="Estimated Number of Wolves", * breaks=seq(0,50,5), * minor_breaks=NULL) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr4a_user_4_output-1.png" width="100%" /> ]] --- # Themes - A theme controls elements of the graph not related to data; e.g., - Background color. - Size of font. - Gridlines. -- - Each of these elements can be individually modified. -- - Entire themes can be created and saved for continual use. -- - We will revisit details of themes in a later module. -- - However, common themes exist; e.g., - `theme_bw()` - `theme_gray()` - `theme_minimal()` - `theme_classic()` --- class: split-50 count: false .column[.content[ ```r p <- ggplot(data=irmw, mapping=aes(x=year,y=wolves)) + geom_line() + geom_point(aes(color=ice_bridges),size=1) + scale_color_manual(values=c("blue","red"), name="Ice Bridge?") + scale_x_continuous(name="Year", breaks=seq(1960,2020,5)) + scale_y_continuous(name="Estimated Number of Wolves", breaks=seq(0,50,5)) p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5_1_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r *p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5a_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + * theme_classic() ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5a_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r * p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5b_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + * theme_minimal() ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5b_user_2_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r *p ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5c_user_1_output-1.png" width="100%" /> ]] --- class: split-50 count: false .column[.content[ ```r p + * theme_bw() ``` ]] .column[.content[ <img src="Lecture_MooseWolves_files/figure-html/scatline_yr5c_user_2_output-1.png" width="100%" /> ]] --- ## Final Thought for Next Time > "[This book will] introduce you to `ggplot2` as a novice, unfamiliar with the grammar; teach you the basics so that you can re-create plots you are already familiar with [and] show you how to use the grammar to create new types of graphics ..." -- Wickham (2009) .pull-right[ ![Hadley Wickham](https://derekogle.com/NCGraphing/modules/zimgs/wickham.jpg) ] --- class: inverse, center, middle # Next Time <font size="7">We will examine graphs for distributions of quantitative data.</font>